@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/scope/index.js
CHANGED
@@ -13,24 +13,63 @@ var _binding = require("./binding");
|
|
13
13
|
|
14
14
|
var _globals = require("globals");
|
15
15
|
|
16
|
-
var
|
16
|
+
var _t = require("@babel/types");
|
17
17
|
|
18
18
|
var _cache = require("../cache");
|
19
19
|
|
20
|
+
const {
|
21
|
+
NOT_LOCAL_BINDING,
|
22
|
+
callExpression,
|
23
|
+
cloneNode,
|
24
|
+
getBindingIdentifiers,
|
25
|
+
identifier,
|
26
|
+
isArrayExpression,
|
27
|
+
isBinary,
|
28
|
+
isClass,
|
29
|
+
isClassBody,
|
30
|
+
isClassDeclaration,
|
31
|
+
isExportAllDeclaration,
|
32
|
+
isExportDefaultDeclaration,
|
33
|
+
isExportNamedDeclaration,
|
34
|
+
isFunctionDeclaration,
|
35
|
+
isIdentifier,
|
36
|
+
isImportDeclaration,
|
37
|
+
isLiteral,
|
38
|
+
isMethod,
|
39
|
+
isModuleDeclaration,
|
40
|
+
isModuleSpecifier,
|
41
|
+
isObjectExpression,
|
42
|
+
isProperty,
|
43
|
+
isPureish,
|
44
|
+
isSuper,
|
45
|
+
isTaggedTemplateExpression,
|
46
|
+
isTemplateLiteral,
|
47
|
+
isThisExpression,
|
48
|
+
isUnaryExpression,
|
49
|
+
isVariableDeclaration,
|
50
|
+
matchesPattern,
|
51
|
+
memberExpression,
|
52
|
+
numericLiteral,
|
53
|
+
toIdentifier,
|
54
|
+
unaryExpression,
|
55
|
+
variableDeclaration,
|
56
|
+
variableDeclarator
|
57
|
+
} = _t;
|
58
|
+
|
20
59
|
function gatherNodeParts(node, parts) {
|
21
60
|
switch (node == null ? void 0 : node.type) {
|
22
61
|
default:
|
23
|
-
if (
|
24
|
-
if ((
|
62
|
+
if (isModuleDeclaration(node)) {
|
63
|
+
if ((isExportAllDeclaration(node) || isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.source) {
|
25
64
|
gatherNodeParts(node.source, parts);
|
26
|
-
} else if ((
|
65
|
+
} else if ((isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.specifiers && node.specifiers.length) {
|
27
66
|
for (const e of node.specifiers) gatherNodeParts(e, parts);
|
28
|
-
} else if ((
|
67
|
+
} else if ((isExportDefaultDeclaration(node) || isExportNamedDeclaration(node)) && node.declaration) {
|
29
68
|
gatherNodeParts(node.declaration, parts);
|
30
69
|
}
|
31
|
-
} else if (
|
70
|
+
} else if (isModuleSpecifier(node)) {
|
32
71
|
gatherNodeParts(node.local, parts);
|
33
|
-
} else if (
|
72
|
+
} else if (isLiteral(node)) {
|
34
73
|
parts.push(node.value);
|
35
74
|
}
|
36
75
|
|
@@ -159,24 +198,31 @@ function gatherNodeParts(node, parts) {
|
|
159
198
|
}
|
160
199
|
|
161
200
|
const collectorVisitor = {
|
162
|
-
|
163
|
-
|
164
|
-
const declar = path.get(key);
|
201
|
+
ForStatement(path) {
|
202
|
+
const declar = path.get("init");
|
165
203
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
}
|
204
|
+
if (declar.isVar()) {
|
205
|
+
const {
|
206
|
+
scope
|
207
|
+
} = path;
|
208
|
+
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
209
|
+
parentScope.registerBinding("var", declar);
|
170
210
|
}
|
171
211
|
},
|
172
212
|
|
173
213
|
Declaration(path) {
|
174
214
|
if (path.isBlockScoped()) return;
|
215
|
+
if (path.isImportDeclaration()) return;
|
175
216
|
if (path.isExportDeclaration()) return;
|
176
217
|
const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
177
218
|
parent.registerDeclaration(path);
|
178
219
|
},
|
179
220
|
|
221
|
+
ImportDeclaration(path) {
|
222
|
+
const parent = path.scope.getBlockParent();
|
223
|
+
parent.registerDeclaration(path);
|
224
|
+
},
|
225
|
+
|
180
226
|
ReferencedIdentifier(path, state) {
|
181
227
|
state.references.push(path);
|
182
228
|
},
|
@@ -186,6 +232,12 @@ const collectorVisitor = {
|
|
186
232
|
|
187
233
|
if (left.isPattern() || left.isIdentifier()) {
|
188
234
|
state.constantViolations.push(path);
|
235
|
+
} else if (left.isVar()) {
|
236
|
+
const {
|
237
|
+
scope
|
238
|
+
} = path;
|
239
|
+
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
240
|
+
parentScope.registerBinding("var", left);
|
189
241
|
}
|
190
242
|
},
|
191
243
|
|
@@ -195,19 +247,19 @@ const collectorVisitor = {
|
|
195
247
|
node,
|
196
248
|
scope
|
197
249
|
} = path;
|
198
|
-
if (
|
250
|
+
if (isExportAllDeclaration(node)) return;
|
199
251
|
const declar = node.declaration;
|
200
252
|
|
201
|
-
if (
|
253
|
+
if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
|
202
254
|
const id = declar.id;
|
203
255
|
if (!id) return;
|
204
256
|
const binding = scope.getBinding(id.name);
|
205
|
-
|
206
|
-
} else if (
|
257
|
+
binding == null ? void 0 : binding.reference(path);
|
258
|
+
} else if (isVariableDeclaration(declar)) {
|
207
259
|
for (const decl of declar.declarations) {
|
208
|
-
for (const name of Object.keys(
|
260
|
+
for (const name of Object.keys(getBindingIdentifiers(decl))) {
|
209
261
|
const binding = scope.getBinding(name);
|
210
|
-
|
262
|
+
binding == null ? void 0 : binding.reference(path);
|
211
263
|
}
|
212
264
|
}
|
213
265
|
}
|
@@ -251,7 +303,7 @@ const collectorVisitor = {
|
|
251
303
|
},
|
252
304
|
|
253
305
|
Function(path) {
|
254
|
-
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[
|
306
|
+
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
255
307
|
path.scope.registerBinding("local", path.get("id"), path);
|
256
308
|
}
|
257
309
|
|
@@ -263,7 +315,7 @@ const collectorVisitor = {
|
|
263
315
|
},
|
264
316
|
|
265
317
|
ClassExpression(path) {
|
266
|
-
if (path.has("id") && !path.get("id").node[
|
318
|
+
if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
267
319
|
path.scope.registerBinding("local", path);
|
268
320
|
}
|
269
321
|
}
|
@@ -336,15 +388,15 @@ class Scope {
|
|
336
388
|
this.push({
|
337
389
|
id
|
338
390
|
});
|
339
|
-
return
|
391
|
+
return cloneNode(id);
|
340
392
|
}
|
341
393
|
|
342
394
|
generateUidIdentifier(name) {
|
343
|
-
return
|
395
|
+
return identifier(this.generateUid(name));
|
344
396
|
}
|
345
397
|
|
346
398
|
generateUid(name = "temp") {
|
347
|
-
name =
|
399
|
+
name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
348
400
|
let uid;
|
349
401
|
let i = 1;
|
350
402
|
|
@@ -374,15 +426,15 @@ class Scope {
|
|
374
426
|
}
|
375
427
|
|
376
428
|
generateUidIdentifierBasedOnNode(node, defaultName) {
|
377
|
-
return
|
429
|
+
return identifier(this.generateUidBasedOnNode(node, defaultName));
|
378
430
|
}
|
379
431
|
|
380
432
|
isStatic(node) {
|
381
|
-
if (
|
433
|
+
if (isThisExpression(node) || isSuper(node)) {
|
382
434
|
return true;
|
383
435
|
}
|
384
436
|
|
385
|
-
if (
|
437
|
+
if (isIdentifier(node)) {
|
386
438
|
const binding = this.getBinding(node.name);
|
387
439
|
|
388
440
|
if (binding) {
|
@@ -405,7 +457,7 @@ class Scope {
|
|
405
457
|
this.push({
|
406
458
|
id
|
407
459
|
});
|
408
|
-
return
|
460
|
+
return cloneNode(id);
|
409
461
|
}
|
410
462
|
|
411
463
|
return id;
|
@@ -461,7 +513,7 @@ class Scope {
|
|
461
513
|
}
|
462
514
|
|
463
515
|
toArray(node, i, arrayLikeIsIterable) {
|
464
|
-
if (
|
516
|
+
if (isIdentifier(node)) {
|
465
517
|
const binding = this.getBinding(node.name);
|
466
518
|
|
467
519
|
if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
|
@@ -469,14 +521,14 @@ class Scope {
|
|
469
521
|
}
|
470
522
|
}
|
471
523
|
|
472
|
-
if (
|
524
|
+
if (isArrayExpression(node)) {
|
473
525
|
return node;
|
474
526
|
}
|
475
527
|
|
476
|
-
if (
|
528
|
+
if (isIdentifier(node, {
|
477
529
|
name: "arguments"
|
478
530
|
})) {
|
479
|
-
return
|
531
|
+
return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
|
480
532
|
}
|
481
533
|
|
482
534
|
let helperName;
|
@@ -485,7 +537,7 @@ class Scope {
|
|
485
537
|
if (i === true) {
|
486
538
|
helperName = "toConsumableArray";
|
487
539
|
} else if (i) {
|
488
|
-
args.push(
|
540
|
+
args.push(numericLiteral(i));
|
489
541
|
helperName = "slicedToArray";
|
490
542
|
} else {
|
491
543
|
helperName = "toArray";
|
@@ -496,7 +548,7 @@ class Scope {
|
|
496
548
|
helperName = "maybeArrayLike";
|
497
549
|
}
|
498
550
|
|
499
|
-
return
|
551
|
+
return callExpression(this.hub.addHelper(helperName), args);
|
500
552
|
}
|
501
553
|
|
502
554
|
hasLabel(name) {
|
@@ -542,7 +594,7 @@ class Scope {
|
|
542
594
|
}
|
543
595
|
|
544
596
|
buildUndefinedNode() {
|
545
|
-
return
|
597
|
+
return unaryExpression("void", numericLiteral(0), true);
|
546
598
|
}
|
547
599
|
|
548
600
|
registerConstantViolation(path) {
|
@@ -624,56 +676,56 @@ class Scope {
|
|
624
676
|
}
|
625
677
|
|
626
678
|
isPure(node, constantsOnly) {
|
627
|
-
if (
|
679
|
+
if (isIdentifier(node)) {
|
628
680
|
const binding = this.getBinding(node.name);
|
629
681
|
if (!binding) return false;
|
630
682
|
if (constantsOnly) return binding.constant;
|
631
683
|
return true;
|
632
|
-
} else if (
|
684
|
+
} else if (isClass(node)) {
|
633
685
|
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
|
634
686
|
return false;
|
635
687
|
}
|
636
688
|
|
637
689
|
return this.isPure(node.body, constantsOnly);
|
638
|
-
} else if (
|
690
|
+
} else if (isClassBody(node)) {
|
639
691
|
for (const method of node.body) {
|
640
692
|
if (!this.isPure(method, constantsOnly)) return false;
|
641
693
|
}
|
642
694
|
|
643
695
|
return true;
|
644
|
-
} else if (
|
696
|
+
} else if (isBinary(node)) {
|
645
697
|
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
646
|
-
} else if (
|
698
|
+
} else if (isArrayExpression(node)) {
|
647
699
|
for (const elem of node.elements) {
|
648
700
|
if (!this.isPure(elem, constantsOnly)) return false;
|
649
701
|
}
|
650
702
|
|
651
703
|
return true;
|
652
|
-
} else if (
|
704
|
+
} else if (isObjectExpression(node)) {
|
653
705
|
for (const prop of node.properties) {
|
654
706
|
if (!this.isPure(prop, constantsOnly)) return false;
|
655
707
|
}
|
656
708
|
|
657
709
|
return true;
|
658
|
-
} else if (
|
710
|
+
} else if (isMethod(node)) {
|
659
711
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
660
712
|
if (node.kind === "get" || node.kind === "set") return false;
|
661
713
|
return true;
|
662
|
-
} else if (
|
714
|
+
} else if (isProperty(node)) {
|
663
715
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
664
716
|
return this.isPure(node.value, constantsOnly);
|
665
|
-
} else if (
|
717
|
+
} else if (isUnaryExpression(node)) {
|
666
718
|
return this.isPure(node.argument, constantsOnly);
|
667
|
-
} else if (
|
668
|
-
return
|
669
|
-
} else if (
|
719
|
+
} else if (isTaggedTemplateExpression(node)) {
|
720
|
+
return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
|
721
|
+
} else if (isTemplateLiteral(node)) {
|
670
722
|
for (const expression of node.expressions) {
|
671
723
|
if (!this.isPure(expression, constantsOnly)) return false;
|
672
724
|
}
|
673
725
|
|
674
726
|
return true;
|
675
727
|
} else {
|
676
|
-
return
|
728
|
+
return isPureish(node);
|
677
729
|
}
|
678
730
|
}
|
679
731
|
|
@@ -788,13 +840,13 @@ class Scope {
|
|
788
840
|
let declarPath = !unique && path.getData(dataKey);
|
789
841
|
|
790
842
|
if (!declarPath) {
|
791
|
-
const declar =
|
843
|
+
const declar = variableDeclaration(kind, []);
|
792
844
|
declar._blockHoist = blockHoist;
|
793
845
|
[declarPath] = path.unshiftContainer("body", [declar]);
|
794
846
|
if (!unique) path.setData(dataKey, declarPath);
|
795
847
|
}
|
796
848
|
|
797
|
-
const declarator =
|
849
|
+
const declarator = variableDeclarator(opts.id, opts.init);
|
798
850
|
declarPath.node.declarations.push(declarator);
|
799
851
|
this.registerBinding(kind, declarPath.get("declarations").pop());
|
800
852
|
}
|
package/lib/scope/lib/renamer.js
CHANGED
@@ -9,8 +9,16 @@ var _binding = require("../binding");
|
|
9
9
|
|
10
10
|
var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
|
11
11
|
|
12
|
-
var
|
13
|
-
|
12
|
+
var _t = require("@babel/types");
|
13
|
+
|
14
|
+
const {
|
15
|
+
VISITOR_KEYS,
|
16
|
+
assignmentExpression,
|
17
|
+
identifier,
|
18
|
+
toExpression,
|
19
|
+
variableDeclaration,
|
20
|
+
variableDeclarator
|
21
|
+
} = _t;
|
14
22
|
const renameVisitor = {
|
15
23
|
ReferencedIdentifier({
|
16
24
|
node
|
@@ -62,20 +70,20 @@ class Renamer {
|
|
62
70
|
return;
|
63
71
|
if (!path.isFunctionDeclaration() && !path.isClassDeclaration()) return;
|
64
72
|
if (this.binding.kind !== "hoisted") return;
|
65
|
-
path.node.id =
|
73
|
+
path.node.id = identifier(this.oldName);
|
66
74
|
path.node._blockHoist = 3;
|
67
|
-
path.replaceWith(
|
75
|
+
path.replaceWith(variableDeclaration("let", [variableDeclarator(identifier(this.newName), toExpression(path.node))]));
|
68
76
|
}
|
69
77
|
|
70
78
|
maybeConvertFromClassFunctionExpression(path) {
|
71
79
|
return;
|
72
80
|
if (!path.isFunctionExpression() && !path.isClassExpression()) return;
|
73
81
|
if (this.binding.kind !== "local") return;
|
74
|
-
path.node.id =
|
82
|
+
path.node.id = identifier(this.oldName);
|
75
83
|
this.binding.scope.parent.push({
|
76
|
-
id:
|
84
|
+
id: identifier(this.newName)
|
77
85
|
});
|
78
|
-
path.replaceWith(
|
86
|
+
path.replaceWith(assignmentExpression("=", identifier(this.newName), path.node));
|
79
87
|
}
|
80
88
|
|
81
89
|
rename(block) {
|
@@ -130,7 +138,7 @@ function skipAllButComputedMethodKey(path) {
|
|
130
138
|
return;
|
131
139
|
}
|
132
140
|
|
133
|
-
const keys =
|
141
|
+
const keys = VISITOR_KEYS[path.type];
|
134
142
|
|
135
143
|
for (const key of keys) {
|
136
144
|
if (key !== "key") path.skipKey(key);
|
package/lib/types.js
CHANGED
package/lib/visitors.js
CHANGED
@@ -9,7 +9,13 @@ exports.merge = merge;
|
|
9
9
|
|
10
10
|
var virtualTypes = require("./path/lib/virtual-types");
|
11
11
|
|
12
|
-
var
|
12
|
+
var _t = require("@babel/types");
|
13
|
+
|
14
|
+
const {
|
15
|
+
DEPRECATED_KEYS,
|
16
|
+
FLIPPED_ALIAS_KEYS,
|
17
|
+
TYPES
|
18
|
+
} = _t;
|
13
19
|
|
14
20
|
function explode(visitor) {
|
15
21
|
if (visitor._exploded) return visitor;
|
@@ -60,8 +66,8 @@ function explode(visitor) {
|
|
60
66
|
for (const nodeType of Object.keys(visitor)) {
|
61
67
|
if (shouldIgnoreKey(nodeType)) continue;
|
62
68
|
const fns = visitor[nodeType];
|
63
|
-
let aliases =
|
64
|
-
const deprecatedKey =
|
69
|
+
let aliases = FLIPPED_ALIAS_KEYS[nodeType];
|
70
|
+
const deprecatedKey = DEPRECATED_KEYS[nodeType];
|
65
71
|
|
66
72
|
if (deprecatedKey) {
|
67
73
|
console.trace(`Visitor defined for ${nodeType} but it has been renamed to ${deprecatedKey}`);
|
@@ -104,7 +110,7 @@ function verify(visitor) {
|
|
104
110
|
|
105
111
|
if (shouldIgnoreKey(nodeType)) continue;
|
106
112
|
|
107
|
-
if (
|
113
|
+
if (TYPES.indexOf(nodeType) < 0) {
|
108
114
|
throw new Error(`You gave us a visitor for the node type ${nodeType} but it's not a valid type`);
|
109
115
|
}
|
110
116
|
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@babel/traverse",
|
3
|
-
"version": "7.
|
3
|
+
"version": "7.15.4",
|
4
4
|
"description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes",
|
5
5
|
"author": "The Babel Team (https://babel.dev/team)",
|
6
6
|
"homepage": "https://babel.dev/docs/en/next/babel-traverse",
|
@@ -17,12 +17,12 @@
|
|
17
17
|
"main": "./lib/index.js",
|
18
18
|
"dependencies": {
|
19
19
|
"@babel/code-frame": "^7.14.5",
|
20
|
-
"@babel/generator": "^7.
|
21
|
-
"@babel/helper-function-name": "^7.
|
22
|
-
"@babel/helper-hoist-variables": "^7.
|
23
|
-
"@babel/helper-split-export-declaration": "^7.
|
24
|
-
"@babel/parser": "^7.
|
25
|
-
"@babel/types": "^7.
|
20
|
+
"@babel/generator": "^7.15.4",
|
21
|
+
"@babel/helper-function-name": "^7.15.4",
|
22
|
+
"@babel/helper-hoist-variables": "^7.15.4",
|
23
|
+
"@babel/helper-split-export-declaration": "^7.15.4",
|
24
|
+
"@babel/parser": "^7.15.4",
|
25
|
+
"@babel/types": "^7.15.4",
|
26
26
|
"debug": "^4.1.0",
|
27
27
|
"globals": "^11.1.0"
|
28
28
|
},
|
@@ -9,6 +9,7 @@ export default function generateValidators() {
|
|
9
9
|
*/
|
10
10
|
import * as t from "@babel/types";
|
11
11
|
import NodePath from "../index";
|
12
|
+
import type { VirtualTypeAliases } from "./virtual-types";
|
12
13
|
|
13
14
|
export interface NodePathValidators {
|
14
15
|
`;
|
@@ -18,10 +19,18 @@ export interface NodePathValidators {
|
|
18
19
|
}
|
19
20
|
|
20
21
|
for (const type of Object.keys(virtualTypes)) {
|
22
|
+
const { types } = virtualTypes[type];
|
21
23
|
if (type[0] === "_") continue;
|
22
24
|
if (definitions.NODE_FIELDS[type] || definitions.FLIPPED_ALIAS_KEYS[type]) {
|
23
25
|
output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
|
26
|
+
} else if (types /* in VirtualTypeAliases */) {
|
27
|
+
output += `is${type}(opts?: object): this is NodePath<VirtualTypeAliases["${type}"]>;`;
|
24
28
|
} else {
|
29
|
+
// if it don't have types, then VirtualTypeAliases[type] is t.Node
|
30
|
+
// which TS marked as always true
|
31
|
+
// eg. if (path.isBlockScope()) return;
|
32
|
+
// path resolved to `never` here
|
33
|
+
// so we have to return boolean instead of this is NodePath<t.Node> here
|
25
34
|
output += `is${type}(opts?: object): boolean;`;
|
26
35
|
}
|
27
36
|
}
|