@babel/traverse 7.0.0-beta.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of @babel/traverse might be problematic. Click here for more details.

package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # @babel/traverse
2
+
3
+ > @babel/traverse maintains the overall tree state, and is responsible for replacing, removing, and adding nodes.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ $ npm install --save @babel/traverse
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ We can use it alongside Babylon to traverse and update nodes:
14
+
15
+ ```js
16
+ import * as babylon from "babylon";
17
+ import traverse from "@babel/traverse";
18
+
19
+ const code = `function square(n) {
20
+ return n * n;
21
+ }`;
22
+
23
+ const ast = babylon.parse(code);
24
+
25
+ traverse(ast, {
26
+ enter(path) {
27
+ if (path.isIdentifier({ name: "n" })) {
28
+ path.node.name = "x";
29
+ }
30
+ }
31
+ });
32
+ ```
33
+ [:book: **Read the full docs here**](https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-traverse)
package/lib/cache.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.clear = clear;
5
+ exports.clearPath = clearPath;
6
+ exports.clearScope = clearScope;
7
+ exports.scope = exports.path = void 0;
8
+ var path = new WeakMap();
9
+ exports.path = path;
10
+ var scope = new WeakMap();
11
+ exports.scope = scope;
12
+
13
+ function clear() {
14
+ clearPath();
15
+ clearScope();
16
+ }
17
+
18
+ function clearPath() {
19
+ exports.path = path = new WeakMap();
20
+ }
21
+
22
+ function clearScope() {
23
+ exports.scope = scope = new WeakMap();
24
+ }
package/lib/context.js ADDED
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+
6
+ var _path4 = _interopRequireDefault(require("./path"));
7
+
8
+ var t = _interopRequireWildcard(require("@babel/types"));
9
+
10
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ var testing = process.env.NODE_ENV === "test";
15
+
16
+ var TraversalContext = function () {
17
+ function TraversalContext(scope, opts, state, parentPath) {
18
+ this.parentPath = void 0;
19
+ this.scope = void 0;
20
+ this.state = void 0;
21
+ this.opts = void 0;
22
+ this.queue = null;
23
+ this.parentPath = parentPath;
24
+ this.scope = scope;
25
+ this.state = state;
26
+ this.opts = opts;
27
+ }
28
+
29
+ var _proto = TraversalContext.prototype;
30
+
31
+ _proto.shouldVisit = function shouldVisit(node) {
32
+ var opts = this.opts;
33
+ if (opts.enter || opts.exit) return true;
34
+ if (opts[node.type]) return true;
35
+ var keys = t.VISITOR_KEYS[node.type];
36
+ if (!keys || !keys.length) return false;
37
+
38
+ for (var _iterator = keys, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
39
+ var _ref;
40
+
41
+ if (_isArray) {
42
+ if (_i >= _iterator.length) break;
43
+ _ref = _iterator[_i++];
44
+ } else {
45
+ _i = _iterator.next();
46
+ if (_i.done) break;
47
+ _ref = _i.value;
48
+ }
49
+
50
+ var _key = _ref;
51
+ if (node[_key]) return true;
52
+ }
53
+
54
+ return false;
55
+ };
56
+
57
+ _proto.create = function create(node, obj, key, listKey) {
58
+ return _path4.default.get({
59
+ parentPath: this.parentPath,
60
+ parent: node,
61
+ container: obj,
62
+ key: key,
63
+ listKey: listKey
64
+ });
65
+ };
66
+
67
+ _proto.maybeQueue = function maybeQueue(path, notPriority) {
68
+ if (this.trap) {
69
+ throw new Error("Infinite cycle detected");
70
+ }
71
+
72
+ if (this.queue) {
73
+ if (notPriority) {
74
+ this.queue.push(path);
75
+ } else {
76
+ this.priorityQueue.push(path);
77
+ }
78
+ }
79
+ };
80
+
81
+ _proto.visitMultiple = function visitMultiple(container, parent, listKey) {
82
+ if (container.length === 0) return false;
83
+ var queue = [];
84
+
85
+ for (var key = 0; key < container.length; key++) {
86
+ var node = container[key];
87
+
88
+ if (node && this.shouldVisit(node)) {
89
+ queue.push(this.create(parent, container, key, listKey));
90
+ }
91
+ }
92
+
93
+ return this.visitQueue(queue);
94
+ };
95
+
96
+ _proto.visitSingle = function visitSingle(node, key) {
97
+ if (this.shouldVisit(node[key])) {
98
+ return this.visitQueue([this.create(node, node, key)]);
99
+ } else {
100
+ return false;
101
+ }
102
+ };
103
+
104
+ _proto.visitQueue = function visitQueue(queue) {
105
+ this.queue = queue;
106
+ this.priorityQueue = [];
107
+ var visited = [];
108
+ var stop = false;
109
+
110
+ for (var _iterator2 = queue, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
111
+ var _ref2;
112
+
113
+ if (_isArray2) {
114
+ if (_i2 >= _iterator2.length) break;
115
+ _ref2 = _iterator2[_i2++];
116
+ } else {
117
+ _i2 = _iterator2.next();
118
+ if (_i2.done) break;
119
+ _ref2 = _i2.value;
120
+ }
121
+
122
+ var _path2 = _ref2;
123
+
124
+ _path2.resync();
125
+
126
+ if (_path2.contexts.length === 0 || _path2.contexts[_path2.contexts.length - 1] !== this) {
127
+ _path2.pushContext(this);
128
+ }
129
+
130
+ if (_path2.key === null) continue;
131
+
132
+ if (testing && queue.length >= 10000) {
133
+ this.trap = true;
134
+ }
135
+
136
+ if (visited.indexOf(_path2.node) >= 0) continue;
137
+ visited.push(_path2.node);
138
+
139
+ if (_path2.visit()) {
140
+ stop = true;
141
+ break;
142
+ }
143
+
144
+ if (this.priorityQueue.length) {
145
+ stop = this.visitQueue(this.priorityQueue);
146
+ this.priorityQueue = [];
147
+ this.queue = queue;
148
+ if (stop) break;
149
+ }
150
+ }
151
+
152
+ for (var _iterator3 = queue, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
153
+ var _ref3;
154
+
155
+ if (_isArray3) {
156
+ if (_i3 >= _iterator3.length) break;
157
+ _ref3 = _iterator3[_i3++];
158
+ } else {
159
+ _i3 = _iterator3.next();
160
+ if (_i3.done) break;
161
+ _ref3 = _i3.value;
162
+ }
163
+
164
+ var _path3 = _ref3;
165
+
166
+ _path3.popContext();
167
+ }
168
+
169
+ this.queue = null;
170
+ return stop;
171
+ };
172
+
173
+ _proto.visit = function visit(node, key) {
174
+ var nodes = node[key];
175
+ if (!nodes) return false;
176
+
177
+ if (Array.isArray(nodes)) {
178
+ return this.visitMultiple(nodes, node, key);
179
+ } else {
180
+ return this.visitSingle(node, key);
181
+ }
182
+ };
183
+
184
+ return TraversalContext;
185
+ }();
186
+
187
+ exports.default = TraversalContext;
package/lib/hub.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+
6
+ var Hub = function Hub(file) {
7
+ this.file = file;
8
+ };
9
+
10
+ exports.default = Hub;
package/lib/index.js ADDED
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = traverse;
5
+ Object.defineProperty(exports, "NodePath", {
6
+ enumerable: true,
7
+ get: function get() {
8
+ return _path.default;
9
+ }
10
+ });
11
+ Object.defineProperty(exports, "Scope", {
12
+ enumerable: true,
13
+ get: function get() {
14
+ return _scope.default;
15
+ }
16
+ });
17
+ Object.defineProperty(exports, "Hub", {
18
+ enumerable: true,
19
+ get: function get() {
20
+ return _hub.default;
21
+ }
22
+ });
23
+ exports.visitors = void 0;
24
+
25
+ var _context = _interopRequireDefault(require("./context"));
26
+
27
+ var visitors = _interopRequireWildcard(require("./visitors"));
28
+
29
+ exports.visitors = visitors;
30
+
31
+ var _includes = _interopRequireDefault(require("lodash/includes"));
32
+
33
+ var t = _interopRequireWildcard(require("@babel/types"));
34
+
35
+ var cache = _interopRequireWildcard(require("./cache"));
36
+
37
+ var _path = _interopRequireDefault(require("./path"));
38
+
39
+ var _scope = _interopRequireDefault(require("./scope"));
40
+
41
+ var _hub = _interopRequireDefault(require("./hub"));
42
+
43
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
44
+
45
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
46
+
47
+ function traverse(parent, opts, scope, state, parentPath) {
48
+ if (!parent) return;
49
+ if (!opts) opts = {};
50
+
51
+ if (!opts.noScope && !scope) {
52
+ if (parent.type !== "Program" && parent.type !== "File") {
53
+ throw new Error("You must pass a scope and parentPath unless traversing a Program/File. " + ("Instead of that you tried to traverse a " + parent.type + " node without ") + "passing scope and parentPath.");
54
+ }
55
+ }
56
+
57
+ visitors.explode(opts);
58
+ traverse.node(parent, opts, scope, state, parentPath);
59
+ }
60
+
61
+ traverse.visitors = visitors;
62
+ traverse.verify = visitors.verify;
63
+ traverse.explode = visitors.explode;
64
+ traverse.NodePath = require("./path");
65
+ traverse.Scope = require("./scope");
66
+ traverse.Hub = require("./hub");
67
+
68
+ traverse.cheap = function (node, enter) {
69
+ return t.traverseFast(node, enter);
70
+ };
71
+
72
+ traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
73
+ var keys = t.VISITOR_KEYS[node.type];
74
+ if (!keys) return;
75
+ var context = new _context.default(scope, opts, state, parentPath);
76
+
77
+ for (var _iterator = keys, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
78
+ var _ref;
79
+
80
+ if (_isArray) {
81
+ if (_i >= _iterator.length) break;
82
+ _ref = _iterator[_i++];
83
+ } else {
84
+ _i = _iterator.next();
85
+ if (_i.done) break;
86
+ _ref = _i.value;
87
+ }
88
+
89
+ var _key = _ref;
90
+ if (skipKeys && skipKeys[_key]) continue;
91
+ if (context.visit(node, _key)) return;
92
+ }
93
+ };
94
+
95
+ traverse.clearNode = function (node, opts) {
96
+ t.removeProperties(node, opts);
97
+ cache.path.delete(node);
98
+ };
99
+
100
+ traverse.removeProperties = function (tree, opts) {
101
+ t.traverseFast(tree, traverse.clearNode, opts);
102
+ return tree;
103
+ };
104
+
105
+ function hasBlacklistedType(path, state) {
106
+ if (path.node.type === state.type) {
107
+ state.has = true;
108
+ path.stop();
109
+ }
110
+ }
111
+
112
+ traverse.hasType = function (tree, type, blacklistTypes) {
113
+ if ((0, _includes.default)(blacklistTypes, tree.type)) return false;
114
+ if (tree.type === type) return true;
115
+ var state = {
116
+ has: false,
117
+ type: type
118
+ };
119
+ traverse(tree, {
120
+ noScope: true,
121
+ blacklist: blacklistTypes,
122
+ enter: hasBlacklistedType
123
+ }, null, state);
124
+ return state.has;
125
+ };
126
+
127
+ traverse.cache = cache;
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.findParent = findParent;
5
+ exports.find = find;
6
+ exports.getFunctionParent = getFunctionParent;
7
+ exports.getStatementParent = getStatementParent;
8
+ exports.getEarliestCommonAncestorFrom = getEarliestCommonAncestorFrom;
9
+ exports.getDeepestCommonAncestorFrom = getDeepestCommonAncestorFrom;
10
+ exports.getAncestry = getAncestry;
11
+ exports.isAncestor = isAncestor;
12
+ exports.isDescendant = isDescendant;
13
+ exports.inType = inType;
14
+
15
+ var t = _interopRequireWildcard(require("@babel/types"));
16
+
17
+ var _index = _interopRequireDefault(require("./index"));
18
+
19
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
+
21
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
22
+
23
+ function findParent(callback) {
24
+ var path = this;
25
+
26
+ while (path = path.parentPath) {
27
+ if (callback(path)) return path;
28
+ }
29
+
30
+ return null;
31
+ }
32
+
33
+ function find(callback) {
34
+ var path = this;
35
+
36
+ do {
37
+ if (callback(path)) return path;
38
+ } while (path = path.parentPath);
39
+
40
+ return null;
41
+ }
42
+
43
+ function getFunctionParent() {
44
+ return this.findParent(function (p) {
45
+ return p.isFunction();
46
+ });
47
+ }
48
+
49
+ function getStatementParent() {
50
+ var path = this;
51
+
52
+ do {
53
+ if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
54
+ break;
55
+ } else {
56
+ path = path.parentPath;
57
+ }
58
+ } while (path);
59
+
60
+ if (path && (path.isProgram() || path.isFile())) {
61
+ throw new Error("File/Program node, we can't possibly find a statement parent to this");
62
+ }
63
+
64
+ return path;
65
+ }
66
+
67
+ function getEarliestCommonAncestorFrom(paths) {
68
+ return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
69
+ var earliest;
70
+ var keys = t.VISITOR_KEYS[deepest.type];
71
+ var _arr = ancestries;
72
+
73
+ for (var _i = 0; _i < _arr.length; _i++) {
74
+ var ancestry = _arr[_i];
75
+ var path = ancestry[i + 1];
76
+
77
+ if (!earliest) {
78
+ earliest = path;
79
+ continue;
80
+ }
81
+
82
+ if (path.listKey && earliest.listKey === path.listKey) {
83
+ if (path.key < earliest.key) {
84
+ earliest = path;
85
+ continue;
86
+ }
87
+ }
88
+
89
+ var earliestKeyIndex = keys.indexOf(earliest.parentKey);
90
+ var currentKeyIndex = keys.indexOf(path.parentKey);
91
+
92
+ if (earliestKeyIndex > currentKeyIndex) {
93
+ earliest = path;
94
+ }
95
+ }
96
+
97
+ return earliest;
98
+ });
99
+ }
100
+
101
+ function getDeepestCommonAncestorFrom(paths, filter) {
102
+ var _this = this;
103
+
104
+ if (!paths.length) {
105
+ return this;
106
+ }
107
+
108
+ if (paths.length === 1) {
109
+ return paths[0];
110
+ }
111
+
112
+ var minDepth = Infinity;
113
+ var lastCommonIndex, lastCommon;
114
+ var ancestries = paths.map(function (path) {
115
+ var ancestry = [];
116
+
117
+ do {
118
+ ancestry.unshift(path);
119
+ } while ((path = path.parentPath) && path !== _this);
120
+
121
+ if (ancestry.length < minDepth) {
122
+ minDepth = ancestry.length;
123
+ }
124
+
125
+ return ancestry;
126
+ });
127
+ var first = ancestries[0];
128
+
129
+ depthLoop: for (var i = 0; i < minDepth; i++) {
130
+ var shouldMatch = first[i];
131
+ var _arr2 = ancestries;
132
+
133
+ for (var _i2 = 0; _i2 < _arr2.length; _i2++) {
134
+ var ancestry = _arr2[_i2];
135
+
136
+ if (ancestry[i] !== shouldMatch) {
137
+ break depthLoop;
138
+ }
139
+ }
140
+
141
+ lastCommonIndex = i;
142
+ lastCommon = shouldMatch;
143
+ }
144
+
145
+ if (lastCommon) {
146
+ if (filter) {
147
+ return filter(lastCommon, lastCommonIndex, ancestries);
148
+ } else {
149
+ return lastCommon;
150
+ }
151
+ } else {
152
+ throw new Error("Couldn't find intersection");
153
+ }
154
+ }
155
+
156
+ function getAncestry() {
157
+ var path = this;
158
+ var paths = [];
159
+
160
+ do {
161
+ paths.push(path);
162
+ } while (path = path.parentPath);
163
+
164
+ return paths;
165
+ }
166
+
167
+ function isAncestor(maybeDescendant) {
168
+ return maybeDescendant.isDescendant(this);
169
+ }
170
+
171
+ function isDescendant(maybeAncestor) {
172
+ return !!this.findParent(function (parent) {
173
+ return parent === maybeAncestor;
174
+ });
175
+ }
176
+
177
+ function inType() {
178
+ var path = this;
179
+
180
+ while (path) {
181
+ var _arr3 = arguments;
182
+
183
+ for (var _i3 = 0; _i3 < _arr3.length; _i3++) {
184
+ var type = _arr3[_i3];
185
+ if (path.node.type === type) return true;
186
+ }
187
+
188
+ path = path.parentPath;
189
+ }
190
+
191
+ return false;
192
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.shareCommentsWithSiblings = shareCommentsWithSiblings;
5
+ exports.addComment = addComment;
6
+ exports.addComments = addComments;
7
+
8
+ var t = _interopRequireWildcard(require("@babel/types"));
9
+
10
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
11
+
12
+ function shareCommentsWithSiblings() {
13
+ if (typeof this.key === "string") return;
14
+ var node = this.node;
15
+ if (!node) return;
16
+ var trailing = node.trailingComments;
17
+ var leading = node.leadingComments;
18
+ if (!trailing && !leading) return;
19
+ var prev = this.getSibling(this.key - 1);
20
+ var next = this.getSibling(this.key + 1);
21
+ var hasPrev = Boolean(prev.node);
22
+ var hasNext = Boolean(next.node);
23
+
24
+ if (hasPrev && hasNext) {} else if (hasPrev) {
25
+ prev.addComments("trailing", trailing);
26
+ } else if (hasNext) {
27
+ next.addComments("leading", leading);
28
+ }
29
+ }
30
+
31
+ function addComment(type, content, line) {
32
+ t.addComment(this.node, type, content, line);
33
+ }
34
+
35
+ function addComments(type, comments) {
36
+ t.addComments(this.node, type, comments);
37
+ }