@babel/traverse 7.14.7 → 7.15.4
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/lib/context.js +6 -2
- package/lib/index.js +12 -6
- package/lib/path/ancestry.js +6 -2
- package/lib/path/comments.js +8 -3
- package/lib/path/conversion.js +77 -31
- package/lib/path/family.js +59 -29
- package/lib/path/index.js +8 -2
- package/lib/path/inference/index.js +36 -18
- package/lib/path/inference/inferer-reference.js +28 -17
- package/lib/path/inference/inferers.js +69 -45
- package/lib/path/introspection.js +23 -11
- package/lib/path/lib/hoister.js +17 -7
- package/lib/path/lib/virtual-types.js +45 -21
- package/lib/path/modification.js +19 -8
- package/lib/path/replacement.js +47 -24
- package/lib/scope/index.js +103 -51
- package/lib/scope/lib/renamer.js +16 -8
- package/lib/types.js +0 -2
- package/lib/visitors.js +10 -4
- package/package.json +7 -7
- package/scripts/generators/validators.js +9 -0
package/lib/context.js
CHANGED
@@ -7,7 +7,11 @@ exports.default = void 0;
|
|
7
7
|
|
8
8
|
var _path = require("./path");
|
9
9
|
|
10
|
-
var
|
10
|
+
var _t = require("@babel/types");
|
11
|
+
|
12
|
+
const {
|
13
|
+
VISITOR_KEYS
|
14
|
+
} = _t;
|
11
15
|
|
12
16
|
class TraversalContext {
|
13
17
|
constructor(scope, opts, state, parentPath) {
|
@@ -23,7 +27,7 @@ class TraversalContext {
|
|
23
27
|
const opts = this.opts;
|
24
28
|
if (opts.enter || opts.exit) return true;
|
25
29
|
if (opts[node.type]) return true;
|
26
|
-
const keys =
|
30
|
+
const keys = VISITOR_KEYS[node.type];
|
27
31
|
if (!(keys != null && keys.length)) return false;
|
28
32
|
|
29
33
|
for (const key of keys) {
|
package/lib/index.js
CHANGED
@@ -29,7 +29,7 @@ var visitors = require("./visitors");
|
|
29
29
|
|
30
30
|
exports.visitors = visitors;
|
31
31
|
|
32
|
-
var
|
32
|
+
var _t = require("@babel/types");
|
33
33
|
|
34
34
|
var cache = require("./cache");
|
35
35
|
|
@@ -39,6 +39,12 @@ var _scope = require("./scope");
|
|
39
39
|
|
40
40
|
var _hub = require("./hub");
|
41
41
|
|
42
|
+
const {
|
43
|
+
VISITOR_KEYS,
|
44
|
+
removeProperties,
|
45
|
+
traverseFast
|
46
|
+
} = _t;
|
47
|
+
|
42
48
|
function traverse(parent, opts = {}, scope, state, parentPath) {
|
43
49
|
if (!parent) return;
|
44
50
|
|
@@ -48,7 +54,7 @@ function traverse(parent, opts = {}, scope, state, parentPath) {
|
|
48
54
|
}
|
49
55
|
}
|
50
56
|
|
51
|
-
if (!
|
57
|
+
if (!VISITOR_KEYS[parent.type]) {
|
52
58
|
return;
|
53
59
|
}
|
54
60
|
|
@@ -63,11 +69,11 @@ traverse.verify = visitors.verify;
|
|
63
69
|
traverse.explode = visitors.explode;
|
64
70
|
|
65
71
|
traverse.cheap = function (node, enter) {
|
66
|
-
return
|
72
|
+
return traverseFast(node, enter);
|
67
73
|
};
|
68
74
|
|
69
75
|
traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
|
70
|
-
const keys =
|
76
|
+
const keys = VISITOR_KEYS[node.type];
|
71
77
|
if (!keys) return;
|
72
78
|
const context = new _context.default(scope, opts, state, parentPath);
|
73
79
|
|
@@ -78,12 +84,12 @@ traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
|
|
78
84
|
};
|
79
85
|
|
80
86
|
traverse.clearNode = function (node, opts) {
|
81
|
-
|
87
|
+
removeProperties(node, opts);
|
82
88
|
cache.path.delete(node);
|
83
89
|
};
|
84
90
|
|
85
91
|
traverse.removeProperties = function (tree, opts) {
|
86
|
-
|
92
|
+
traverseFast(tree, traverse.clearNode, opts);
|
87
93
|
return tree;
|
88
94
|
};
|
89
95
|
|
package/lib/path/ancestry.js
CHANGED
@@ -14,10 +14,14 @@ exports.isAncestor = isAncestor;
|
|
14
14
|
exports.isDescendant = isDescendant;
|
15
15
|
exports.inType = inType;
|
16
16
|
|
17
|
-
var
|
17
|
+
var _t = require("@babel/types");
|
18
18
|
|
19
19
|
var _index = require("./index");
|
20
20
|
|
21
|
+
const {
|
22
|
+
VISITOR_KEYS
|
23
|
+
} = _t;
|
24
|
+
|
21
25
|
function findParent(callback) {
|
22
26
|
let path = this;
|
23
27
|
|
@@ -63,7 +67,7 @@ function getStatementParent() {
|
|
63
67
|
function getEarliestCommonAncestorFrom(paths) {
|
64
68
|
return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
|
65
69
|
let earliest;
|
66
|
-
const keys =
|
70
|
+
const keys = VISITOR_KEYS[deepest.type];
|
67
71
|
|
68
72
|
for (const ancestry of ancestries) {
|
69
73
|
const path = ancestry[i + 1];
|
package/lib/path/comments.js
CHANGED
@@ -7,7 +7,12 @@ exports.shareCommentsWithSiblings = shareCommentsWithSiblings;
|
|
7
7
|
exports.addComment = addComment;
|
8
8
|
exports.addComments = addComments;
|
9
9
|
|
10
|
-
var
|
10
|
+
var _t = require("@babel/types");
|
11
|
+
|
12
|
+
const {
|
13
|
+
addComment: _addComment,
|
14
|
+
addComments: _addComments
|
15
|
+
} = _t;
|
11
16
|
|
12
17
|
function shareCommentsWithSiblings() {
|
13
18
|
if (typeof this.key === "string") return;
|
@@ -29,9 +34,9 @@ function shareCommentsWithSiblings() {
|
|
29
34
|
}
|
30
35
|
|
31
36
|
function addComment(type, content, line) {
|
32
|
-
|
37
|
+
_addComment(this.node, type, content, line);
|
33
38
|
}
|
34
39
|
|
35
40
|
function addComments(type, comments) {
|
36
|
-
|
41
|
+
_addComments(this.node, type, comments);
|
37
42
|
}
|
package/lib/path/conversion.js
CHANGED
@@ -9,10 +9,35 @@ exports.arrowFunctionToShadowed = arrowFunctionToShadowed;
|
|
9
9
|
exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
|
10
10
|
exports.arrowFunctionToExpression = arrowFunctionToExpression;
|
11
11
|
|
12
|
-
var
|
12
|
+
var _t = require("@babel/types");
|
13
13
|
|
14
14
|
var _helperFunctionName = require("@babel/helper-function-name");
|
15
15
|
|
16
|
+
const {
|
17
|
+
arrowFunctionExpression,
|
18
|
+
assignmentExpression,
|
19
|
+
binaryExpression,
|
20
|
+
blockStatement,
|
21
|
+
callExpression,
|
22
|
+
conditionalExpression,
|
23
|
+
expressionStatement,
|
24
|
+
identifier,
|
25
|
+
isIdentifier,
|
26
|
+
jsxIdentifier,
|
27
|
+
memberExpression,
|
28
|
+
metaProperty,
|
29
|
+
numericLiteral,
|
30
|
+
objectExpression,
|
31
|
+
restElement,
|
32
|
+
returnStatement,
|
33
|
+
sequenceExpression,
|
34
|
+
spreadElement,
|
35
|
+
stringLiteral,
|
36
|
+
super: _super,
|
37
|
+
thisExpression,
|
38
|
+
unaryExpression
|
39
|
+
} = _t;
|
40
|
+
|
16
41
|
function toComputedKey() {
|
17
42
|
let key;
|
18
43
|
|
@@ -25,7 +50,7 @@ function toComputedKey() {
|
|
25
50
|
}
|
26
51
|
|
27
52
|
if (!this.node.computed) {
|
28
|
-
if (
|
53
|
+
if (isIdentifier(key)) key = stringLiteral(key.name);
|
29
54
|
}
|
30
55
|
|
31
56
|
return key;
|
@@ -61,14 +86,14 @@ function ensureBlock() {
|
|
61
86
|
|
62
87
|
if (this.isFunction()) {
|
63
88
|
key = "argument";
|
64
|
-
statements.push(
|
89
|
+
statements.push(returnStatement(body.node));
|
65
90
|
} else {
|
66
91
|
key = "expression";
|
67
|
-
statements.push(
|
92
|
+
statements.push(expressionStatement(body.node));
|
68
93
|
}
|
69
94
|
}
|
70
95
|
|
71
|
-
this.node.body =
|
96
|
+
this.node.body = blockStatement(statements);
|
72
97
|
const parentPath = this.get(stringPath);
|
73
98
|
body.setup(parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key);
|
74
99
|
return this.node;
|
@@ -106,12 +131,12 @@ function arrowFunctionToExpression({
|
|
106
131
|
if (checkBinding) {
|
107
132
|
this.parentPath.scope.push({
|
108
133
|
id: checkBinding,
|
109
|
-
init:
|
134
|
+
init: objectExpression([])
|
110
135
|
});
|
111
136
|
}
|
112
137
|
|
113
|
-
this.get("body").unshiftContainer("body",
|
114
|
-
this.replaceWith(
|
138
|
+
this.get("body").unshiftContainer("body", expressionStatement(callExpression(this.hub.addHelper("newArrowCheck"), [thisExpression(), checkBinding ? identifier(checkBinding.name) : identifier(thisBinding)])));
|
139
|
+
this.replaceWith(callExpression(memberExpression((0, _helperFunctionName.default)(this, true) || this.node, identifier("bind")), [checkBinding ? identifier(checkBinding.name) : thisExpression()]));
|
115
140
|
}
|
116
141
|
}
|
117
142
|
|
@@ -159,25 +184,33 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
|
|
159
184
|
});
|
160
185
|
const superBinding = getSuperBinding(thisEnvFn);
|
161
186
|
allSuperCalls.forEach(superCall => {
|
162
|
-
const callee =
|
187
|
+
const callee = identifier(superBinding);
|
163
188
|
callee.loc = superCall.node.callee.loc;
|
164
189
|
superCall.get("callee").replaceWith(callee);
|
165
190
|
});
|
166
191
|
}
|
167
192
|
|
168
193
|
if (argumentsPaths.length > 0) {
|
169
|
-
const argumentsBinding = getBinding(thisEnvFn, "arguments", () =>
|
194
|
+
const argumentsBinding = getBinding(thisEnvFn, "arguments", () => {
|
195
|
+
const args = () => identifier("arguments");
|
196
|
+
|
197
|
+
if (thisEnvFn.scope.path.isProgram()) {
|
198
|
+
return conditionalExpression(binaryExpression("===", unaryExpression("typeof", args()), stringLiteral("undefined")), thisEnvFn.scope.buildUndefinedNode(), args());
|
199
|
+
} else {
|
200
|
+
return args();
|
201
|
+
}
|
202
|
+
});
|
170
203
|
argumentsPaths.forEach(argumentsChild => {
|
171
|
-
const argsRef =
|
204
|
+
const argsRef = identifier(argumentsBinding);
|
172
205
|
argsRef.loc = argumentsChild.node.loc;
|
173
206
|
argumentsChild.replaceWith(argsRef);
|
174
207
|
});
|
175
208
|
}
|
176
209
|
|
177
210
|
if (newTargetPaths.length > 0) {
|
178
|
-
const newTargetBinding = getBinding(thisEnvFn, "newtarget", () =>
|
211
|
+
const newTargetBinding = getBinding(thisEnvFn, "newtarget", () => metaProperty(identifier("new"), identifier("target")));
|
179
212
|
newTargetPaths.forEach(targetChild => {
|
180
|
-
const targetRef =
|
213
|
+
const targetRef = identifier(newTargetBinding);
|
181
214
|
targetRef.loc = targetChild.node.loc;
|
182
215
|
targetChild.replaceWith(targetRef);
|
183
216
|
});
|
@@ -209,11 +242,11 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
|
|
209
242
|
args.push(value);
|
210
243
|
}
|
211
244
|
|
212
|
-
const call =
|
245
|
+
const call = callExpression(identifier(superBinding), args);
|
213
246
|
|
214
247
|
if (isCall) {
|
215
|
-
superProp.parentPath.unshiftContainer("arguments",
|
216
|
-
superProp.replaceWith(
|
248
|
+
superProp.parentPath.unshiftContainer("arguments", thisExpression());
|
249
|
+
superProp.replaceWith(memberExpression(call, identifier("call")));
|
217
250
|
thisPaths.push(superProp.parentPath.get("arguments.0"));
|
218
251
|
} else if (isAssignment) {
|
219
252
|
superProp.parentPath.replaceWith(call);
|
@@ -230,7 +263,7 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
|
|
230
263
|
|
231
264
|
if (noNewArrows || inConstructor && hasSuperClass(thisEnvFn)) {
|
232
265
|
thisPaths.forEach(thisChild => {
|
233
|
-
const thisRef = thisChild.isJSX() ?
|
266
|
+
const thisRef = thisChild.isJSX() ? jsxIdentifier(thisBinding) : identifier(thisBinding);
|
234
267
|
thisRef.loc = thisChild.node.loc;
|
235
268
|
thisChild.replaceWith(thisRef);
|
236
269
|
});
|
@@ -250,11 +283,11 @@ function standardizeSuperProperty(superProp) {
|
|
250
283
|
|
251
284
|
if (superProp.node.computed) {
|
252
285
|
const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
|
253
|
-
assignmentPath.get("left").replaceWith(
|
254
|
-
assignmentPath.get("right").replaceWith(
|
286
|
+
assignmentPath.get("left").replaceWith(memberExpression(superProp.node.object, assignmentExpression("=", tmp, superProp.node.property), true));
|
287
|
+
assignmentPath.get("right").replaceWith(binaryExpression(op, memberExpression(superProp.node.object, identifier(tmp.name), true), value));
|
255
288
|
} else {
|
256
|
-
assignmentPath.get("left").replaceWith(
|
257
|
-
assignmentPath.get("right").replaceWith(
|
289
|
+
assignmentPath.get("left").replaceWith(memberExpression(superProp.node.object, superProp.node.property));
|
290
|
+
assignmentPath.get("right").replaceWith(binaryExpression(op, memberExpression(superProp.node.object, identifier(superProp.node.property.name)), value));
|
258
291
|
}
|
259
292
|
|
260
293
|
return [assignmentPath.get("left"), assignmentPath.get("right").get("left")];
|
@@ -262,13 +295,13 @@ function standardizeSuperProperty(superProp) {
|
|
262
295
|
const updateExpr = superProp.parentPath;
|
263
296
|
const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
|
264
297
|
const computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null;
|
265
|
-
const parts = [
|
298
|
+
const parts = [assignmentExpression("=", tmp, memberExpression(superProp.node.object, computedKey ? assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), assignmentExpression("=", memberExpression(superProp.node.object, computedKey ? identifier(computedKey.name) : superProp.node.property, superProp.node.computed), binaryExpression("+", identifier(tmp.name), numericLiteral(1)))];
|
266
299
|
|
267
300
|
if (!superProp.parentPath.node.prefix) {
|
268
|
-
parts.push(
|
301
|
+
parts.push(identifier(tmp.name));
|
269
302
|
}
|
270
303
|
|
271
|
-
updateExpr.replaceWith(
|
304
|
+
updateExpr.replaceWith(sequenceExpression(parts));
|
272
305
|
const left = updateExpr.get("expressions.0.right");
|
273
306
|
const right = updateExpr.get("expressions.1.left");
|
274
307
|
return [left, right];
|
@@ -283,7 +316,7 @@ function hasSuperClass(thisEnvFn) {
|
|
283
316
|
|
284
317
|
function getThisBinding(thisEnvFn, inConstructor) {
|
285
318
|
return getBinding(thisEnvFn, "this", thisBinding => {
|
286
|
-
if (!inConstructor || !hasSuperClass(thisEnvFn)) return
|
319
|
+
if (!inConstructor || !hasSuperClass(thisEnvFn)) return thisExpression();
|
287
320
|
const supers = new WeakSet();
|
288
321
|
thisEnvFn.traverse({
|
289
322
|
Function(child) {
|
@@ -299,7 +332,7 @@ function getThisBinding(thisEnvFn, inConstructor) {
|
|
299
332
|
if (!child.get("callee").isSuper()) return;
|
300
333
|
if (supers.has(child.node)) return;
|
301
334
|
supers.add(child.node);
|
302
|
-
child.replaceWithMultiple([child.node,
|
335
|
+
child.replaceWithMultiple([child.node, assignmentExpression("=", identifier(thisBinding), identifier("this"))]);
|
303
336
|
}
|
304
337
|
|
305
338
|
});
|
@@ -309,7 +342,7 @@ function getThisBinding(thisEnvFn, inConstructor) {
|
|
309
342
|
function getSuperBinding(thisEnvFn) {
|
310
343
|
return getBinding(thisEnvFn, "supercall", () => {
|
311
344
|
const argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
|
312
|
-
return
|
345
|
+
return arrowFunctionExpression([restElement(argsBinding)], callExpression(_super(), [spreadElement(identifier(argsBinding.name))]));
|
313
346
|
});
|
314
347
|
}
|
315
348
|
|
@@ -320,20 +353,20 @@ function getSuperPropBinding(thisEnvFn, isAssignment, propName) {
|
|
320
353
|
let fnBody;
|
321
354
|
|
322
355
|
if (propName) {
|
323
|
-
fnBody =
|
356
|
+
fnBody = memberExpression(_super(), identifier(propName));
|
324
357
|
} else {
|
325
358
|
const method = thisEnvFn.scope.generateUidIdentifier("prop");
|
326
359
|
argsList.unshift(method);
|
327
|
-
fnBody =
|
360
|
+
fnBody = memberExpression(_super(), identifier(method.name), true);
|
328
361
|
}
|
329
362
|
|
330
363
|
if (isAssignment) {
|
331
364
|
const valueIdent = thisEnvFn.scope.generateUidIdentifier("value");
|
332
365
|
argsList.push(valueIdent);
|
333
|
-
fnBody =
|
366
|
+
fnBody = assignmentExpression("=", fnBody, identifier(valueIdent.name));
|
334
367
|
}
|
335
368
|
|
336
|
-
return
|
369
|
+
return arrowFunctionExpression(argsList, fnBody);
|
337
370
|
});
|
338
371
|
}
|
339
372
|
|
@@ -398,6 +431,19 @@ function getScopeInformation(fnPath) {
|
|
398
431
|
|
399
432
|
ReferencedIdentifier(child) {
|
400
433
|
if (child.node.name !== "arguments") return;
|
434
|
+
let curr = child.scope;
|
435
|
+
|
436
|
+
do {
|
437
|
+
if (curr.hasOwnBinding("arguments")) {
|
438
|
+
curr.rename("arguments");
|
439
|
+
return;
|
440
|
+
}
|
441
|
+
|
442
|
+
if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
|
443
|
+
break;
|
444
|
+
}
|
445
|
+
} while (curr = curr.parent);
|
446
|
+
|
401
447
|
argumentsPaths.push(child);
|
402
448
|
},
|
403
449
|
|
package/lib/path/family.js
CHANGED
@@ -20,8 +20,15 @@ exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
|
|
20
20
|
|
21
21
|
var _index = require("./index");
|
22
22
|
|
23
|
-
var
|
24
|
-
|
23
|
+
var _t = require("@babel/types");
|
24
|
+
|
25
|
+
const {
|
26
|
+
getBindingIdentifiers: _getBindingIdentifiers,
|
27
|
+
getOuterBindingIdentifiers: _getOuterBindingIdentifiers,
|
28
|
+
isDeclaration,
|
29
|
+
numericLiteral,
|
30
|
+
unaryExpression
|
31
|
+
} = _t;
|
25
32
|
const NORMAL_COMPLETION = 0;
|
26
33
|
const BREAK_COMPLETION = 1;
|
27
34
|
|
@@ -50,7 +57,10 @@ function getOpposite() {
|
|
50
57
|
}
|
51
58
|
|
52
59
|
function addCompletionRecords(path, records, context) {
|
53
|
-
if (path)
|
60
|
+
if (path) {
|
61
|
+
records.push(..._getCompletionRecords(path, context));
|
62
|
+
}
|
63
|
+
|
54
64
|
return records;
|
55
65
|
}
|
56
66
|
|
@@ -79,10 +89,10 @@ function completionRecordForSwitch(cases, records, context) {
|
|
79
89
|
lastNormalCompletions = normalCompletions;
|
80
90
|
}
|
81
91
|
|
82
|
-
records
|
92
|
+
records.push(...breakCompletions);
|
83
93
|
}
|
84
94
|
|
85
|
-
records
|
95
|
+
records.push(...lastNormalCompletions);
|
86
96
|
return records;
|
87
97
|
}
|
88
98
|
|
@@ -98,7 +108,7 @@ function replaceBreakStatementInBreakCompletion(completions, reachable) {
|
|
98
108
|
label: null
|
99
109
|
})) {
|
100
110
|
if (reachable) {
|
101
|
-
c.path.replaceWith(
|
111
|
+
c.path.replaceWith(unaryExpression("void", numericLiteral(0)));
|
102
112
|
} else {
|
103
113
|
c.path.remove();
|
104
114
|
}
|
@@ -107,7 +117,7 @@ function replaceBreakStatementInBreakCompletion(completions, reachable) {
|
|
107
117
|
}
|
108
118
|
|
109
119
|
function getStatementListCompletion(paths, context) {
|
110
|
-
|
120
|
+
const completions = [];
|
111
121
|
|
112
122
|
if (context.canHaveBreak) {
|
113
123
|
let lastNormalCompletions = [];
|
@@ -119,8 +129,8 @@ function getStatementListCompletion(paths, context) {
|
|
119
129
|
});
|
120
130
|
|
121
131
|
if (path.isBlockStatement() && (context.inCaseClause || context.shouldPopulateBreak)) {
|
122
|
-
|
123
|
-
|
132
|
+
newContext.shouldPopulateBreak = true;
|
133
|
+
} else {
|
124
134
|
newContext.shouldPopulateBreak = false;
|
125
135
|
}
|
126
136
|
|
@@ -131,16 +141,16 @@ function getStatementListCompletion(paths, context) {
|
|
131
141
|
label: null
|
132
142
|
}))) {
|
133
143
|
normalCompletionToBreak(lastNormalCompletions);
|
134
|
-
completions
|
144
|
+
completions.push(...lastNormalCompletions);
|
135
145
|
|
136
146
|
if (lastNormalCompletions.some(c => c.path.isDeclaration())) {
|
137
|
-
completions
|
147
|
+
completions.push(...statementCompletions);
|
138
148
|
replaceBreakStatementInBreakCompletion(statementCompletions, true);
|
139
149
|
}
|
140
150
|
|
141
151
|
replaceBreakStatementInBreakCompletion(statementCompletions, false);
|
142
152
|
} else {
|
143
|
-
completions
|
153
|
+
completions.push(...statementCompletions);
|
144
154
|
|
145
155
|
if (!context.shouldPopulateBreak) {
|
146
156
|
replaceBreakStatementInBreakCompletion(statementCompletions, true);
|
@@ -151,14 +161,32 @@ function getStatementListCompletion(paths, context) {
|
|
151
161
|
}
|
152
162
|
|
153
163
|
if (i === paths.length - 1) {
|
154
|
-
completions
|
164
|
+
completions.push(...statementCompletions);
|
155
165
|
} else {
|
156
|
-
|
157
|
-
|
166
|
+
lastNormalCompletions = [];
|
167
|
+
|
168
|
+
for (let i = 0; i < statementCompletions.length; i++) {
|
169
|
+
const c = statementCompletions[i];
|
170
|
+
|
171
|
+
if (c.type === BREAK_COMPLETION) {
|
172
|
+
completions.push(c);
|
173
|
+
}
|
174
|
+
|
175
|
+
if (c.type === NORMAL_COMPLETION) {
|
176
|
+
lastNormalCompletions.push(c);
|
177
|
+
}
|
178
|
+
}
|
158
179
|
}
|
159
180
|
}
|
160
181
|
} else if (paths.length) {
|
161
|
-
|
182
|
+
for (let i = paths.length - 1; i >= 0; i--) {
|
183
|
+
const pathCompletions = _getCompletionRecords(paths[i], context);
|
184
|
+
|
185
|
+
if (pathCompletions.length > 1 || pathCompletions.length === 1 && !pathCompletions[0].path.isVariableDeclaration()) {
|
186
|
+
completions.push(...pathCompletions);
|
187
|
+
break;
|
188
|
+
}
|
189
|
+
}
|
162
190
|
}
|
163
191
|
|
164
192
|
return completions;
|
@@ -171,24 +199,24 @@ function _getCompletionRecords(path, context) {
|
|
171
199
|
records = addCompletionRecords(path.get("consequent"), records, context);
|
172
200
|
records = addCompletionRecords(path.get("alternate"), records, context);
|
173
201
|
} else if (path.isDoExpression() || path.isFor() || path.isWhile() || path.isLabeledStatement()) {
|
174
|
-
|
202
|
+
return addCompletionRecords(path.get("body"), records, context);
|
175
203
|
} else if (path.isProgram() || path.isBlockStatement()) {
|
176
|
-
|
204
|
+
return getStatementListCompletion(path.get("body"), context);
|
177
205
|
} else if (path.isFunction()) {
|
178
206
|
return _getCompletionRecords(path.get("body"), context);
|
179
207
|
} else if (path.isTryStatement()) {
|
180
208
|
records = addCompletionRecords(path.get("block"), records, context);
|
181
209
|
records = addCompletionRecords(path.get("handler"), records, context);
|
182
210
|
} else if (path.isCatchClause()) {
|
183
|
-
|
211
|
+
return addCompletionRecords(path.get("body"), records, context);
|
184
212
|
} else if (path.isSwitchStatement()) {
|
185
|
-
|
213
|
+
return completionRecordForSwitch(path.get("cases"), records, context);
|
186
214
|
} else if (path.isSwitchCase()) {
|
187
|
-
|
215
|
+
return getStatementListCompletion(path.get("consequent"), {
|
188
216
|
canHaveBreak: true,
|
189
217
|
shouldPopulateBreak: false,
|
190
218
|
inCaseClause: true
|
191
|
-
})
|
219
|
+
});
|
192
220
|
} else if (path.isBreakStatement()) {
|
193
221
|
records.push(BreakCompletion(path));
|
194
222
|
} else {
|
@@ -306,23 +334,23 @@ function _getPattern(parts, context) {
|
|
306
334
|
}
|
307
335
|
|
308
336
|
function getBindingIdentifiers(duplicates) {
|
309
|
-
return
|
337
|
+
return _getBindingIdentifiers(this.node, duplicates);
|
310
338
|
}
|
311
339
|
|
312
340
|
function getOuterBindingIdentifiers(duplicates) {
|
313
|
-
return
|
341
|
+
return _getOuterBindingIdentifiers(this.node, duplicates);
|
314
342
|
}
|
315
343
|
|
316
344
|
function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
317
345
|
const path = this;
|
318
|
-
|
346
|
+
const search = [path];
|
319
347
|
const ids = Object.create(null);
|
320
348
|
|
321
349
|
while (search.length) {
|
322
350
|
const id = search.shift();
|
323
351
|
if (!id) continue;
|
324
352
|
if (!id.node) continue;
|
325
|
-
const keys =
|
353
|
+
const keys = _getBindingIdentifiers.keys[id.node.type];
|
326
354
|
|
327
355
|
if (id.isIdentifier()) {
|
328
356
|
if (duplicates) {
|
@@ -339,7 +367,7 @@ function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
|
339
367
|
if (id.isExportDeclaration()) {
|
340
368
|
const declaration = id.get("declaration");
|
341
369
|
|
342
|
-
if (
|
370
|
+
if (isDeclaration(declaration)) {
|
343
371
|
search.push(declaration);
|
344
372
|
}
|
345
373
|
|
@@ -362,8 +390,10 @@ function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
|
362
390
|
const key = keys[i];
|
363
391
|
const child = id.get(key);
|
364
392
|
|
365
|
-
if (Array.isArray(child)
|
366
|
-
search
|
393
|
+
if (Array.isArray(child)) {
|
394
|
+
search.push(...child);
|
395
|
+
} else if (child.node) {
|
396
|
+
search.push(child);
|
367
397
|
}
|
368
398
|
}
|
369
399
|
}
|
package/lib/path/index.js
CHANGED
@@ -13,7 +13,9 @@ var _index = require("../index");
|
|
13
13
|
|
14
14
|
var _scope = require("../scope");
|
15
15
|
|
16
|
-
var
|
16
|
+
var _t = require("@babel/types");
|
17
|
+
|
18
|
+
var t = _t;
|
17
19
|
|
18
20
|
var _cache = require("../cache");
|
19
21
|
|
@@ -41,6 +43,10 @@ var NodePath_family = require("./family");
|
|
41
43
|
|
42
44
|
var NodePath_comments = require("./comments");
|
43
45
|
|
46
|
+
const {
|
47
|
+
validate
|
48
|
+
} = _t;
|
49
|
+
|
44
50
|
const debug = _debug("babel");
|
45
51
|
|
46
52
|
const REMOVED = 1 << 0;
|
@@ -138,7 +144,7 @@ class NodePath {
|
|
138
144
|
}
|
139
145
|
|
140
146
|
set(key, node) {
|
141
|
-
|
147
|
+
validate(this.node, key, node);
|
142
148
|
this.node[key] = node;
|
143
149
|
}
|
144
150
|
|