@babel/traverse 7.0.0-beta.5 → 7.0.0-beta.53
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 +9 -23
- package/lib/cache.js +5 -3
- package/lib/context.js +55 -91
- package/lib/hub.js +9 -4
- package/lib/index.js +36 -33
- package/lib/path/ancestry.js +37 -41
- package/lib/path/comments.js +22 -12
- package/lib/path/context.js +24 -48
- package/lib/path/conversion.js +170 -150
- package/lib/path/evaluation.js +108 -150
- package/lib/path/family.js +40 -43
- package/lib/path/index.js +99 -80
- package/lib/path/inference/index.js +39 -33
- package/lib/path/inference/inferer-reference.js +55 -49
- package/lib/path/inference/inferers.js +61 -49
- package/lib/path/introspection.js +105 -96
- package/lib/path/lib/hoister.js +63 -61
- package/lib/path/lib/removal-hooks.js +5 -3
- package/lib/path/lib/virtual-types.js +106 -66
- package/lib/path/modification.js +70 -68
- package/lib/path/removal.js +12 -5
- package/lib/path/replacement.js +76 -69
- package/lib/scope/binding.js +23 -23
- package/lib/scope/index.js +389 -487
- package/lib/scope/lib/renamer.js +71 -61
- package/lib/visitors.js +84 -124
- package/package.json +11 -10
package/README.md
CHANGED
@@ -1,33 +1,19 @@
|
|
1
1
|
# @babel/traverse
|
2
2
|
|
3
|
-
>
|
3
|
+
> The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes
|
4
|
+
|
5
|
+
See our website [@babel/traverse](https://babeljs.io/docs/en/next/babel-traverse.html) for more information or the [issues](https://github.com/babel/babel/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3A%22pkg%3A%20traverse%22+is%3Aopen) associated with this package.
|
4
6
|
|
5
7
|
## Install
|
6
8
|
|
9
|
+
Using npm:
|
10
|
+
|
7
11
|
```sh
|
8
|
-
|
12
|
+
npm install --save-dev @babel/traverse
|
9
13
|
```
|
10
14
|
|
11
|
-
|
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";
|
15
|
+
or using yarn:
|
18
16
|
|
19
|
-
|
20
|
-
|
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
|
-
});
|
17
|
+
```sh
|
18
|
+
yarn add @babel/traverse --dev
|
32
19
|
```
|
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
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.clear = clear;
|
5
7
|
exports.clearPath = clearPath;
|
6
8
|
exports.clearScope = clearScope;
|
7
9
|
exports.scope = exports.path = void 0;
|
8
|
-
|
10
|
+
let path = new WeakMap();
|
9
11
|
exports.path = path;
|
10
|
-
|
12
|
+
let scope = new WeakMap();
|
11
13
|
exports.scope = scope;
|
12
14
|
|
13
15
|
function clear() {
|
package/lib/context.js
CHANGED
@@ -1,71 +1,62 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.default = void 0;
|
5
7
|
|
6
|
-
var
|
8
|
+
var _path = _interopRequireDefault(require("./path"));
|
7
9
|
|
8
|
-
|
10
|
+
function t() {
|
11
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
9
12
|
|
10
|
-
|
13
|
+
t = function () {
|
14
|
+
return data;
|
15
|
+
};
|
16
|
+
|
17
|
+
return data;
|
18
|
+
}
|
19
|
+
|
20
|
+
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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
11
21
|
|
12
22
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13
23
|
|
14
|
-
|
24
|
+
const testing = process.env.NODE_ENV === "test";
|
15
25
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
configurable: true,
|
20
|
-
enumerable: true,
|
21
|
-
writable: true,
|
22
|
-
value: null
|
23
|
-
});
|
26
|
+
class TraversalContext {
|
27
|
+
constructor(scope, opts, state, parentPath) {
|
28
|
+
this.queue = null;
|
24
29
|
this.parentPath = parentPath;
|
25
30
|
this.scope = scope;
|
26
31
|
this.state = state;
|
27
32
|
this.opts = opts;
|
28
33
|
}
|
29
34
|
|
30
|
-
|
31
|
-
|
32
|
-
_proto.shouldVisit = function shouldVisit(node) {
|
33
|
-
var opts = this.opts;
|
35
|
+
shouldVisit(node) {
|
36
|
+
const opts = this.opts;
|
34
37
|
if (opts.enter || opts.exit) return true;
|
35
38
|
if (opts[node.type]) return true;
|
36
|
-
|
39
|
+
const keys = t().VISITOR_KEYS[node.type];
|
37
40
|
if (!keys || !keys.length) return false;
|
38
41
|
|
39
|
-
for (
|
40
|
-
|
41
|
-
|
42
|
-
if (_isArray) {
|
43
|
-
if (_i >= _iterator.length) break;
|
44
|
-
_ref = _iterator[_i++];
|
45
|
-
} else {
|
46
|
-
_i = _iterator.next();
|
47
|
-
if (_i.done) break;
|
48
|
-
_ref = _i.value;
|
49
|
-
}
|
50
|
-
|
51
|
-
var _key = _ref;
|
52
|
-
if (node[_key]) return true;
|
42
|
+
for (const key of keys) {
|
43
|
+
if (node[key]) return true;
|
53
44
|
}
|
54
45
|
|
55
46
|
return false;
|
56
|
-
}
|
47
|
+
}
|
57
48
|
|
58
|
-
|
59
|
-
return
|
49
|
+
create(node, obj, key, listKey) {
|
50
|
+
return _path.default.get({
|
60
51
|
parentPath: this.parentPath,
|
61
52
|
parent: node,
|
62
53
|
container: obj,
|
63
54
|
key: key,
|
64
|
-
listKey
|
55
|
+
listKey
|
65
56
|
});
|
66
|
-
}
|
57
|
+
}
|
67
58
|
|
68
|
-
|
59
|
+
maybeQueue(path, notPriority) {
|
69
60
|
if (this.trap) {
|
70
61
|
throw new Error("Infinite cycle detected");
|
71
62
|
}
|
@@ -77,14 +68,14 @@ var TraversalContext = function () {
|
|
77
68
|
this.priorityQueue.push(path);
|
78
69
|
}
|
79
70
|
}
|
80
|
-
}
|
71
|
+
}
|
81
72
|
|
82
|
-
|
73
|
+
visitMultiple(container, parent, listKey) {
|
83
74
|
if (container.length === 0) return false;
|
84
|
-
|
75
|
+
const queue = [];
|
85
76
|
|
86
|
-
for (
|
87
|
-
|
77
|
+
for (let key = 0; key < container.length; key++) {
|
78
|
+
const node = container[key];
|
88
79
|
|
89
80
|
if (node && this.shouldVisit(node)) {
|
90
81
|
queue.push(this.create(parent, container, key, listKey));
|
@@ -92,52 +83,39 @@ var TraversalContext = function () {
|
|
92
83
|
}
|
93
84
|
|
94
85
|
return this.visitQueue(queue);
|
95
|
-
}
|
86
|
+
}
|
96
87
|
|
97
|
-
|
88
|
+
visitSingle(node, key) {
|
98
89
|
if (this.shouldVisit(node[key])) {
|
99
90
|
return this.visitQueue([this.create(node, node, key)]);
|
100
91
|
} else {
|
101
92
|
return false;
|
102
93
|
}
|
103
|
-
}
|
94
|
+
}
|
104
95
|
|
105
|
-
|
96
|
+
visitQueue(queue) {
|
106
97
|
this.queue = queue;
|
107
98
|
this.priorityQueue = [];
|
108
|
-
|
109
|
-
|
99
|
+
const visited = [];
|
100
|
+
let stop = false;
|
110
101
|
|
111
|
-
for (
|
112
|
-
|
102
|
+
for (const path of queue) {
|
103
|
+
path.resync();
|
113
104
|
|
114
|
-
if (
|
115
|
-
|
116
|
-
_ref2 = _iterator2[_i2++];
|
117
|
-
} else {
|
118
|
-
_i2 = _iterator2.next();
|
119
|
-
if (_i2.done) break;
|
120
|
-
_ref2 = _i2.value;
|
105
|
+
if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
|
106
|
+
path.pushContext(this);
|
121
107
|
}
|
122
108
|
|
123
|
-
|
124
|
-
|
125
|
-
_path2.resync();
|
126
|
-
|
127
|
-
if (_path2.contexts.length === 0 || _path2.contexts[_path2.contexts.length - 1] !== this) {
|
128
|
-
_path2.pushContext(this);
|
129
|
-
}
|
130
|
-
|
131
|
-
if (_path2.key === null) continue;
|
109
|
+
if (path.key === null) continue;
|
132
110
|
|
133
111
|
if (testing && queue.length >= 10000) {
|
134
112
|
this.trap = true;
|
135
113
|
}
|
136
114
|
|
137
|
-
if (visited.indexOf(
|
138
|
-
visited.push(
|
115
|
+
if (visited.indexOf(path.node) >= 0) continue;
|
116
|
+
visited.push(path.node);
|
139
117
|
|
140
|
-
if (
|
118
|
+
if (path.visit()) {
|
141
119
|
stop = true;
|
142
120
|
break;
|
143
121
|
}
|
@@ -150,29 +128,16 @@ var TraversalContext = function () {
|
|
150
128
|
}
|
151
129
|
}
|
152
130
|
|
153
|
-
for (
|
154
|
-
|
155
|
-
|
156
|
-
if (_isArray3) {
|
157
|
-
if (_i3 >= _iterator3.length) break;
|
158
|
-
_ref3 = _iterator3[_i3++];
|
159
|
-
} else {
|
160
|
-
_i3 = _iterator3.next();
|
161
|
-
if (_i3.done) break;
|
162
|
-
_ref3 = _i3.value;
|
163
|
-
}
|
164
|
-
|
165
|
-
var _path3 = _ref3;
|
166
|
-
|
167
|
-
_path3.popContext();
|
131
|
+
for (const path of queue) {
|
132
|
+
path.popContext();
|
168
133
|
}
|
169
134
|
|
170
135
|
this.queue = null;
|
171
136
|
return stop;
|
172
|
-
}
|
137
|
+
}
|
173
138
|
|
174
|
-
|
175
|
-
|
139
|
+
visit(node, key) {
|
140
|
+
const nodes = node[key];
|
176
141
|
if (!nodes) return false;
|
177
142
|
|
178
143
|
if (Array.isArray(nodes)) {
|
@@ -180,9 +145,8 @@ var TraversalContext = function () {
|
|
180
145
|
} else {
|
181
146
|
return this.visitSingle(node, key);
|
182
147
|
}
|
183
|
-
}
|
148
|
+
}
|
184
149
|
|
185
|
-
|
186
|
-
}();
|
150
|
+
}
|
187
151
|
|
188
152
|
exports.default = TraversalContext;
|
package/lib/hub.js
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.default = void 0;
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
class Hub {
|
9
|
+
constructor(file) {
|
10
|
+
this.file = file;
|
11
|
+
}
|
12
|
+
|
13
|
+
}
|
9
14
|
|
10
15
|
exports.default = Hub;
|
package/lib/index.js
CHANGED
@@ -1,22 +1,24 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.default = traverse;
|
5
7
|
Object.defineProperty(exports, "NodePath", {
|
6
8
|
enumerable: true,
|
7
|
-
get: function
|
9
|
+
get: function () {
|
8
10
|
return _path.default;
|
9
11
|
}
|
10
12
|
});
|
11
13
|
Object.defineProperty(exports, "Scope", {
|
12
14
|
enumerable: true,
|
13
|
-
get: function
|
15
|
+
get: function () {
|
14
16
|
return _scope.default;
|
15
17
|
}
|
16
18
|
});
|
17
19
|
Object.defineProperty(exports, "Hub", {
|
18
20
|
enumerable: true,
|
19
|
-
get: function
|
21
|
+
get: function () {
|
20
22
|
return _hub.default;
|
21
23
|
}
|
22
24
|
});
|
@@ -28,9 +30,25 @@ var visitors = _interopRequireWildcard(require("./visitors"));
|
|
28
30
|
|
29
31
|
exports.visitors = visitors;
|
30
32
|
|
31
|
-
|
33
|
+
function _includes() {
|
34
|
+
const data = _interopRequireDefault(require("lodash/includes"));
|
35
|
+
|
36
|
+
_includes = function () {
|
37
|
+
return data;
|
38
|
+
};
|
39
|
+
|
40
|
+
return data;
|
41
|
+
}
|
42
|
+
|
43
|
+
function t() {
|
44
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
32
45
|
|
33
|
-
|
46
|
+
t = function () {
|
47
|
+
return data;
|
48
|
+
};
|
49
|
+
|
50
|
+
return data;
|
51
|
+
}
|
34
52
|
|
35
53
|
var cache = _interopRequireWildcard(require("./cache"));
|
36
54
|
|
@@ -40,7 +58,7 @@ var _scope = _interopRequireDefault(require("./scope"));
|
|
40
58
|
|
41
59
|
var _hub = _interopRequireDefault(require("./hub"));
|
42
60
|
|
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; } }
|
61
|
+
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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
44
62
|
|
45
63
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
46
64
|
|
@@ -50,7 +68,7 @@ function traverse(parent, opts, scope, state, parentPath) {
|
|
50
68
|
|
51
69
|
if (!opts.noScope && !scope) {
|
52
70
|
if (parent.type !== "Program" && parent.type !== "File") {
|
53
|
-
throw new Error("You must pass a scope and parentPath unless traversing a Program/File. " +
|
71
|
+
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
72
|
}
|
55
73
|
}
|
56
74
|
|
@@ -61,44 +79,29 @@ function traverse(parent, opts, scope, state, parentPath) {
|
|
61
79
|
traverse.visitors = visitors;
|
62
80
|
traverse.verify = visitors.verify;
|
63
81
|
traverse.explode = visitors.explode;
|
64
|
-
traverse.NodePath = require("./path");
|
65
|
-
traverse.Scope = require("./scope");
|
66
|
-
traverse.Hub = require("./hub");
|
67
82
|
|
68
83
|
traverse.cheap = function (node, enter) {
|
69
|
-
return t.traverseFast(node, enter);
|
84
|
+
return t().traverseFast(node, enter);
|
70
85
|
};
|
71
86
|
|
72
87
|
traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
|
73
|
-
|
88
|
+
const keys = t().VISITOR_KEYS[node.type];
|
74
89
|
if (!keys) return;
|
75
|
-
|
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
|
-
}
|
90
|
+
const context = new _context.default(scope, opts, state, parentPath);
|
88
91
|
|
89
|
-
|
90
|
-
if (skipKeys && skipKeys[
|
91
|
-
if (context.visit(node,
|
92
|
+
for (const key of keys) {
|
93
|
+
if (skipKeys && skipKeys[key]) continue;
|
94
|
+
if (context.visit(node, key)) return;
|
92
95
|
}
|
93
96
|
};
|
94
97
|
|
95
98
|
traverse.clearNode = function (node, opts) {
|
96
|
-
t.removeProperties(node, opts);
|
99
|
+
t().removeProperties(node, opts);
|
97
100
|
cache.path.delete(node);
|
98
101
|
};
|
99
102
|
|
100
103
|
traverse.removeProperties = function (tree, opts) {
|
101
|
-
t.traverseFast(tree, traverse.clearNode, opts);
|
104
|
+
t().traverseFast(tree, traverse.clearNode, opts);
|
102
105
|
return tree;
|
103
106
|
};
|
104
107
|
|
@@ -110,9 +113,9 @@ function hasBlacklistedType(path, state) {
|
|
110
113
|
}
|
111
114
|
|
112
115
|
traverse.hasType = function (tree, type, blacklistTypes) {
|
113
|
-
if ((0, _includes.default)(blacklistTypes, tree.type)) return false;
|
116
|
+
if ((0, _includes().default)(blacklistTypes, tree.type)) return false;
|
114
117
|
if (tree.type === type) return true;
|
115
|
-
|
118
|
+
const state = {
|
116
119
|
has: false,
|
117
120
|
type: type
|
118
121
|
};
|
package/lib/path/ancestry.js
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.findParent = findParent;
|
5
7
|
exports.find = find;
|
6
8
|
exports.getFunctionParent = getFunctionParent;
|
@@ -12,16 +14,24 @@ exports.isAncestor = isAncestor;
|
|
12
14
|
exports.isDescendant = isDescendant;
|
13
15
|
exports.inType = inType;
|
14
16
|
|
15
|
-
|
17
|
+
function t() {
|
18
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
19
|
+
|
20
|
+
t = function () {
|
21
|
+
return data;
|
22
|
+
};
|
23
|
+
|
24
|
+
return data;
|
25
|
+
}
|
16
26
|
|
17
27
|
var _index = _interopRequireDefault(require("./index"));
|
18
28
|
|
19
29
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
20
30
|
|
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; } }
|
31
|
+
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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
22
32
|
|
23
33
|
function findParent(callback) {
|
24
|
-
|
34
|
+
let path = this;
|
25
35
|
|
26
36
|
while (path = path.parentPath) {
|
27
37
|
if (callback(path)) return path;
|
@@ -31,7 +41,7 @@ function findParent(callback) {
|
|
31
41
|
}
|
32
42
|
|
33
43
|
function find(callback) {
|
34
|
-
|
44
|
+
let path = this;
|
35
45
|
|
36
46
|
do {
|
37
47
|
if (callback(path)) return path;
|
@@ -41,13 +51,11 @@ function find(callback) {
|
|
41
51
|
}
|
42
52
|
|
43
53
|
function getFunctionParent() {
|
44
|
-
return this.findParent(
|
45
|
-
return p.isFunction();
|
46
|
-
});
|
54
|
+
return this.findParent(p => p.isFunction());
|
47
55
|
}
|
48
56
|
|
49
57
|
function getStatementParent() {
|
50
|
-
|
58
|
+
let path = this;
|
51
59
|
|
52
60
|
do {
|
53
61
|
if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
|
@@ -66,13 +74,11 @@ function getStatementParent() {
|
|
66
74
|
|
67
75
|
function getEarliestCommonAncestorFrom(paths) {
|
68
76
|
return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
|
69
|
-
|
70
|
-
|
71
|
-
var _arr = ancestries;
|
77
|
+
let earliest;
|
78
|
+
const keys = t().VISITOR_KEYS[deepest.type];
|
72
79
|
|
73
|
-
for (
|
74
|
-
|
75
|
-
var path = ancestry[i + 1];
|
80
|
+
for (const ancestry of ancestries) {
|
81
|
+
const path = ancestry[i + 1];
|
76
82
|
|
77
83
|
if (!earliest) {
|
78
84
|
earliest = path;
|
@@ -86,8 +92,8 @@ function getEarliestCommonAncestorFrom(paths) {
|
|
86
92
|
}
|
87
93
|
}
|
88
94
|
|
89
|
-
|
90
|
-
|
95
|
+
const earliestKeyIndex = keys.indexOf(earliest.parentKey);
|
96
|
+
const currentKeyIndex = keys.indexOf(path.parentKey);
|
91
97
|
|
92
98
|
if (earliestKeyIndex > currentKeyIndex) {
|
93
99
|
earliest = path;
|
@@ -99,8 +105,6 @@ function getEarliestCommonAncestorFrom(paths) {
|
|
99
105
|
}
|
100
106
|
|
101
107
|
function getDeepestCommonAncestorFrom(paths, filter) {
|
102
|
-
var _this = this;
|
103
|
-
|
104
108
|
if (!paths.length) {
|
105
109
|
return this;
|
106
110
|
}
|
@@ -109,14 +113,14 @@ function getDeepestCommonAncestorFrom(paths, filter) {
|
|
109
113
|
return paths[0];
|
110
114
|
}
|
111
115
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
+
let minDepth = Infinity;
|
117
|
+
let lastCommonIndex, lastCommon;
|
118
|
+
const ancestries = paths.map(path => {
|
119
|
+
const ancestry = [];
|
116
120
|
|
117
121
|
do {
|
118
122
|
ancestry.unshift(path);
|
119
|
-
} while ((path = path.parentPath) && path !==
|
123
|
+
} while ((path = path.parentPath) && path !== this);
|
120
124
|
|
121
125
|
if (ancestry.length < minDepth) {
|
122
126
|
minDepth = ancestry.length;
|
@@ -124,15 +128,12 @@ function getDeepestCommonAncestorFrom(paths, filter) {
|
|
124
128
|
|
125
129
|
return ancestry;
|
126
130
|
});
|
127
|
-
|
128
|
-
|
129
|
-
depthLoop: for (var i = 0; i < minDepth; i++) {
|
130
|
-
var shouldMatch = first[i];
|
131
|
-
var _arr2 = ancestries;
|
131
|
+
const first = ancestries[0];
|
132
132
|
|
133
|
-
|
134
|
-
|
133
|
+
depthLoop: for (let i = 0; i < minDepth; i++) {
|
134
|
+
const shouldMatch = first[i];
|
135
135
|
|
136
|
+
for (const ancestry of ancestries) {
|
136
137
|
if (ancestry[i] !== shouldMatch) {
|
137
138
|
break depthLoop;
|
138
139
|
}
|
@@ -154,8 +155,8 @@ function getDeepestCommonAncestorFrom(paths, filter) {
|
|
154
155
|
}
|
155
156
|
|
156
157
|
function getAncestry() {
|
157
|
-
|
158
|
-
|
158
|
+
let path = this;
|
159
|
+
const paths = [];
|
159
160
|
|
160
161
|
do {
|
161
162
|
paths.push(path);
|
@@ -169,19 +170,14 @@ function isAncestor(maybeDescendant) {
|
|
169
170
|
}
|
170
171
|
|
171
172
|
function isDescendant(maybeAncestor) {
|
172
|
-
return !!this.findParent(
|
173
|
-
return parent === maybeAncestor;
|
174
|
-
});
|
173
|
+
return !!this.findParent(parent => parent === maybeAncestor);
|
175
174
|
}
|
176
175
|
|
177
176
|
function inType() {
|
178
|
-
|
177
|
+
let path = this;
|
179
178
|
|
180
179
|
while (path) {
|
181
|
-
|
182
|
-
|
183
|
-
for (var _i3 = 0; _i3 < _arr3.length; _i3++) {
|
184
|
-
var type = _arr3[_i3];
|
180
|
+
for (const type of arguments) {
|
185
181
|
if (path.node.type === type) return true;
|
186
182
|
}
|
187
183
|
|
package/lib/path/comments.js
CHANGED
@@ -1,25 +1,35 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.shareCommentsWithSiblings = shareCommentsWithSiblings;
|
5
7
|
exports.addComment = addComment;
|
6
8
|
exports.addComments = addComments;
|
7
9
|
|
8
|
-
|
10
|
+
function t() {
|
11
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
9
12
|
|
10
|
-
|
13
|
+
t = function () {
|
14
|
+
return data;
|
15
|
+
};
|
16
|
+
|
17
|
+
return data;
|
18
|
+
}
|
19
|
+
|
20
|
+
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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
11
21
|
|
12
22
|
function shareCommentsWithSiblings() {
|
13
23
|
if (typeof this.key === "string") return;
|
14
|
-
|
24
|
+
const node = this.node;
|
15
25
|
if (!node) return;
|
16
|
-
|
17
|
-
|
26
|
+
const trailing = node.trailingComments;
|
27
|
+
const leading = node.leadingComments;
|
18
28
|
if (!trailing && !leading) return;
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
29
|
+
const prev = this.getSibling(this.key - 1);
|
30
|
+
const next = this.getSibling(this.key + 1);
|
31
|
+
const hasPrev = Boolean(prev.node);
|
32
|
+
const hasNext = Boolean(next.node);
|
23
33
|
|
24
34
|
if (hasPrev && hasNext) {} else if (hasPrev) {
|
25
35
|
prev.addComments("trailing", trailing);
|
@@ -29,9 +39,9 @@ function shareCommentsWithSiblings() {
|
|
29
39
|
}
|
30
40
|
|
31
41
|
function addComment(type, content, line) {
|
32
|
-
t.addComment(this.node, type, content, line);
|
42
|
+
t().addComment(this.node, type, content, line);
|
33
43
|
}
|
34
44
|
|
35
45
|
function addComments(type, comments) {
|
36
|
-
t.addComments(this.node, type, comments);
|
46
|
+
t().addComments(this.node, type, comments);
|
37
47
|
}
|