@babel/traverse 7.9.0 → 7.10.1
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 +1 -1
- package/lib/path/context.js +3 -1
- package/lib/path/conversion.js +1 -1
- package/lib/path/evaluation.js +1 -1
- package/lib/path/inference/index.js +3 -1
- package/lib/path/inference/inferer-reference.js +26 -2
- package/lib/path/inference/inferers.js +25 -3
- package/lib/path/removal.js +3 -1
- package/lib/path/replacement.js +2 -2
- package/lib/scope/index.js +54 -50
- package/lib/scope/lib/renamer.js +2 -1
- package/package.json +14 -10
package/lib/context.js
CHANGED
@@ -31,7 +31,7 @@ class TraversalContext {
|
|
31
31
|
if (opts.enter || opts.exit) return true;
|
32
32
|
if (opts[node.type]) return true;
|
33
33
|
const keys = t.VISITOR_KEYS[node.type];
|
34
|
-
if (!keys
|
34
|
+
if (!(keys == null ? void 0 : keys.length)) return false;
|
35
35
|
|
36
36
|
for (const key of keys) {
|
37
37
|
if (node[key]) return true;
|
package/lib/path/context.js
CHANGED
@@ -221,9 +221,11 @@ function setup(parentPath, container, listKey, key) {
|
|
221
221
|
}
|
222
222
|
|
223
223
|
function setKey(key) {
|
224
|
+
var _this$node;
|
225
|
+
|
224
226
|
this.key = key;
|
225
227
|
this.node = this.container[this.key];
|
226
|
-
this.type =
|
228
|
+
this.type = (_this$node = this.node) == null ? void 0 : _this$node.type;
|
227
229
|
}
|
228
230
|
|
229
231
|
function requeue(pathToQueue = this) {
|
package/lib/path/conversion.js
CHANGED
@@ -127,7 +127,7 @@ function hoistFunctionEnvironment(fnPath, specCompliant = false, allowInsertArro
|
|
127
127
|
static: false
|
128
128
|
});
|
129
129
|
});
|
130
|
-
const inConstructor = thisEnvFn
|
130
|
+
const inConstructor = (thisEnvFn == null ? void 0 : thisEnvFn.node.kind) === "constructor";
|
131
131
|
|
132
132
|
if (thisEnvFn.isClassProperty()) {
|
133
133
|
throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
|
package/lib/path/evaluation.js
CHANGED
@@ -132,7 +132,7 @@ function _evaluate(path, state) {
|
|
132
132
|
return deopt(binding.path, state);
|
133
133
|
}
|
134
134
|
|
135
|
-
if (binding
|
135
|
+
if (binding == null ? void 0 : binding.hasValue) {
|
136
136
|
return binding.value;
|
137
137
|
} else {
|
138
138
|
if (node.name === "undefined") {
|
@@ -26,6 +26,8 @@ function getTypeAnnotation() {
|
|
26
26
|
}
|
27
27
|
|
28
28
|
function _getTypeAnnotation() {
|
29
|
+
var _inferer;
|
30
|
+
|
29
31
|
const node = this.node;
|
30
32
|
|
31
33
|
if (!node) {
|
@@ -59,7 +61,7 @@ function _getTypeAnnotation() {
|
|
59
61
|
|
60
62
|
inferer = inferers[this.parentPath.type];
|
61
63
|
|
62
|
-
if (inferer
|
64
|
+
if ((_inferer = inferer) == null ? void 0 : _inferer.validParent) {
|
63
65
|
return this.parentPath.getTypeAnnotation();
|
64
66
|
}
|
65
67
|
}
|
@@ -50,9 +50,19 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
|
|
50
50
|
}
|
51
51
|
}
|
52
52
|
|
53
|
-
if (types.length) {
|
54
|
-
return
|
53
|
+
if (!types.length) {
|
54
|
+
return;
|
55
|
+
}
|
56
|
+
|
57
|
+
if (t.isTSTypeAnnotation(types[0]) && t.createTSUnionType) {
|
58
|
+
return t.createTSUnionType(types);
|
59
|
+
}
|
60
|
+
|
61
|
+
if (t.createFlowUnionType) {
|
62
|
+
return t.createFlowUnionType(types);
|
55
63
|
}
|
64
|
+
|
65
|
+
return t.createUnionTypeAnnotation(types);
|
56
66
|
}
|
57
67
|
|
58
68
|
function getConstantViolationsBefore(binding, path, functions) {
|
@@ -165,6 +175,20 @@ function getConditionalAnnotation(binding, path, name) {
|
|
165
175
|
}
|
166
176
|
|
167
177
|
if (types.length) {
|
178
|
+
if (t.isTSTypeAnnotation(types[0]) && t.createTSUnionType) {
|
179
|
+
return {
|
180
|
+
typeAnnotation: t.createTSUnionType(types),
|
181
|
+
ifStatement
|
182
|
+
};
|
183
|
+
}
|
184
|
+
|
185
|
+
if (t.createFlowUnionType) {
|
186
|
+
return {
|
187
|
+
typeAnnotation: t.createFlowUnionType(types),
|
188
|
+
ifStatement
|
189
|
+
};
|
190
|
+
}
|
191
|
+
|
168
192
|
return {
|
169
193
|
typeAnnotation: t.createUnionTypeAnnotation(types),
|
170
194
|
ifStatement
|
@@ -44,12 +44,14 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
|
|
44
44
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
45
45
|
|
46
46
|
function VariableDeclarator() {
|
47
|
+
var _type;
|
48
|
+
|
47
49
|
const id = this.get("id");
|
48
50
|
if (!id.isIdentifier()) return;
|
49
51
|
const init = this.get("init");
|
50
52
|
let type = init.getTypeAnnotation();
|
51
53
|
|
52
|
-
if (type
|
54
|
+
if (((_type = type) == null ? void 0 : _type.type) === "AnyTypeAnnotation") {
|
53
55
|
if (init.isCallExpression() && init.get("callee").isIdentifier({
|
54
56
|
name: "Array"
|
55
57
|
}) && !init.scope.hasBinding("Array", true)) {
|
@@ -112,11 +114,31 @@ function BinaryExpression(node) {
|
|
112
114
|
}
|
113
115
|
|
114
116
|
function LogicalExpression() {
|
115
|
-
|
117
|
+
const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
|
118
|
+
|
119
|
+
if (t.isTSTypeAnnotation(argumentTypes[0]) && t.createTSUnionType) {
|
120
|
+
return t.createTSUnionType(argumentTypes);
|
121
|
+
}
|
122
|
+
|
123
|
+
if (t.createFlowUnionType) {
|
124
|
+
return t.createFlowUnionType(argumentTypes);
|
125
|
+
}
|
126
|
+
|
127
|
+
return t.createUnionTypeAnnotation(argumentTypes);
|
116
128
|
}
|
117
129
|
|
118
130
|
function ConditionalExpression() {
|
119
|
-
|
131
|
+
const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
|
132
|
+
|
133
|
+
if (t.isTSTypeAnnotation(argumentTypes[0]) && t.createTSUnionType) {
|
134
|
+
return t.createTSUnionType(argumentTypes);
|
135
|
+
}
|
136
|
+
|
137
|
+
if (t.createFlowUnionType) {
|
138
|
+
return t.createFlowUnionType(argumentTypes);
|
139
|
+
}
|
140
|
+
|
141
|
+
return t.createUnionTypeAnnotation(argumentTypes);
|
120
142
|
}
|
121
143
|
|
122
144
|
function SequenceExpression() {
|
package/lib/path/removal.js
CHANGED
@@ -15,11 +15,13 @@ var _removalHooks = require("./lib/removal-hooks");
|
|
15
15
|
var _index = require("./index");
|
16
16
|
|
17
17
|
function remove() {
|
18
|
+
var _this$opts;
|
19
|
+
|
18
20
|
this._assertUnremoved();
|
19
21
|
|
20
22
|
this.resync();
|
21
23
|
|
22
|
-
if (!
|
24
|
+
if (!((_this$opts = this.opts) == null ? void 0 : _this$opts.noScope)) {
|
23
25
|
this._removeFromScope();
|
24
26
|
}
|
25
27
|
|
package/lib/path/replacement.js
CHANGED
@@ -172,7 +172,7 @@ function _replaceWith(node) {
|
|
172
172
|
t.validate(this.parent, this.key, node);
|
173
173
|
}
|
174
174
|
|
175
|
-
this.debug(`Replace with ${node
|
175
|
+
this.debug(`Replace with ${node == null ? void 0 : node.type}`);
|
176
176
|
this.node = this.container[this.key] = node;
|
177
177
|
}
|
178
178
|
|
@@ -185,7 +185,7 @@ function replaceExpressionWithStatements(nodes) {
|
|
185
185
|
}
|
186
186
|
|
187
187
|
const functionParent = this.getFunctionParent();
|
188
|
-
const isParentAsync = functionParent
|
188
|
+
const isParentAsync = functionParent == null ? void 0 : functionParent.is("async");
|
189
189
|
const container = t.arrowFunctionExpression([], t.blockStatement(nodes));
|
190
190
|
this.replaceWith(t.callExpression(container, []));
|
191
191
|
this.traverse(hoistVariablesVisitor);
|
package/lib/scope/index.js
CHANGED
@@ -270,6 +270,28 @@ const collectorVisitor = {
|
|
270
270
|
path.scope.getBlockParent().registerDeclaration(bodyPath);
|
271
271
|
}
|
272
272
|
}
|
273
|
+
},
|
274
|
+
|
275
|
+
CatchClause(path) {
|
276
|
+
path.scope.registerBinding("let", path);
|
277
|
+
},
|
278
|
+
|
279
|
+
Function(path) {
|
280
|
+
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[t.NOT_LOCAL_BINDING]) {
|
281
|
+
path.scope.registerBinding("local", path.get("id"), path);
|
282
|
+
}
|
283
|
+
|
284
|
+
const params = path.get("params");
|
285
|
+
|
286
|
+
for (const param of params) {
|
287
|
+
path.scope.registerBinding("param", param);
|
288
|
+
}
|
289
|
+
},
|
290
|
+
|
291
|
+
ClassExpression(path) {
|
292
|
+
if (path.has("id") && !path.get("id").node[t.NOT_LOCAL_BINDING]) {
|
293
|
+
path.scope.registerBinding("local", path);
|
294
|
+
}
|
273
295
|
}
|
274
296
|
|
275
297
|
};
|
@@ -283,7 +305,7 @@ class Scope {
|
|
283
305
|
|
284
306
|
const cached = _cache.scope.get(node);
|
285
307
|
|
286
|
-
if (cached
|
308
|
+
if ((cached == null ? void 0 : cached.path) === path) {
|
287
309
|
return cached;
|
288
310
|
}
|
289
311
|
|
@@ -293,11 +315,12 @@ class Scope {
|
|
293
315
|
this.block = node;
|
294
316
|
this.path = path;
|
295
317
|
this.labels = new Map();
|
318
|
+
this.inited = false;
|
296
319
|
}
|
297
320
|
|
298
321
|
get parent() {
|
299
322
|
const parent = this.path.findParent(p => p.isScope());
|
300
|
-
return parent
|
323
|
+
return parent == null ? void 0 : parent.scope;
|
301
324
|
}
|
302
325
|
|
303
326
|
get parentBlock() {
|
@@ -441,11 +464,11 @@ class Scope {
|
|
441
464
|
console.log(sep);
|
442
465
|
}
|
443
466
|
|
444
|
-
toArray(node, i) {
|
467
|
+
toArray(node, i, allowArrayLike) {
|
445
468
|
if (t.isIdentifier(node)) {
|
446
469
|
const binding = this.getBinding(node.name);
|
447
470
|
|
448
|
-
if (binding
|
471
|
+
if ((binding == null ? void 0 : binding.constant) && binding.path.isGenericType("Array")) {
|
449
472
|
return node;
|
450
473
|
}
|
451
474
|
}
|
@@ -472,6 +495,11 @@ class Scope {
|
|
472
495
|
helperName = "toArray";
|
473
496
|
}
|
474
497
|
|
498
|
+
if (allowArrayLike) {
|
499
|
+
args.unshift(this.hub.addHelper(helperName));
|
500
|
+
helperName = "maybeArrayLike";
|
501
|
+
}
|
502
|
+
|
475
503
|
return t.callExpression(this.hub.addHelper(helperName), args);
|
476
504
|
}
|
477
505
|
|
@@ -547,6 +575,8 @@ class Scope {
|
|
547
575
|
const ids = path.getOuterBindingIdentifiers(true);
|
548
576
|
|
549
577
|
for (const name of Object.keys(ids)) {
|
578
|
+
parent.references[name] = true;
|
579
|
+
|
550
580
|
for (const id of ids[name]) {
|
551
581
|
const local = this.getOwnBinding(name);
|
552
582
|
|
@@ -555,8 +585,6 @@ class Scope {
|
|
555
585
|
this.checkBlockScopedCollisions(local, kind, name, id);
|
556
586
|
}
|
557
587
|
|
558
|
-
parent.references[name] = true;
|
559
|
-
|
560
588
|
if (local) {
|
561
589
|
this.registerConstantViolation(bindingPath);
|
562
590
|
} else {
|
@@ -596,13 +624,7 @@ class Scope {
|
|
596
624
|
}
|
597
625
|
|
598
626
|
hasReference(name) {
|
599
|
-
|
600
|
-
|
601
|
-
do {
|
602
|
-
if (scope.references[name]) return true;
|
603
|
-
} while (scope = scope.parent);
|
604
|
-
|
605
|
-
return false;
|
627
|
+
return !!this.getProgramParent().references[name];
|
606
628
|
}
|
607
629
|
|
608
630
|
isPure(node, constantsOnly) {
|
@@ -637,7 +659,7 @@ class Scope {
|
|
637
659
|
}
|
638
660
|
|
639
661
|
return true;
|
640
|
-
} else if (t.
|
662
|
+
} else if (t.isMethod(node)) {
|
641
663
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
642
664
|
if (node.kind === "get" || node.kind === "set") return false;
|
643
665
|
return true;
|
@@ -682,7 +704,10 @@ class Scope {
|
|
682
704
|
}
|
683
705
|
|
684
706
|
init() {
|
685
|
-
if (!this.
|
707
|
+
if (!this.inited) {
|
708
|
+
this.inited = true;
|
709
|
+
this.crawl();
|
710
|
+
}
|
686
711
|
}
|
687
712
|
|
688
713
|
crawl() {
|
@@ -693,26 +718,11 @@ class Scope {
|
|
693
718
|
this.uids = Object.create(null);
|
694
719
|
this.data = Object.create(null);
|
695
720
|
|
696
|
-
if (path.
|
697
|
-
|
698
|
-
const node = path.get(key);
|
699
|
-
if (node.isBlockScoped()) this.registerBinding(node.node.kind, node);
|
700
|
-
}
|
701
|
-
}
|
702
|
-
|
703
|
-
if (path.isFunctionExpression() && path.has("id")) {
|
704
|
-
if (!path.get("id").node[t.NOT_LOCAL_BINDING]) {
|
721
|
+
if (path.isFunction()) {
|
722
|
+
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[t.NOT_LOCAL_BINDING]) {
|
705
723
|
this.registerBinding("local", path.get("id"), path);
|
706
724
|
}
|
707
|
-
}
|
708
725
|
|
709
|
-
if (path.isClassExpression() && path.has("id")) {
|
710
|
-
if (!path.get("id").node[t.NOT_LOCAL_BINDING]) {
|
711
|
-
this.registerBinding("local", path);
|
712
|
-
}
|
713
|
-
}
|
714
|
-
|
715
|
-
if (path.isFunction()) {
|
716
726
|
const params = path.get("params");
|
717
727
|
|
718
728
|
for (const param of params) {
|
@@ -720,12 +730,8 @@ class Scope {
|
|
720
730
|
}
|
721
731
|
}
|
722
732
|
|
723
|
-
|
724
|
-
|
725
|
-
}
|
726
|
-
|
727
|
-
const parent = this.getProgramParent();
|
728
|
-
if (parent.crawling) return;
|
733
|
+
const programParent = this.getProgramParent();
|
734
|
+
if (programParent.crawling) return;
|
729
735
|
const state = {
|
730
736
|
references: [],
|
731
737
|
constantViolations: [],
|
@@ -737,11 +743,9 @@ class Scope {
|
|
737
743
|
|
738
744
|
for (const path of state.assignments) {
|
739
745
|
const ids = path.getBindingIdentifiers();
|
740
|
-
let programParent;
|
741
746
|
|
742
747
|
for (const name of Object.keys(ids)) {
|
743
748
|
if (path.scope.getBinding(name)) continue;
|
744
|
-
programParent = programParent || path.scope.getProgramParent();
|
745
749
|
programParent.addGlobal(ids[name]);
|
746
750
|
}
|
747
751
|
|
@@ -754,7 +758,7 @@ class Scope {
|
|
754
758
|
if (binding) {
|
755
759
|
binding.reference(ref);
|
756
760
|
} else {
|
757
|
-
|
761
|
+
programParent.addGlobal(ref.node);
|
758
762
|
}
|
759
763
|
}
|
760
764
|
|
@@ -890,13 +894,14 @@ class Scope {
|
|
890
894
|
}
|
891
895
|
|
892
896
|
getBindingIdentifier(name) {
|
893
|
-
|
894
|
-
|
897
|
+
var _this$getBinding;
|
898
|
+
|
899
|
+
return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
|
895
900
|
}
|
896
901
|
|
897
902
|
getOwnBindingIdentifier(name) {
|
898
903
|
const binding = this.bindings[name];
|
899
|
-
return binding
|
904
|
+
return binding == null ? void 0 : binding.identifier;
|
900
905
|
}
|
901
906
|
|
902
907
|
hasOwnBinding(name) {
|
@@ -914,7 +919,9 @@ class Scope {
|
|
914
919
|
}
|
915
920
|
|
916
921
|
parentHasBinding(name, noGlobals) {
|
917
|
-
|
922
|
+
var _this$parent;
|
923
|
+
|
924
|
+
return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, noGlobals);
|
918
925
|
}
|
919
926
|
|
920
927
|
moveBindingTo(name, scope) {
|
@@ -932,12 +939,9 @@ class Scope {
|
|
932
939
|
}
|
933
940
|
|
934
941
|
removeBinding(name) {
|
935
|
-
|
936
|
-
|
937
|
-
if (info) {
|
938
|
-
info.scope.removeOwnBinding(name);
|
939
|
-
}
|
942
|
+
var _this$getBinding2;
|
940
943
|
|
944
|
+
(_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
|
941
945
|
let scope = this;
|
942
946
|
|
943
947
|
do {
|
package/lib/scope/lib/renamer.js
CHANGED
@@ -32,7 +32,8 @@ const renameVisitor = {
|
|
32
32
|
}
|
33
33
|
},
|
34
34
|
|
35
|
-
"AssignmentExpression|Declaration"(path, state) {
|
35
|
+
"AssignmentExpression|Declaration|VariableDeclarator"(path, state) {
|
36
|
+
if (path.isVariableDeclaration()) return;
|
36
37
|
const ids = path.getOuterBindingIdentifiers();
|
37
38
|
|
38
39
|
for (const name in ids) {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@babel/traverse",
|
3
|
-
"version": "7.
|
3
|
+
"version": "7.10.1",
|
4
4
|
"description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes",
|
5
5
|
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
6
6
|
"homepage": "https://babeljs.io/",
|
@@ -8,21 +8,25 @@
|
|
8
8
|
"publishConfig": {
|
9
9
|
"access": "public"
|
10
10
|
},
|
11
|
-
"repository":
|
11
|
+
"repository": {
|
12
|
+
"type": "git",
|
13
|
+
"url": "https://github.com/babel/babel.git",
|
14
|
+
"directory": "packages/babel-traverse"
|
15
|
+
},
|
12
16
|
"main": "lib/index.js",
|
13
17
|
"dependencies": {
|
14
|
-
"@babel/code-frame": "^7.
|
15
|
-
"@babel/generator": "^7.
|
16
|
-
"@babel/helper-function-name": "^7.
|
17
|
-
"@babel/helper-split-export-declaration": "^7.
|
18
|
-
"@babel/parser": "^7.
|
19
|
-
"@babel/types": "^7.
|
18
|
+
"@babel/code-frame": "^7.10.1",
|
19
|
+
"@babel/generator": "^7.10.1",
|
20
|
+
"@babel/helper-function-name": "^7.10.1",
|
21
|
+
"@babel/helper-split-export-declaration": "^7.10.1",
|
22
|
+
"@babel/parser": "^7.10.1",
|
23
|
+
"@babel/types": "^7.10.1",
|
20
24
|
"debug": "^4.1.0",
|
21
25
|
"globals": "^11.1.0",
|
22
26
|
"lodash": "^4.17.13"
|
23
27
|
},
|
24
28
|
"devDependencies": {
|
25
|
-
"@babel/helper-plugin-test-runner": "^7.
|
29
|
+
"@babel/helper-plugin-test-runner": "^7.10.1"
|
26
30
|
},
|
27
|
-
"gitHead": "
|
31
|
+
"gitHead": "88f57a7ea659d25232bf62de1efceb5d6299b8cf"
|
28
32
|
}
|