@babel/traverse 7.16.0 → 7.16.8
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/index.js +5 -12
- package/lib/path/context.js +4 -6
- package/lib/path/conversion.js +149 -110
- package/lib/scope/index.js +4 -1
- package/lib/traverse-node.js +30 -0
- package/package.json +10 -9
- package/scripts/generators/validators.js +1 -2
package/lib/index.js
CHANGED
@@ -23,8 +23,6 @@ Object.defineProperty(exports, "Scope", {
|
|
23
23
|
});
|
24
24
|
exports.visitors = exports.default = void 0;
|
25
25
|
|
26
|
-
var _context = require("./context");
|
27
|
-
|
28
26
|
var visitors = require("./visitors");
|
29
27
|
|
30
28
|
exports.visitors = visitors;
|
@@ -33,6 +31,8 @@ var _t = require("@babel/types");
|
|
33
31
|
|
34
32
|
var cache = require("./cache");
|
35
33
|
|
34
|
+
var _traverseNode = require("./traverse-node");
|
35
|
+
|
36
36
|
var _path = require("./path");
|
37
37
|
|
38
38
|
var _scope = require("./scope");
|
@@ -59,7 +59,7 @@ function traverse(parent, opts = {}, scope, state, parentPath) {
|
|
59
59
|
}
|
60
60
|
|
61
61
|
visitors.explode(opts);
|
62
|
-
|
62
|
+
(0, _traverseNode.traverseNode)(parent, opts, scope, state, parentPath);
|
63
63
|
}
|
64
64
|
|
65
65
|
var _default = traverse;
|
@@ -72,15 +72,8 @@ traverse.cheap = function (node, enter) {
|
|
72
72
|
return traverseFast(node, enter);
|
73
73
|
};
|
74
74
|
|
75
|
-
traverse.node = function (node, opts, scope, state,
|
76
|
-
|
77
|
-
if (!keys) return;
|
78
|
-
const context = new _context.default(scope, opts, state, parentPath);
|
79
|
-
|
80
|
-
for (const key of keys) {
|
81
|
-
if (skipKeys && skipKeys[key]) continue;
|
82
|
-
if (context.visit(node, key)) return;
|
83
|
-
}
|
75
|
+
traverse.node = function (node, opts, scope, state, path, skipKeys) {
|
76
|
+
(0, _traverseNode.traverseNode)(node, opts, scope, state, path, skipKeys);
|
84
77
|
};
|
85
78
|
|
86
79
|
traverse.clearNode = function (node, opts) {
|
package/lib/path/context.js
CHANGED
@@ -24,9 +24,9 @@ exports.skipKey = skipKey;
|
|
24
24
|
exports.stop = stop;
|
25
25
|
exports.visit = visit;
|
26
26
|
|
27
|
-
var
|
27
|
+
var _traverseNode = require("../traverse-node");
|
28
28
|
|
29
|
-
var
|
29
|
+
var _index = require("./index");
|
30
30
|
|
31
31
|
function call(key) {
|
32
32
|
const opts = this.opts;
|
@@ -104,9 +104,7 @@ function visit() {
|
|
104
104
|
|
105
105
|
restoreContext(this, currentContext);
|
106
106
|
this.debug("Recursing into...");
|
107
|
-
|
108
|
-
_index.default.node(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
|
109
|
-
|
107
|
+
this.shouldStop = (0, _traverseNode.traverseNode)(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
|
110
108
|
restoreContext(this, currentContext);
|
111
109
|
this.call("exit");
|
112
110
|
return this.shouldStop;
|
@@ -125,7 +123,7 @@ function skipKey(key) {
|
|
125
123
|
}
|
126
124
|
|
127
125
|
function stop() {
|
128
|
-
this._traverseFlags |=
|
126
|
+
this._traverseFlags |= _index.SHOULD_SKIP | _index.SHOULD_STOP;
|
129
127
|
}
|
130
128
|
|
131
129
|
function setScope() {
|
package/lib/path/conversion.js
CHANGED
@@ -11,8 +11,12 @@ exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
|
|
11
11
|
|
12
12
|
var _t = require("@babel/types");
|
13
13
|
|
14
|
+
var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
|
15
|
+
|
14
16
|
var _helperFunctionName = require("@babel/helper-function-name");
|
15
17
|
|
18
|
+
var _visitors = require("../visitors");
|
19
|
+
|
16
20
|
const {
|
17
21
|
arrowFunctionExpression,
|
18
22
|
assignmentExpression,
|
@@ -35,6 +39,7 @@ const {
|
|
35
39
|
stringLiteral,
|
36
40
|
super: _super,
|
37
41
|
thisExpression,
|
42
|
+
toExpression,
|
38
43
|
unaryExpression
|
39
44
|
} = _t;
|
40
45
|
|
@@ -121,35 +126,68 @@ function arrowFunctionToExpression({
|
|
121
126
|
throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression.");
|
122
127
|
}
|
123
128
|
|
124
|
-
const
|
125
|
-
|
126
|
-
|
129
|
+
const {
|
130
|
+
thisBinding,
|
131
|
+
fnPath: fn
|
132
|
+
} = hoistFunctionEnvironment(this, noNewArrows, allowInsertArrow);
|
133
|
+
fn.ensureBlock();
|
134
|
+
fn.node.type = "FunctionExpression";
|
127
135
|
|
128
136
|
if (!noNewArrows) {
|
129
|
-
const checkBinding = thisBinding ? null :
|
137
|
+
const checkBinding = thisBinding ? null : fn.scope.generateUidIdentifier("arrowCheckId");
|
130
138
|
|
131
139
|
if (checkBinding) {
|
132
|
-
|
140
|
+
fn.parentPath.scope.push({
|
133
141
|
id: checkBinding,
|
134
142
|
init: objectExpression([])
|
135
143
|
});
|
136
144
|
}
|
137
145
|
|
138
|
-
|
139
|
-
|
146
|
+
fn.get("body").unshiftContainer("body", expressionStatement(callExpression(this.hub.addHelper("newArrowCheck"), [thisExpression(), checkBinding ? identifier(checkBinding.name) : identifier(thisBinding)])));
|
147
|
+
fn.replaceWith(callExpression(memberExpression((0, _helperFunctionName.default)(this, true) || fn.node, identifier("bind")), [checkBinding ? identifier(checkBinding.name) : thisExpression()]));
|
140
148
|
}
|
141
149
|
}
|
142
150
|
|
151
|
+
const getSuperCallsVisitor = (0, _visitors.merge)([{
|
152
|
+
CallExpression(child, {
|
153
|
+
allSuperCalls
|
154
|
+
}) {
|
155
|
+
if (!child.get("callee").isSuper()) return;
|
156
|
+
allSuperCalls.push(child);
|
157
|
+
}
|
158
|
+
|
159
|
+
}, _helperEnvironmentVisitor.default]);
|
160
|
+
|
143
161
|
function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow = true) {
|
144
|
-
|
145
|
-
|
162
|
+
let arrowParent;
|
163
|
+
let thisEnvFn = fnPath.findParent(p => {
|
164
|
+
if (p.isArrowFunctionExpression()) {
|
165
|
+
var _arrowParent;
|
166
|
+
|
167
|
+
(_arrowParent = arrowParent) != null ? _arrowParent : arrowParent = p;
|
168
|
+
return false;
|
169
|
+
}
|
170
|
+
|
171
|
+
return p.isFunction() || p.isProgram() || p.isClassProperty({
|
172
|
+
static: false
|
173
|
+
}) || p.isClassPrivateProperty({
|
146
174
|
static: false
|
147
175
|
});
|
148
176
|
});
|
149
|
-
const inConstructor =
|
177
|
+
const inConstructor = thisEnvFn.isClassMethod({
|
178
|
+
kind: "constructor"
|
179
|
+
});
|
150
180
|
|
151
|
-
if (thisEnvFn.isClassProperty()) {
|
152
|
-
|
181
|
+
if (thisEnvFn.isClassProperty() || thisEnvFn.isClassPrivateProperty()) {
|
182
|
+
if (arrowParent) {
|
183
|
+
thisEnvFn = arrowParent;
|
184
|
+
} else if (allowInsertArrow) {
|
185
|
+
fnPath.replaceWith(callExpression(arrowFunctionExpression([], toExpression(fnPath.node)), []));
|
186
|
+
thisEnvFn = fnPath.get("callee");
|
187
|
+
fnPath = thisEnvFn.get("body");
|
188
|
+
} else {
|
189
|
+
throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
|
190
|
+
}
|
153
191
|
}
|
154
192
|
|
155
193
|
const {
|
@@ -166,21 +204,8 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
|
|
166
204
|
}
|
167
205
|
|
168
206
|
const allSuperCalls = [];
|
169
|
-
thisEnvFn.traverse({
|
170
|
-
|
171
|
-
if (child.isArrowFunctionExpression()) return;
|
172
|
-
child.skip();
|
173
|
-
},
|
174
|
-
|
175
|
-
ClassProperty(child) {
|
176
|
-
child.skip();
|
177
|
-
},
|
178
|
-
|
179
|
-
CallExpression(child) {
|
180
|
-
if (!child.get("callee").isSuper()) return;
|
181
|
-
allSuperCalls.push(child);
|
182
|
-
}
|
183
|
-
|
207
|
+
thisEnvFn.traverse(getSuperCallsVisitor, {
|
208
|
+
allSuperCalls
|
184
209
|
});
|
185
210
|
const superBinding = getSuperBinding(thisEnvFn);
|
186
211
|
allSuperCalls.forEach(superCall => {
|
@@ -271,7 +296,10 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
|
|
271
296
|
}
|
272
297
|
}
|
273
298
|
|
274
|
-
return
|
299
|
+
return {
|
300
|
+
thisBinding,
|
301
|
+
fnPath
|
302
|
+
};
|
275
303
|
}
|
276
304
|
|
277
305
|
function standardizeSuperProperty(superProp) {
|
@@ -314,27 +342,25 @@ function hasSuperClass(thisEnvFn) {
|
|
314
342
|
return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
|
315
343
|
}
|
316
344
|
|
345
|
+
const assignSuperThisVisitor = (0, _visitors.merge)([{
|
346
|
+
CallExpression(child, {
|
347
|
+
supers,
|
348
|
+
thisBinding
|
349
|
+
}) {
|
350
|
+
if (!child.get("callee").isSuper()) return;
|
351
|
+
if (supers.has(child.node)) return;
|
352
|
+
supers.add(child.node);
|
353
|
+
child.replaceWithMultiple([child.node, assignmentExpression("=", identifier(thisBinding), identifier("this"))]);
|
354
|
+
}
|
355
|
+
|
356
|
+
}, _helperEnvironmentVisitor.default]);
|
357
|
+
|
317
358
|
function getThisBinding(thisEnvFn, inConstructor) {
|
318
359
|
return getBinding(thisEnvFn, "this", thisBinding => {
|
319
360
|
if (!inConstructor || !hasSuperClass(thisEnvFn)) return thisExpression();
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
if (child.isArrowFunctionExpression()) return;
|
324
|
-
child.skip();
|
325
|
-
},
|
326
|
-
|
327
|
-
ClassProperty(child) {
|
328
|
-
child.skip();
|
329
|
-
},
|
330
|
-
|
331
|
-
CallExpression(child) {
|
332
|
-
if (!child.get("callee").isSuper()) return;
|
333
|
-
if (supers.has(child.node)) return;
|
334
|
-
supers.add(child.node);
|
335
|
-
child.replaceWithMultiple([child.node, assignmentExpression("=", identifier(thisBinding), identifier("this"))]);
|
336
|
-
}
|
337
|
-
|
361
|
+
thisEnvFn.traverse(assignSuperThisVisitor, {
|
362
|
+
supers: new WeakSet(),
|
363
|
+
thisBinding
|
338
364
|
});
|
339
365
|
});
|
340
366
|
}
|
@@ -387,76 +413,89 @@ function getBinding(thisEnvFn, key, init) {
|
|
387
413
|
return data;
|
388
414
|
}
|
389
415
|
|
416
|
+
const getScopeInformationVisitor = (0, _visitors.merge)([{
|
417
|
+
ThisExpression(child, {
|
418
|
+
thisPaths
|
419
|
+
}) {
|
420
|
+
thisPaths.push(child);
|
421
|
+
},
|
422
|
+
|
423
|
+
JSXIdentifier(child, {
|
424
|
+
thisPaths
|
425
|
+
}) {
|
426
|
+
if (child.node.name !== "this") return;
|
427
|
+
|
428
|
+
if (!child.parentPath.isJSXMemberExpression({
|
429
|
+
object: child.node
|
430
|
+
}) && !child.parentPath.isJSXOpeningElement({
|
431
|
+
name: child.node
|
432
|
+
})) {
|
433
|
+
return;
|
434
|
+
}
|
435
|
+
|
436
|
+
thisPaths.push(child);
|
437
|
+
},
|
438
|
+
|
439
|
+
CallExpression(child, {
|
440
|
+
superCalls
|
441
|
+
}) {
|
442
|
+
if (child.get("callee").isSuper()) superCalls.push(child);
|
443
|
+
},
|
444
|
+
|
445
|
+
MemberExpression(child, {
|
446
|
+
superProps
|
447
|
+
}) {
|
448
|
+
if (child.get("object").isSuper()) superProps.push(child);
|
449
|
+
},
|
450
|
+
|
451
|
+
Identifier(child, {
|
452
|
+
argumentsPaths
|
453
|
+
}) {
|
454
|
+
if (!child.isReferencedIdentifier({
|
455
|
+
name: "arguments"
|
456
|
+
})) return;
|
457
|
+
let curr = child.scope;
|
458
|
+
|
459
|
+
do {
|
460
|
+
if (curr.hasOwnBinding("arguments")) {
|
461
|
+
curr.rename("arguments");
|
462
|
+
return;
|
463
|
+
}
|
464
|
+
|
465
|
+
if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
|
466
|
+
break;
|
467
|
+
}
|
468
|
+
} while (curr = curr.parent);
|
469
|
+
|
470
|
+
argumentsPaths.push(child);
|
471
|
+
},
|
472
|
+
|
473
|
+
MetaProperty(child, {
|
474
|
+
newTargetPaths
|
475
|
+
}) {
|
476
|
+
if (!child.get("meta").isIdentifier({
|
477
|
+
name: "new"
|
478
|
+
})) return;
|
479
|
+
if (!child.get("property").isIdentifier({
|
480
|
+
name: "target"
|
481
|
+
})) return;
|
482
|
+
newTargetPaths.push(child);
|
483
|
+
}
|
484
|
+
|
485
|
+
}, _helperEnvironmentVisitor.default]);
|
486
|
+
|
390
487
|
function getScopeInformation(fnPath) {
|
391
488
|
const thisPaths = [];
|
392
489
|
const argumentsPaths = [];
|
393
490
|
const newTargetPaths = [];
|
394
491
|
const superProps = [];
|
395
492
|
const superCalls = [];
|
396
|
-
fnPath.traverse({
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
if (child.isArrowFunctionExpression()) return;
|
403
|
-
child.skip();
|
404
|
-
},
|
405
|
-
|
406
|
-
ThisExpression(child) {
|
407
|
-
thisPaths.push(child);
|
408
|
-
},
|
409
|
-
|
410
|
-
JSXIdentifier(child) {
|
411
|
-
if (child.node.name !== "this") return;
|
412
|
-
|
413
|
-
if (!child.parentPath.isJSXMemberExpression({
|
414
|
-
object: child.node
|
415
|
-
}) && !child.parentPath.isJSXOpeningElement({
|
416
|
-
name: child.node
|
417
|
-
})) {
|
418
|
-
return;
|
419
|
-
}
|
420
|
-
|
421
|
-
thisPaths.push(child);
|
422
|
-
},
|
423
|
-
|
424
|
-
CallExpression(child) {
|
425
|
-
if (child.get("callee").isSuper()) superCalls.push(child);
|
426
|
-
},
|
427
|
-
|
428
|
-
MemberExpression(child) {
|
429
|
-
if (child.get("object").isSuper()) superProps.push(child);
|
430
|
-
},
|
431
|
-
|
432
|
-
ReferencedIdentifier(child) {
|
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
|
-
|
447
|
-
argumentsPaths.push(child);
|
448
|
-
},
|
449
|
-
|
450
|
-
MetaProperty(child) {
|
451
|
-
if (!child.get("meta").isIdentifier({
|
452
|
-
name: "new"
|
453
|
-
})) return;
|
454
|
-
if (!child.get("property").isIdentifier({
|
455
|
-
name: "target"
|
456
|
-
})) return;
|
457
|
-
newTargetPaths.push(child);
|
458
|
-
}
|
459
|
-
|
493
|
+
fnPath.traverse(getScopeInformationVisitor, {
|
494
|
+
thisPaths,
|
495
|
+
argumentsPaths,
|
496
|
+
newTargetPaths,
|
497
|
+
superProps,
|
498
|
+
superCalls
|
460
499
|
});
|
461
500
|
return {
|
462
501
|
thisPaths,
|
package/lib/scope/index.js
CHANGED
@@ -467,7 +467,7 @@ class Scope {
|
|
467
467
|
checkBlockScopedCollisions(local, kind, name, id) {
|
468
468
|
if (kind === "param") return;
|
469
469
|
if (local.kind === "local") return;
|
470
|
-
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" &&
|
470
|
+
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && kind === "const";
|
471
471
|
|
472
472
|
if (duplicate) {
|
473
473
|
throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
|
@@ -575,6 +575,7 @@ class Scope {
|
|
575
575
|
this.registerBinding(path.node.kind, declar);
|
576
576
|
}
|
577
577
|
} else if (path.isClassDeclaration()) {
|
578
|
+
if (path.node.declare) return;
|
578
579
|
this.registerBinding("let", path);
|
579
580
|
} else if (path.isImportDeclaration()) {
|
580
581
|
const specifiers = path.get("specifiers");
|
@@ -940,6 +941,8 @@ class Scope {
|
|
940
941
|
if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {} else {
|
941
942
|
return binding;
|
942
943
|
}
|
944
|
+
} else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
945
|
+
break;
|
943
946
|
}
|
944
947
|
|
945
948
|
previousPath = scope.path;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.traverseNode = traverseNode;
|
7
|
+
|
8
|
+
var _context = require("./context");
|
9
|
+
|
10
|
+
var _t = require("@babel/types");
|
11
|
+
|
12
|
+
const {
|
13
|
+
VISITOR_KEYS
|
14
|
+
} = _t;
|
15
|
+
|
16
|
+
function traverseNode(node, opts, scope, state, path, skipKeys) {
|
17
|
+
const keys = VISITOR_KEYS[node.type];
|
18
|
+
if (!keys) return false;
|
19
|
+
const context = new _context.default(scope, opts, state, path);
|
20
|
+
|
21
|
+
for (const key of keys) {
|
22
|
+
if (skipKeys && skipKeys[key]) continue;
|
23
|
+
|
24
|
+
if (context.visit(node, key)) {
|
25
|
+
return true;
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
return false;
|
30
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@babel/traverse",
|
3
|
-
"version": "7.16.
|
3
|
+
"version": "7.16.8",
|
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",
|
@@ -16,18 +16,19 @@
|
|
16
16
|
},
|
17
17
|
"main": "./lib/index.js",
|
18
18
|
"dependencies": {
|
19
|
-
"@babel/code-frame": "^7.16.
|
20
|
-
"@babel/generator": "^7.16.
|
21
|
-
"@babel/helper-
|
22
|
-
"@babel/helper-
|
23
|
-
"@babel/helper-
|
24
|
-
"@babel/
|
25
|
-
"@babel/
|
19
|
+
"@babel/code-frame": "^7.16.7",
|
20
|
+
"@babel/generator": "^7.16.8",
|
21
|
+
"@babel/helper-environment-visitor": "^7.16.7",
|
22
|
+
"@babel/helper-function-name": "^7.16.7",
|
23
|
+
"@babel/helper-hoist-variables": "^7.16.7",
|
24
|
+
"@babel/helper-split-export-declaration": "^7.16.7",
|
25
|
+
"@babel/parser": "^7.16.8",
|
26
|
+
"@babel/types": "^7.16.8",
|
26
27
|
"debug": "^4.1.0",
|
27
28
|
"globals": "^11.1.0"
|
28
29
|
},
|
29
30
|
"devDependencies": {
|
30
|
-
"@babel/helper-plugin-test-runner": "^7.16.
|
31
|
+
"@babel/helper-plugin-test-runner": "^7.16.7"
|
31
32
|
},
|
32
33
|
"engines": {
|
33
34
|
"node": ">=6.9.0"
|
@@ -1,6 +1,5 @@
|
|
1
1
|
import t from "@babel/types";
|
2
2
|
import virtualTypes from "../../lib/path/lib/virtual-types.js";
|
3
|
-
import definitions from "@babel/types/lib/definitions/index.js";
|
4
3
|
|
5
4
|
export default function generateValidators() {
|
6
5
|
let output = `/*
|
@@ -21,7 +20,7 @@ export interface NodePathValidators {
|
|
21
20
|
for (const type of Object.keys(virtualTypes)) {
|
22
21
|
const { types } = virtualTypes[type];
|
23
22
|
if (type[0] === "_") continue;
|
24
|
-
if (
|
23
|
+
if (t.NODE_FIELDS[type] || t.FLIPPED_ALIAS_KEYS[type]) {
|
25
24
|
output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
|
26
25
|
} else if (types /* in VirtualTypeAliases */) {
|
27
26
|
output += `is${type}(opts?: object): this is NodePath<VirtualTypeAliases["${type}"]>;`;
|