@babel/traverse 7.15.0 → 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 +57 -32
- package/lib/path/family.js +52 -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 +97 -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/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,14 +198,15 @@ 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
|
|
@@ -192,6 +232,12 @@ const collectorVisitor = {
|
|
192
232
|
|
193
233
|
if (left.isPattern() || left.isIdentifier()) {
|
194
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);
|
195
241
|
}
|
196
242
|
},
|
197
243
|
|
@@ -201,19 +247,19 @@ const collectorVisitor = {
|
|
201
247
|
node,
|
202
248
|
scope
|
203
249
|
} = path;
|
204
|
-
if (
|
250
|
+
if (isExportAllDeclaration(node)) return;
|
205
251
|
const declar = node.declaration;
|
206
252
|
|
207
|
-
if (
|
253
|
+
if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
|
208
254
|
const id = declar.id;
|
209
255
|
if (!id) return;
|
210
256
|
const binding = scope.getBinding(id.name);
|
211
|
-
|
212
|
-
} else if (
|
257
|
+
binding == null ? void 0 : binding.reference(path);
|
258
|
+
} else if (isVariableDeclaration(declar)) {
|
213
259
|
for (const decl of declar.declarations) {
|
214
|
-
for (const name of Object.keys(
|
260
|
+
for (const name of Object.keys(getBindingIdentifiers(decl))) {
|
215
261
|
const binding = scope.getBinding(name);
|
216
|
-
|
262
|
+
binding == null ? void 0 : binding.reference(path);
|
217
263
|
}
|
218
264
|
}
|
219
265
|
}
|
@@ -257,7 +303,7 @@ const collectorVisitor = {
|
|
257
303
|
},
|
258
304
|
|
259
305
|
Function(path) {
|
260
|
-
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[
|
306
|
+
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
261
307
|
path.scope.registerBinding("local", path.get("id"), path);
|
262
308
|
}
|
263
309
|
|
@@ -269,7 +315,7 @@ const collectorVisitor = {
|
|
269
315
|
},
|
270
316
|
|
271
317
|
ClassExpression(path) {
|
272
|
-
if (path.has("id") && !path.get("id").node[
|
318
|
+
if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
273
319
|
path.scope.registerBinding("local", path);
|
274
320
|
}
|
275
321
|
}
|
@@ -342,15 +388,15 @@ class Scope {
|
|
342
388
|
this.push({
|
343
389
|
id
|
344
390
|
});
|
345
|
-
return
|
391
|
+
return cloneNode(id);
|
346
392
|
}
|
347
393
|
|
348
394
|
generateUidIdentifier(name) {
|
349
|
-
return
|
395
|
+
return identifier(this.generateUid(name));
|
350
396
|
}
|
351
397
|
|
352
398
|
generateUid(name = "temp") {
|
353
|
-
name =
|
399
|
+
name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
354
400
|
let uid;
|
355
401
|
let i = 1;
|
356
402
|
|
@@ -380,15 +426,15 @@ class Scope {
|
|
380
426
|
}
|
381
427
|
|
382
428
|
generateUidIdentifierBasedOnNode(node, defaultName) {
|
383
|
-
return
|
429
|
+
return identifier(this.generateUidBasedOnNode(node, defaultName));
|
384
430
|
}
|
385
431
|
|
386
432
|
isStatic(node) {
|
387
|
-
if (
|
433
|
+
if (isThisExpression(node) || isSuper(node)) {
|
388
434
|
return true;
|
389
435
|
}
|
390
436
|
|
391
|
-
if (
|
437
|
+
if (isIdentifier(node)) {
|
392
438
|
const binding = this.getBinding(node.name);
|
393
439
|
|
394
440
|
if (binding) {
|
@@ -411,7 +457,7 @@ class Scope {
|
|
411
457
|
this.push({
|
412
458
|
id
|
413
459
|
});
|
414
|
-
return
|
460
|
+
return cloneNode(id);
|
415
461
|
}
|
416
462
|
|
417
463
|
return id;
|
@@ -467,7 +513,7 @@ class Scope {
|
|
467
513
|
}
|
468
514
|
|
469
515
|
toArray(node, i, arrayLikeIsIterable) {
|
470
|
-
if (
|
516
|
+
if (isIdentifier(node)) {
|
471
517
|
const binding = this.getBinding(node.name);
|
472
518
|
|
473
519
|
if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
|
@@ -475,14 +521,14 @@ class Scope {
|
|
475
521
|
}
|
476
522
|
}
|
477
523
|
|
478
|
-
if (
|
524
|
+
if (isArrayExpression(node)) {
|
479
525
|
return node;
|
480
526
|
}
|
481
527
|
|
482
|
-
if (
|
528
|
+
if (isIdentifier(node, {
|
483
529
|
name: "arguments"
|
484
530
|
})) {
|
485
|
-
return
|
531
|
+
return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
|
486
532
|
}
|
487
533
|
|
488
534
|
let helperName;
|
@@ -491,7 +537,7 @@ class Scope {
|
|
491
537
|
if (i === true) {
|
492
538
|
helperName = "toConsumableArray";
|
493
539
|
} else if (i) {
|
494
|
-
args.push(
|
540
|
+
args.push(numericLiteral(i));
|
495
541
|
helperName = "slicedToArray";
|
496
542
|
} else {
|
497
543
|
helperName = "toArray";
|
@@ -502,7 +548,7 @@ class Scope {
|
|
502
548
|
helperName = "maybeArrayLike";
|
503
549
|
}
|
504
550
|
|
505
|
-
return
|
551
|
+
return callExpression(this.hub.addHelper(helperName), args);
|
506
552
|
}
|
507
553
|
|
508
554
|
hasLabel(name) {
|
@@ -548,7 +594,7 @@ class Scope {
|
|
548
594
|
}
|
549
595
|
|
550
596
|
buildUndefinedNode() {
|
551
|
-
return
|
597
|
+
return unaryExpression("void", numericLiteral(0), true);
|
552
598
|
}
|
553
599
|
|
554
600
|
registerConstantViolation(path) {
|
@@ -630,56 +676,56 @@ class Scope {
|
|
630
676
|
}
|
631
677
|
|
632
678
|
isPure(node, constantsOnly) {
|
633
|
-
if (
|
679
|
+
if (isIdentifier(node)) {
|
634
680
|
const binding = this.getBinding(node.name);
|
635
681
|
if (!binding) return false;
|
636
682
|
if (constantsOnly) return binding.constant;
|
637
683
|
return true;
|
638
|
-
} else if (
|
684
|
+
} else if (isClass(node)) {
|
639
685
|
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
|
640
686
|
return false;
|
641
687
|
}
|
642
688
|
|
643
689
|
return this.isPure(node.body, constantsOnly);
|
644
|
-
} else if (
|
690
|
+
} else if (isClassBody(node)) {
|
645
691
|
for (const method of node.body) {
|
646
692
|
if (!this.isPure(method, constantsOnly)) return false;
|
647
693
|
}
|
648
694
|
|
649
695
|
return true;
|
650
|
-
} else if (
|
696
|
+
} else if (isBinary(node)) {
|
651
697
|
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
652
|
-
} else if (
|
698
|
+
} else if (isArrayExpression(node)) {
|
653
699
|
for (const elem of node.elements) {
|
654
700
|
if (!this.isPure(elem, constantsOnly)) return false;
|
655
701
|
}
|
656
702
|
|
657
703
|
return true;
|
658
|
-
} else if (
|
704
|
+
} else if (isObjectExpression(node)) {
|
659
705
|
for (const prop of node.properties) {
|
660
706
|
if (!this.isPure(prop, constantsOnly)) return false;
|
661
707
|
}
|
662
708
|
|
663
709
|
return true;
|
664
|
-
} else if (
|
710
|
+
} else if (isMethod(node)) {
|
665
711
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
666
712
|
if (node.kind === "get" || node.kind === "set") return false;
|
667
713
|
return true;
|
668
|
-
} else if (
|
714
|
+
} else if (isProperty(node)) {
|
669
715
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
670
716
|
return this.isPure(node.value, constantsOnly);
|
671
|
-
} else if (
|
717
|
+
} else if (isUnaryExpression(node)) {
|
672
718
|
return this.isPure(node.argument, constantsOnly);
|
673
|
-
} else if (
|
674
|
-
return
|
675
|
-
} 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)) {
|
676
722
|
for (const expression of node.expressions) {
|
677
723
|
if (!this.isPure(expression, constantsOnly)) return false;
|
678
724
|
}
|
679
725
|
|
680
726
|
return true;
|
681
727
|
} else {
|
682
|
-
return
|
728
|
+
return isPureish(node);
|
683
729
|
}
|
684
730
|
}
|
685
731
|
|
@@ -794,13 +840,13 @@ class Scope {
|
|
794
840
|
let declarPath = !unique && path.getData(dataKey);
|
795
841
|
|
796
842
|
if (!declarPath) {
|
797
|
-
const declar =
|
843
|
+
const declar = variableDeclaration(kind, []);
|
798
844
|
declar._blockHoist = blockHoist;
|
799
845
|
[declarPath] = path.unshiftContainer("body", [declar]);
|
800
846
|
if (!unique) path.setData(dataKey, declarPath);
|
801
847
|
}
|
802
848
|
|
803
|
-
const declarator =
|
849
|
+
const declarator = variableDeclarator(opts.id, opts.init);
|
804
850
|
declarPath.node.declarations.push(declarator);
|
805
851
|
this.registerBinding(kind, declarPath.get("declarations").pop());
|
806
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.15.
|
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.15.
|
21
|
-
"@babel/helper-function-name": "^7.
|
22
|
-
"@babel/helper-hoist-variables": "^7.
|
23
|
-
"@babel/helper-split-export-declaration": "^7.
|
24
|
-
"@babel/parser": "^7.15.
|
25
|
-
"@babel/types": "^7.15.
|
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
|
},
|