@babel/traverse 8.0.0-alpha.12 → 8.0.0-alpha.14
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.
- package/lib/index.d.ts +24 -94
- package/lib/index.js +340 -576
- package/lib/index.js.map +1 -1
- package/package.json +10 -10
package/lib/index.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
import * as _t from '@babel/types';
|
2
1
|
import buildDebug from 'debug';
|
3
|
-
import globals from 'globals';
|
4
|
-
import generator from '@babel/generator';
|
5
2
|
import { codeFrameColumns } from '@babel/code-frame';
|
3
|
+
import * as _t from '@babel/types';
|
6
4
|
import { parse } from '@babel/parser';
|
5
|
+
import globals from 'globals';
|
6
|
+
import generator from '@babel/generator';
|
7
7
|
import template from '@babel/template';
|
8
8
|
|
9
9
|
const ReferencedIdentifier = ["Identifier", "JSXIdentifier"];
|
@@ -67,12 +67,12 @@ const {
|
|
67
67
|
isStatement: nodeIsStatement,
|
68
68
|
isVar: nodeIsVar,
|
69
69
|
isVariableDeclaration: isVariableDeclaration$2,
|
70
|
-
react
|
70
|
+
react,
|
71
71
|
isForOfStatement
|
72
72
|
} = _t;
|
73
73
|
const {
|
74
74
|
isCompatTag
|
75
|
-
} = react
|
75
|
+
} = react;
|
76
76
|
function isReferencedIdentifier(opts) {
|
77
77
|
const {
|
78
78
|
node,
|
@@ -289,7 +289,7 @@ function verify$1(visitor) {
|
|
289
289
|
}
|
290
290
|
if (shouldIgnoreKey(nodeType)) continue;
|
291
291
|
if (!TYPES.includes(nodeType)) {
|
292
|
-
throw new Error(`You gave us a visitor for the node type ${nodeType} but it's not a valid type`);
|
292
|
+
throw new Error(`You gave us a visitor for the node type ${nodeType} but it's not a valid type in @babel/traverse ${"8.0.0-alpha.14"}`);
|
293
293
|
}
|
294
294
|
const visitors = visitor[nodeType];
|
295
295
|
if (typeof visitors === "object") {
|
@@ -313,7 +313,10 @@ function validateVisitorMethods(path, val) {
|
|
313
313
|
}
|
314
314
|
}
|
315
315
|
function merge(visitors, states = [], wrapper) {
|
316
|
-
const mergedVisitor = {
|
316
|
+
const mergedVisitor = {
|
317
|
+
_verified: true,
|
318
|
+
_exploded: true
|
319
|
+
};
|
317
320
|
for (let i = 0; i < visitors.length; i++) {
|
318
321
|
const visitor = explode$1(visitors[i]);
|
319
322
|
const state = states[i];
|
@@ -332,12 +335,7 @@ function merge(visitors, states = [], wrapper) {
|
|
332
335
|
mergePair(nodeVisitor, typeVisitor);
|
333
336
|
}
|
334
337
|
}
|
335
|
-
|
336
|
-
return Object.assign({}, mergedVisitor, {
|
337
|
-
_exploded: true,
|
338
|
-
_verified: true
|
339
|
-
});
|
340
|
-
}
|
338
|
+
return mergedVisitor;
|
341
339
|
}
|
342
340
|
function wrapWithStateOrWrapper(oldVisitor, state, wrapper) {
|
343
341
|
const newVisitor = {};
|
@@ -408,13 +406,17 @@ const _environmentVisitor = {
|
|
408
406
|
if (path.isArrowFunctionExpression()) return;
|
409
407
|
path.skip();
|
410
408
|
if (path.isMethod()) {
|
411
|
-
|
409
|
+
{
|
410
|
+
path.requeueComputedKeyAndDecorators();
|
411
|
+
}
|
412
412
|
}
|
413
413
|
},
|
414
414
|
Property(path) {
|
415
415
|
if (path.isObjectProperty()) return;
|
416
416
|
path.skip();
|
417
|
-
|
417
|
+
{
|
418
|
+
path.requeueComputedKeyAndDecorators();
|
419
|
+
}
|
418
420
|
}
|
419
421
|
};
|
420
422
|
function environmentVisitor(visitor) {
|
@@ -430,41 +432,9 @@ var visitors = /*#__PURE__*/Object.freeze({
|
|
430
432
|
verify: verify$1
|
431
433
|
});
|
432
434
|
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
clearPath();
|
437
|
-
clearScope();
|
438
|
-
}
|
439
|
-
function clearPath() {
|
440
|
-
pathsCache = new WeakMap();
|
441
|
-
}
|
442
|
-
function clearScope() {
|
443
|
-
scope = new WeakMap();
|
444
|
-
}
|
445
|
-
const nullHub = Object.freeze({});
|
446
|
-
function getCachedPaths(hub, parent) {
|
447
|
-
return pathsCache.get(hub ?? nullHub)?.get(parent);
|
448
|
-
}
|
449
|
-
function getOrCreateCachedPaths(hub, parent) {
|
450
|
-
let parents = pathsCache.get(hub ?? nullHub);
|
451
|
-
if (!parents) pathsCache.set(hub ?? nullHub, parents = new WeakMap());
|
452
|
-
let paths = parents.get(parent);
|
453
|
-
if (!paths) parents.set(parent, paths = new Map());
|
454
|
-
return paths;
|
455
|
-
}
|
456
|
-
|
457
|
-
var cache = /*#__PURE__*/Object.freeze({
|
458
|
-
__proto__: null,
|
459
|
-
clear: clear,
|
460
|
-
clearPath: clearPath,
|
461
|
-
clearScope: clearScope,
|
462
|
-
getCachedPaths: getCachedPaths,
|
463
|
-
getOrCreateCachedPaths: getOrCreateCachedPaths,
|
464
|
-
get path () { return pathsCache; },
|
465
|
-
get scope () { return scope; }
|
466
|
-
});
|
467
|
-
|
435
|
+
const {
|
436
|
+
getAssignmentIdentifiers: getAssignmentIdentifiers$1
|
437
|
+
} = _t;
|
468
438
|
const renameVisitor = {
|
469
439
|
ReferencedIdentifier({
|
470
440
|
node
|
@@ -477,7 +447,9 @@ const renameVisitor = {
|
|
477
447
|
if (!path.scope.bindingIdentifierEquals(state.oldName, state.binding.identifier)) {
|
478
448
|
path.skip();
|
479
449
|
if (path.isMethod()) {
|
480
|
-
|
450
|
+
{
|
451
|
+
path.requeueComputedKeyAndDecorators();
|
452
|
+
}
|
481
453
|
}
|
482
454
|
}
|
483
455
|
},
|
@@ -494,7 +466,7 @@ const renameVisitor = {
|
|
494
466
|
},
|
495
467
|
"AssignmentExpression|Declaration|VariableDeclarator"(path, state) {
|
496
468
|
if (path.isVariableDeclaration()) return;
|
497
|
-
const ids = path.isAssignmentExpression() ? path.
|
469
|
+
const ids = path.isAssignmentExpression() ? getAssignmentIdentifiers$1(path.node) : path.getOuterBindingIdentifiers();
|
498
470
|
for (const name in ids) {
|
499
471
|
if (name === state.oldName) ids[name].name = state.newName;
|
500
472
|
}
|
@@ -548,9 +520,18 @@ class Renamer {
|
|
548
520
|
}
|
549
521
|
}
|
550
522
|
const blockToTraverse = scope.block;
|
551
|
-
|
523
|
+
const skipKeys = {
|
552
524
|
discriminant: true
|
553
|
-
}
|
525
|
+
};
|
526
|
+
if (_t.isMethod(blockToTraverse)) {
|
527
|
+
if (blockToTraverse.computed) {
|
528
|
+
skipKeys.key = true;
|
529
|
+
}
|
530
|
+
if (!_t.isObjectMethod(blockToTraverse)) {
|
531
|
+
skipKeys.decorators = true;
|
532
|
+
}
|
533
|
+
}
|
534
|
+
traverseNode(blockToTraverse, explode$1(renameVisitor), scope, this, scope.path, skipKeys);
|
554
535
|
{
|
555
536
|
scope.removeOwnBinding(oldName);
|
556
537
|
scope.bindings[newName] = binding;
|
@@ -626,10 +607,10 @@ function isDeclaredInLoop(path) {
|
|
626
607
|
for (let {
|
627
608
|
parentPath,
|
628
609
|
key
|
629
|
-
} = path; parentPath;
|
610
|
+
} = path; parentPath; {
|
630
611
|
parentPath,
|
631
612
|
key
|
632
|
-
} = parentPath)
|
613
|
+
} = parentPath) {
|
633
614
|
if (parentPath.isFunctionParent()) return false;
|
634
615
|
if (parentPath.isWhile() || parentPath.isForXStatement() || parentPath.isForStatement() && key === "body") {
|
635
616
|
return true;
|
@@ -638,11 +619,46 @@ function isDeclaredInLoop(path) {
|
|
638
619
|
return false;
|
639
620
|
}
|
640
621
|
|
622
|
+
let pathsCache = new WeakMap();
|
623
|
+
let scope = new WeakMap();
|
624
|
+
function clear() {
|
625
|
+
clearPath();
|
626
|
+
clearScope();
|
627
|
+
}
|
628
|
+
function clearPath() {
|
629
|
+
pathsCache = new WeakMap();
|
630
|
+
}
|
631
|
+
function clearScope() {
|
632
|
+
scope = new WeakMap();
|
633
|
+
}
|
634
|
+
const nullHub = Object.freeze({});
|
635
|
+
function getCachedPaths(hub, parent) {
|
636
|
+
return pathsCache.get(hub ?? nullHub)?.get(parent);
|
637
|
+
}
|
638
|
+
function getOrCreateCachedPaths(hub, parent) {
|
639
|
+
let parents = pathsCache.get(hub ?? nullHub);
|
640
|
+
if (!parents) pathsCache.set(hub ?? nullHub, parents = new WeakMap());
|
641
|
+
let paths = parents.get(parent);
|
642
|
+
if (!paths) parents.set(parent, paths = new Map());
|
643
|
+
return paths;
|
644
|
+
}
|
645
|
+
|
646
|
+
var cache = /*#__PURE__*/Object.freeze({
|
647
|
+
__proto__: null,
|
648
|
+
clear: clear,
|
649
|
+
clearPath: clearPath,
|
650
|
+
clearScope: clearScope,
|
651
|
+
getCachedPaths: getCachedPaths,
|
652
|
+
getOrCreateCachedPaths: getOrCreateCachedPaths,
|
653
|
+
get path () { return pathsCache; },
|
654
|
+
get scope () { return scope; }
|
655
|
+
});
|
656
|
+
|
641
657
|
const {
|
642
658
|
NOT_LOCAL_BINDING,
|
643
659
|
assignmentExpression: assignmentExpression$3,
|
644
660
|
callExpression: callExpression$3,
|
645
|
-
cloneNode: cloneNode$
|
661
|
+
cloneNode: cloneNode$3,
|
646
662
|
getBindingIdentifiers: getBindingIdentifiers$3,
|
647
663
|
identifier: identifier$3,
|
648
664
|
isArrayExpression,
|
@@ -677,8 +693,8 @@ const {
|
|
677
693
|
memberExpression: memberExpression$1,
|
678
694
|
numericLiteral: numericLiteral$2,
|
679
695
|
toIdentifier,
|
680
|
-
variableDeclaration: variableDeclaration$
|
681
|
-
variableDeclarator: variableDeclarator$
|
696
|
+
variableDeclaration: variableDeclaration$1,
|
697
|
+
variableDeclarator: variableDeclarator$1,
|
682
698
|
isRecordExpression,
|
683
699
|
isTupleExpression,
|
684
700
|
isObjectProperty,
|
@@ -896,12 +912,12 @@ const collectorVisitor = {
|
|
896
912
|
for (const param of params) {
|
897
913
|
path.scope.registerBinding("param", param);
|
898
914
|
}
|
899
|
-
if (path.isFunctionExpression() && path.
|
915
|
+
if (path.isFunctionExpression() && path.node.id && !path.node.id[NOT_LOCAL_BINDING]) {
|
900
916
|
path.scope.registerBinding("local", path.get("id"), path);
|
901
917
|
}
|
902
918
|
},
|
903
919
|
ClassExpression(path) {
|
904
|
-
if (path.
|
920
|
+
if (path.node.id && !path.node.id[NOT_LOCAL_BINDING]) {
|
905
921
|
path.scope.registerBinding("local", path.get("id"), path);
|
906
922
|
}
|
907
923
|
},
|
@@ -914,8 +930,8 @@ class Scope {
|
|
914
930
|
uid;
|
915
931
|
path;
|
916
932
|
block;
|
917
|
-
labels;
|
918
933
|
inited;
|
934
|
+
labels;
|
919
935
|
bindings;
|
920
936
|
references;
|
921
937
|
globals;
|
@@ -950,31 +966,23 @@ class Scope {
|
|
950
966
|
} while (path && !parent);
|
951
967
|
return parent?.scope;
|
952
968
|
}
|
953
|
-
get parentBlock() {
|
954
|
-
return this.path.parent;
|
955
|
-
}
|
956
|
-
get hub() {
|
957
|
-
return this.path.hub;
|
958
|
-
}
|
959
|
-
traverse(node, opts, state) {
|
960
|
-
traverse(node, opts, this, state, this.path);
|
961
|
-
}
|
962
969
|
generateDeclaredUidIdentifier(name) {
|
963
970
|
const id = this.generateUidIdentifier(name);
|
964
971
|
this.push({
|
965
972
|
id
|
966
973
|
});
|
967
|
-
return cloneNode$
|
974
|
+
return cloneNode$3(id);
|
968
975
|
}
|
969
976
|
generateUidIdentifier(name) {
|
970
977
|
return identifier$3(this.generateUid(name));
|
971
978
|
}
|
972
979
|
generateUid(name = "temp") {
|
973
|
-
name = toIdentifier(name).replace(/^_+/, "").replace(
|
980
|
+
name = toIdentifier(name).replace(/^_+/, "").replace(/\d+$/g, "");
|
974
981
|
let uid;
|
975
982
|
let i = 1;
|
976
983
|
do {
|
977
|
-
uid =
|
984
|
+
uid = `_${name}`;
|
985
|
+
if (i > 1) uid += i;
|
978
986
|
i++;
|
979
987
|
} while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
|
980
988
|
const program = this.getProgramParent();
|
@@ -982,11 +990,6 @@ class Scope {
|
|
982
990
|
program.uids[uid] = true;
|
983
991
|
return uid;
|
984
992
|
}
|
985
|
-
_generateUid(name, i) {
|
986
|
-
let id = name;
|
987
|
-
if (i > 1) id += i;
|
988
|
-
return `_${id}`;
|
989
|
-
}
|
990
993
|
generateUidBasedOnNode(node, defaultName) {
|
991
994
|
const parts = [];
|
992
995
|
gatherNodeParts(node, parts);
|
@@ -1020,7 +1023,7 @@ class Scope {
|
|
1020
1023
|
this.push({
|
1021
1024
|
id
|
1022
1025
|
});
|
1023
|
-
return cloneNode$
|
1026
|
+
return cloneNode$3(id);
|
1024
1027
|
}
|
1025
1028
|
return id;
|
1026
1029
|
}
|
@@ -1030,7 +1033,7 @@ class Scope {
|
|
1030
1033
|
if (local.kind === "local") return;
|
1031
1034
|
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && kind === "const";
|
1032
1035
|
if (duplicate) {
|
1033
|
-
throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
|
1036
|
+
throw this.path.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
|
1034
1037
|
}
|
1035
1038
|
}
|
1036
1039
|
rename(oldName, newName) {
|
@@ -1043,12 +1046,6 @@ class Scope {
|
|
1043
1046
|
}
|
1044
1047
|
}
|
1045
1048
|
}
|
1046
|
-
_renameFromMap(map, oldName, newName, value) {
|
1047
|
-
if (map[oldName]) {
|
1048
|
-
map[newName] = value;
|
1049
|
-
map[oldName] = null;
|
1050
|
-
}
|
1051
|
-
}
|
1052
1049
|
dump() {
|
1053
1050
|
const sep = "-".repeat(60);
|
1054
1051
|
console.log(sep);
|
@@ -1067,37 +1064,6 @@ class Scope {
|
|
1067
1064
|
} while (scope = scope.parent);
|
1068
1065
|
console.log(sep);
|
1069
1066
|
}
|
1070
|
-
toArray(node, i, arrayLikeIsIterable) {
|
1071
|
-
if (isIdentifier$5(node)) {
|
1072
|
-
const binding = this.getBinding(node.name);
|
1073
|
-
if (binding?.constant && binding.path.isGenericType("Array")) {
|
1074
|
-
return node;
|
1075
|
-
}
|
1076
|
-
}
|
1077
|
-
if (isArrayExpression(node)) {
|
1078
|
-
return node;
|
1079
|
-
}
|
1080
|
-
if (isIdentifier$5(node, {
|
1081
|
-
name: "arguments"
|
1082
|
-
})) {
|
1083
|
-
return callExpression$3(memberExpression$1(memberExpression$1(memberExpression$1(identifier$3("Array"), identifier$3("prototype")), identifier$3("slice")), identifier$3("call")), [node]);
|
1084
|
-
}
|
1085
|
-
let helperName;
|
1086
|
-
const args = [node];
|
1087
|
-
if (i === true) {
|
1088
|
-
helperName = "toConsumableArray";
|
1089
|
-
} else if (typeof i === "number") {
|
1090
|
-
args.push(numericLiteral$2(i));
|
1091
|
-
helperName = "slicedToArray";
|
1092
|
-
} else {
|
1093
|
-
helperName = "toArray";
|
1094
|
-
}
|
1095
|
-
if (arrayLikeIsIterable) {
|
1096
|
-
args.unshift(this.hub.addHelper(helperName));
|
1097
|
-
helperName = "maybeArrayLike";
|
1098
|
-
}
|
1099
|
-
return callExpression$3(this.hub.addHelper(helperName), args);
|
1100
|
-
}
|
1101
1067
|
hasLabel(name) {
|
1102
1068
|
return !!this.getLabel(name);
|
1103
1069
|
}
|
@@ -1303,8 +1269,14 @@ class Scope {
|
|
1303
1269
|
this.globals = Object.create(null);
|
1304
1270
|
this.uids = Object.create(null);
|
1305
1271
|
this.data = Object.create(null);
|
1306
|
-
|
1307
|
-
|
1272
|
+
let scope = this;
|
1273
|
+
do {
|
1274
|
+
if (scope.crawling) return;
|
1275
|
+
if (scope.path.isProgram()) {
|
1276
|
+
break;
|
1277
|
+
}
|
1278
|
+
} while (scope = scope.parent);
|
1279
|
+
const programParent = scope;
|
1308
1280
|
const state = {
|
1309
1281
|
references: [],
|
1310
1282
|
constantViolations: [],
|
@@ -1375,12 +1347,12 @@ class Scope {
|
|
1375
1347
|
const dataKey = `declaration:${kind}:${blockHoist}`;
|
1376
1348
|
let declarPath = !unique && path.getData(dataKey);
|
1377
1349
|
if (!declarPath) {
|
1378
|
-
const declar = variableDeclaration$
|
1350
|
+
const declar = variableDeclaration$1(kind, []);
|
1379
1351
|
declar._blockHoist = blockHoist;
|
1380
1352
|
[declarPath] = path.unshiftContainer("body", [declar]);
|
1381
1353
|
if (!unique) path.setData(dataKey, declarPath);
|
1382
1354
|
}
|
1383
|
-
const declarator = variableDeclarator$
|
1355
|
+
const declarator = variableDeclarator$1(id, init);
|
1384
1356
|
const len = declarPath.node.declarations.push(declarator);
|
1385
1357
|
path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
|
1386
1358
|
}
|
@@ -1433,20 +1405,6 @@ class Scope {
|
|
1433
1405
|
} while (scope);
|
1434
1406
|
return ids;
|
1435
1407
|
}
|
1436
|
-
getAllBindingsOfKind(...kinds) {
|
1437
|
-
const ids = Object.create(null);
|
1438
|
-
for (const kind of kinds) {
|
1439
|
-
let scope = this;
|
1440
|
-
do {
|
1441
|
-
for (const name of Object.keys(scope.bindings)) {
|
1442
|
-
const binding = scope.bindings[name];
|
1443
|
-
if (binding.kind === kind) ids[name] = binding;
|
1444
|
-
}
|
1445
|
-
scope = scope.parent;
|
1446
|
-
} while (scope);
|
1447
|
-
}
|
1448
|
-
return ids;
|
1449
|
-
}
|
1450
1408
|
bindingIdentifierEquals(name, node) {
|
1451
1409
|
return this.getBindingIdentifier(name) === node;
|
1452
1410
|
}
|
@@ -2191,167 +2149,6 @@ function isGenericType(genericName) {
|
|
2191
2149
|
});
|
2192
2150
|
}
|
2193
2151
|
|
2194
|
-
const {
|
2195
|
-
react
|
2196
|
-
} = _t;
|
2197
|
-
const {
|
2198
|
-
cloneNode: cloneNode$3,
|
2199
|
-
jsxExpressionContainer,
|
2200
|
-
variableDeclaration: variableDeclaration$1,
|
2201
|
-
variableDeclarator: variableDeclarator$1
|
2202
|
-
} = _t;
|
2203
|
-
const referenceVisitor = {
|
2204
|
-
ReferencedIdentifier(path, state) {
|
2205
|
-
if (path.isJSXIdentifier() && react.isCompatTag(path.node.name) && !path.parentPath.isJSXMemberExpression()) {
|
2206
|
-
return;
|
2207
|
-
}
|
2208
|
-
if (path.node.name === "this") {
|
2209
|
-
let scope = path.scope;
|
2210
|
-
do {
|
2211
|
-
if (scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
2212
|
-
break;
|
2213
|
-
}
|
2214
|
-
} while (scope = scope.parent);
|
2215
|
-
if (scope) state.breakOnScopePaths.push(scope.path);
|
2216
|
-
}
|
2217
|
-
const binding = path.scope.getBinding(path.node.name);
|
2218
|
-
if (!binding) return;
|
2219
|
-
for (const violation of binding.constantViolations) {
|
2220
|
-
if (violation.scope !== binding.path.scope) {
|
2221
|
-
state.mutableBinding = true;
|
2222
|
-
path.stop();
|
2223
|
-
return;
|
2224
|
-
}
|
2225
|
-
}
|
2226
|
-
if (binding !== state.scope.getBinding(path.node.name)) return;
|
2227
|
-
state.bindings[path.node.name] = binding;
|
2228
|
-
}
|
2229
|
-
};
|
2230
|
-
class PathHoister {
|
2231
|
-
breakOnScopePaths;
|
2232
|
-
bindings;
|
2233
|
-
mutableBinding;
|
2234
|
-
scopes;
|
2235
|
-
scope;
|
2236
|
-
path;
|
2237
|
-
attachAfter;
|
2238
|
-
constructor(path, scope) {
|
2239
|
-
this.breakOnScopePaths = [];
|
2240
|
-
this.bindings = {};
|
2241
|
-
this.mutableBinding = false;
|
2242
|
-
this.scopes = [];
|
2243
|
-
this.scope = scope;
|
2244
|
-
this.path = path;
|
2245
|
-
this.attachAfter = false;
|
2246
|
-
}
|
2247
|
-
isCompatibleScope(scope) {
|
2248
|
-
for (const key of Object.keys(this.bindings)) {
|
2249
|
-
const binding = this.bindings[key];
|
2250
|
-
if (!scope.bindingIdentifierEquals(key, binding.identifier)) {
|
2251
|
-
return false;
|
2252
|
-
}
|
2253
|
-
}
|
2254
|
-
return true;
|
2255
|
-
}
|
2256
|
-
getCompatibleScopes() {
|
2257
|
-
let scope = this.path.scope;
|
2258
|
-
do {
|
2259
|
-
if (this.isCompatibleScope(scope)) {
|
2260
|
-
this.scopes.push(scope);
|
2261
|
-
} else {
|
2262
|
-
break;
|
2263
|
-
}
|
2264
|
-
if (this.breakOnScopePaths.includes(scope.path)) {
|
2265
|
-
break;
|
2266
|
-
}
|
2267
|
-
} while (scope = scope.parent);
|
2268
|
-
}
|
2269
|
-
getAttachmentPath() {
|
2270
|
-
let path = this._getAttachmentPath();
|
2271
|
-
if (!path) return;
|
2272
|
-
let targetScope = path.scope;
|
2273
|
-
if (targetScope.path === path) {
|
2274
|
-
targetScope = path.scope.parent;
|
2275
|
-
}
|
2276
|
-
if (targetScope.path.isProgram() || targetScope.path.isFunction()) {
|
2277
|
-
for (const name of Object.keys(this.bindings)) {
|
2278
|
-
if (!targetScope.hasOwnBinding(name)) continue;
|
2279
|
-
const binding = this.bindings[name];
|
2280
|
-
if (binding.kind === "param" || binding.path.parentKey === "params") {
|
2281
|
-
continue;
|
2282
|
-
}
|
2283
|
-
const bindingParentPath = this.getAttachmentParentForPath(binding.path);
|
2284
|
-
if (bindingParentPath.key >= path.key) {
|
2285
|
-
this.attachAfter = true;
|
2286
|
-
path = binding.path;
|
2287
|
-
for (const violationPath of binding.constantViolations) {
|
2288
|
-
if (this.getAttachmentParentForPath(violationPath).key > path.key) {
|
2289
|
-
path = violationPath;
|
2290
|
-
}
|
2291
|
-
}
|
2292
|
-
}
|
2293
|
-
}
|
2294
|
-
}
|
2295
|
-
return path;
|
2296
|
-
}
|
2297
|
-
_getAttachmentPath() {
|
2298
|
-
const scopes = this.scopes;
|
2299
|
-
const scope = scopes.pop();
|
2300
|
-
if (!scope) return;
|
2301
|
-
if (scope.path.isFunction()) {
|
2302
|
-
if (this.hasOwnParamBindings(scope)) {
|
2303
|
-
if (this.scope === scope) return;
|
2304
|
-
const bodies = scope.path.get("body").get("body");
|
2305
|
-
for (let i = 0; i < bodies.length; i++) {
|
2306
|
-
if (bodies[i].node._blockHoist) continue;
|
2307
|
-
return bodies[i];
|
2308
|
-
}
|
2309
|
-
} else {
|
2310
|
-
return this.getNextScopeAttachmentParent();
|
2311
|
-
}
|
2312
|
-
} else if (scope.path.isProgram()) {
|
2313
|
-
return this.getNextScopeAttachmentParent();
|
2314
|
-
}
|
2315
|
-
}
|
2316
|
-
getNextScopeAttachmentParent() {
|
2317
|
-
const scope = this.scopes.pop();
|
2318
|
-
if (scope) return this.getAttachmentParentForPath(scope.path);
|
2319
|
-
}
|
2320
|
-
getAttachmentParentForPath(path) {
|
2321
|
-
do {
|
2322
|
-
if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
|
2323
|
-
return path;
|
2324
|
-
}
|
2325
|
-
} while (path = path.parentPath);
|
2326
|
-
}
|
2327
|
-
hasOwnParamBindings(scope) {
|
2328
|
-
for (const name of Object.keys(this.bindings)) {
|
2329
|
-
if (!scope.hasOwnBinding(name)) continue;
|
2330
|
-
const binding = this.bindings[name];
|
2331
|
-
if (binding.kind === "param" && binding.constant) return true;
|
2332
|
-
}
|
2333
|
-
return false;
|
2334
|
-
}
|
2335
|
-
run() {
|
2336
|
-
this.path.traverse(referenceVisitor, this);
|
2337
|
-
if (this.mutableBinding) return;
|
2338
|
-
this.getCompatibleScopes();
|
2339
|
-
const attachTo = this.getAttachmentPath();
|
2340
|
-
if (!attachTo) return;
|
2341
|
-
if (attachTo.getFunctionParent() === this.path.getFunctionParent()) return;
|
2342
|
-
let uid = attachTo.scope.generateUidIdentifier("ref");
|
2343
|
-
const declarator = variableDeclarator$1(uid, this.path.node);
|
2344
|
-
const insertFn = this.attachAfter ? "insertAfter" : "insertBefore";
|
2345
|
-
const [attached] = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : variableDeclaration$1("var", [declarator])]);
|
2346
|
-
const parent = this.path.parentPath;
|
2347
|
-
if (parent.isJSXElement() && this.path.container === parent.node.children) {
|
2348
|
-
uid = jsxExpressionContainer(uid);
|
2349
|
-
}
|
2350
|
-
this.path.replaceWith(cloneNode$3(uid));
|
2351
|
-
return attachTo.isVariableDeclarator() ? attached.get("init") : attached.get("declarations.0.init");
|
2352
|
-
}
|
2353
|
-
}
|
2354
|
-
|
2355
2152
|
const hooks = [function (self, parent) {
|
2356
2153
|
const removeParent = self.key === "test" && (parent.isWhile() || parent.isSwitchCase()) || self.key === "declaration" && parent.isExportDeclaration() || self.key === "body" && parent.isLabeledStatement() || self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1 || self.key === "expression" && parent.isExpressionStatement();
|
2357
2154
|
if (removeParent) {
|
@@ -2387,247 +2184,48 @@ const {
|
|
2387
2184
|
} = _t;
|
2388
2185
|
function remove() {
|
2389
2186
|
_assertUnremoved.call(this);
|
2390
|
-
|
2391
|
-
if (!this.opts?.noScope) {
|
2392
|
-
_removeFromScope.call(this);
|
2393
|
-
}
|
2187
|
+
resync.call(this);
|
2394
2188
|
if (_callRemovalHooks.call(this)) {
|
2395
2189
|
_markRemoved.call(this);
|
2396
2190
|
return;
|
2397
2191
|
}
|
2398
|
-
this.
|
2399
|
-
|
2400
|
-
|
2401
|
-
|
2402
|
-
|
2403
|
-
|
2404
|
-
Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
|
2405
|
-
}
|
2406
|
-
function _callRemovalHooks() {
|
2407
|
-
if (this.parentPath) {
|
2408
|
-
for (const fn of hooks) {
|
2409
|
-
if (fn(this, this.parentPath)) return true;
|
2410
|
-
}
|
2411
|
-
}
|
2412
|
-
}
|
2413
|
-
function _remove() {
|
2414
|
-
if (Array.isArray(this.container)) {
|
2415
|
-
this.container.splice(this.key, 1);
|
2416
|
-
this.updateSiblingKeys(this.key, -1);
|
2417
|
-
} else {
|
2418
|
-
_replaceWith.call(this, null);
|
2419
|
-
}
|
2420
|
-
}
|
2421
|
-
function _markRemoved() {
|
2422
|
-
this._traverseFlags |= SHOULD_SKIP | REMOVED;
|
2423
|
-
if (this.parent) {
|
2424
|
-
getCachedPaths(this.hub, this.parent).delete(this.node);
|
2425
|
-
}
|
2426
|
-
this.node = null;
|
2427
|
-
}
|
2428
|
-
function _assertUnremoved() {
|
2429
|
-
if (this.removed) {
|
2430
|
-
throw this.buildCodeFrameError("NodePath has been removed so is read-only.");
|
2431
|
-
}
|
2432
|
-
}
|
2433
|
-
|
2434
|
-
function call(key) {
|
2435
|
-
const opts = this.opts;
|
2436
|
-
this.debug(key);
|
2437
|
-
if (this.node) {
|
2438
|
-
if (_call.call(this, opts[key])) return true;
|
2439
|
-
}
|
2440
|
-
if (this.node) {
|
2441
|
-
return _call.call(this, opts[this.node.type]?.[key]);
|
2442
|
-
}
|
2443
|
-
return false;
|
2444
|
-
}
|
2445
|
-
function _call(fns) {
|
2446
|
-
if (!fns) return false;
|
2447
|
-
for (const fn of fns) {
|
2448
|
-
if (!fn) continue;
|
2449
|
-
const node = this.node;
|
2450
|
-
if (!node) return true;
|
2451
|
-
const ret = fn.call(this.state, this, this.state);
|
2452
|
-
if (ret && typeof ret === "object" && typeof ret.then === "function") {
|
2453
|
-
throw new Error(`You appear to be using a plugin with an async traversal visitor, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, you may need to upgrade ` + `your @babel/core version.`);
|
2454
|
-
}
|
2455
|
-
if (ret) {
|
2456
|
-
throw new Error(`Unexpected return value from visitor method ${fn}`);
|
2457
|
-
}
|
2458
|
-
if (this.node !== node) return true;
|
2459
|
-
if (this._traverseFlags > 0) return true;
|
2460
|
-
}
|
2461
|
-
return false;
|
2462
|
-
}
|
2463
|
-
function isDenylisted() {
|
2464
|
-
const denylist = this.opts.denylist ?? this.opts.blacklist;
|
2465
|
-
return denylist && denylist.indexOf(this.node.type) > -1;
|
2466
|
-
}
|
2467
|
-
function restoreContext(path, context) {
|
2468
|
-
if (path.context !== context) {
|
2469
|
-
path.context = context;
|
2470
|
-
path.state = context.state;
|
2471
|
-
path.opts = context.opts;
|
2472
|
-
}
|
2473
|
-
}
|
2474
|
-
function visit() {
|
2475
|
-
if (!this.node) {
|
2476
|
-
return false;
|
2477
|
-
}
|
2478
|
-
if (this.isDenylisted()) {
|
2479
|
-
return false;
|
2480
|
-
}
|
2481
|
-
if (this.opts.shouldSkip?.(this)) {
|
2482
|
-
return false;
|
2483
|
-
}
|
2484
|
-
const currentContext = this.context;
|
2485
|
-
if (this.shouldSkip || this.call("enter")) {
|
2486
|
-
this.debug("Skip...");
|
2487
|
-
return this.shouldStop;
|
2488
|
-
}
|
2489
|
-
restoreContext(this, currentContext);
|
2490
|
-
this.debug("Recursing into...");
|
2491
|
-
this.shouldStop = traverseNode(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
|
2492
|
-
restoreContext(this, currentContext);
|
2493
|
-
this.call("exit");
|
2494
|
-
return this.shouldStop;
|
2495
|
-
}
|
2496
|
-
function skip() {
|
2497
|
-
this.shouldSkip = true;
|
2498
|
-
}
|
2499
|
-
function skipKey(key) {
|
2500
|
-
if (this.skipKeys == null) {
|
2501
|
-
this.skipKeys = {};
|
2502
|
-
}
|
2503
|
-
this.skipKeys[key] = true;
|
2504
|
-
}
|
2505
|
-
function stop() {
|
2506
|
-
this._traverseFlags |= SHOULD_SKIP | SHOULD_STOP;
|
2507
|
-
}
|
2508
|
-
function setScope() {
|
2509
|
-
if (this.opts?.noScope) return;
|
2510
|
-
let path = this.parentPath;
|
2511
|
-
if ((this.key === "key" || this.listKey === "decorators") && path.isMethod() || this.key === "discriminant" && path.isSwitchStatement()) {
|
2512
|
-
path = path.parentPath;
|
2513
|
-
}
|
2514
|
-
let target;
|
2515
|
-
while (path && !target) {
|
2516
|
-
if (path.opts?.noScope) return;
|
2517
|
-
target = path.scope;
|
2518
|
-
path = path.parentPath;
|
2519
|
-
}
|
2520
|
-
this.scope = this.getScope(target);
|
2521
|
-
this.scope?.init();
|
2522
|
-
}
|
2523
|
-
function setContext(context) {
|
2524
|
-
if (this.skipKeys != null) {
|
2525
|
-
this.skipKeys = {};
|
2526
|
-
}
|
2527
|
-
this._traverseFlags = 0;
|
2528
|
-
if (context) {
|
2529
|
-
this.context = context;
|
2530
|
-
this.state = context.state;
|
2531
|
-
this.opts = context.opts;
|
2532
|
-
}
|
2533
|
-
this.setScope();
|
2534
|
-
return this;
|
2535
|
-
}
|
2536
|
-
function resync() {
|
2537
|
-
if (this.removed) return;
|
2538
|
-
_resyncParent.call(this);
|
2539
|
-
_resyncList.call(this);
|
2540
|
-
_resyncKey.call(this);
|
2541
|
-
}
|
2542
|
-
function _resyncParent() {
|
2543
|
-
if (this.parentPath) {
|
2544
|
-
this.parent = this.parentPath.node;
|
2545
|
-
}
|
2546
|
-
}
|
2547
|
-
function _resyncKey() {
|
2548
|
-
if (!this.container) return;
|
2549
|
-
if (this.node === this.container[this.key]) {
|
2550
|
-
return;
|
2551
|
-
}
|
2552
|
-
if (Array.isArray(this.container)) {
|
2553
|
-
for (let i = 0; i < this.container.length; i++) {
|
2554
|
-
if (this.container[i] === this.node) {
|
2555
|
-
this.setKey(i);
|
2556
|
-
return;
|
2557
|
-
}
|
2558
|
-
}
|
2559
|
-
} else {
|
2560
|
-
for (const key of Object.keys(this.container)) {
|
2561
|
-
if (this.container[key] === this.node) {
|
2562
|
-
this.setKey(key);
|
2563
|
-
return;
|
2564
|
-
}
|
2565
|
-
}
|
2566
|
-
}
|
2567
|
-
this.key = null;
|
2568
|
-
}
|
2569
|
-
function _resyncList() {
|
2570
|
-
if (!this.parent || !this.inList) return;
|
2571
|
-
const newContainer = this.parent[this.listKey];
|
2572
|
-
if (this.container === newContainer) return;
|
2573
|
-
this.container = newContainer || null;
|
2574
|
-
}
|
2575
|
-
function popContext() {
|
2576
|
-
this.contexts.pop();
|
2577
|
-
if (this.contexts.length > 0) {
|
2578
|
-
this.setContext(this.contexts[this.contexts.length - 1]);
|
2579
|
-
} else {
|
2580
|
-
this.setContext(undefined);
|
2581
|
-
}
|
2582
|
-
}
|
2583
|
-
function pushContext(context) {
|
2584
|
-
this.contexts.push(context);
|
2585
|
-
this.setContext(context);
|
2586
|
-
}
|
2587
|
-
function setup(parentPath, container, listKey, key) {
|
2588
|
-
this.listKey = listKey;
|
2589
|
-
this.container = container;
|
2590
|
-
this.parentPath = parentPath || this.parentPath;
|
2591
|
-
this.setKey(key);
|
2192
|
+
if (!this.opts?.noScope) {
|
2193
|
+
_removeFromScope.call(this);
|
2194
|
+
}
|
2195
|
+
this.shareCommentsWithSiblings();
|
2196
|
+
_remove.call(this);
|
2197
|
+
_markRemoved.call(this);
|
2592
2198
|
}
|
2593
|
-
function
|
2594
|
-
this.
|
2595
|
-
|
2596
|
-
this.type = this.node?.type;
|
2199
|
+
function _removeFromScope() {
|
2200
|
+
const bindings = getBindingIdentifiers$2(this.node, false, false, true);
|
2201
|
+
Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
|
2597
2202
|
}
|
2598
|
-
function
|
2599
|
-
if (
|
2600
|
-
|
2601
|
-
|
2602
|
-
|
2603
|
-
const contexts = this.contexts;
|
2604
|
-
for (const context of contexts) {
|
2605
|
-
context.maybeQueue(pathToQueue);
|
2203
|
+
function _callRemovalHooks() {
|
2204
|
+
if (this.parentPath) {
|
2205
|
+
for (const fn of hooks) {
|
2206
|
+
if (fn(this, this.parentPath)) return true;
|
2207
|
+
}
|
2606
2208
|
}
|
2607
2209
|
}
|
2608
|
-
function
|
2609
|
-
|
2610
|
-
|
2611
|
-
|
2612
|
-
}
|
2613
|
-
|
2614
|
-
context.maybeQueue(this.get("key"));
|
2210
|
+
function _remove() {
|
2211
|
+
if (Array.isArray(this.container)) {
|
2212
|
+
this.container.splice(this.key, 1);
|
2213
|
+
updateSiblingKeys.call(this, this.key, -1);
|
2214
|
+
} else {
|
2215
|
+
_replaceWith.call(this, null);
|
2615
2216
|
}
|
2616
|
-
|
2617
|
-
|
2618
|
-
|
2619
|
-
|
2217
|
+
}
|
2218
|
+
function _markRemoved() {
|
2219
|
+
this._traverseFlags |= SHOULD_SKIP | REMOVED;
|
2220
|
+
if (this.parent) {
|
2221
|
+
getCachedPaths(this.hub, this.parent).delete(this.node);
|
2620
2222
|
}
|
2223
|
+
this.node = null;
|
2621
2224
|
}
|
2622
|
-
function
|
2623
|
-
|
2624
|
-
|
2625
|
-
while (!contexts.length) {
|
2626
|
-
path = path.parentPath;
|
2627
|
-
if (!path) break;
|
2628
|
-
contexts = path.contexts;
|
2225
|
+
function _assertUnremoved() {
|
2226
|
+
if (this.removed) {
|
2227
|
+
throw this.buildCodeFrameError("NodePath has been removed so is read-only.");
|
2629
2228
|
}
|
2630
|
-
return contexts;
|
2631
2229
|
}
|
2632
2230
|
|
2633
2231
|
const {
|
@@ -2671,7 +2269,7 @@ function insertBefore(nodes_) {
|
|
2671
2269
|
}
|
2672
2270
|
}
|
2673
2271
|
function _containerInsert(from, nodes) {
|
2674
|
-
|
2272
|
+
updateSiblingKeys.call(this, from, nodes.length);
|
2675
2273
|
const paths = [];
|
2676
2274
|
this.container.splice(from, 0, ...nodes);
|
2677
2275
|
for (let i = 0; i < nodes.length; i++) {
|
@@ -2679,12 +2277,12 @@ function _containerInsert(from, nodes) {
|
|
2679
2277
|
const path = this.getSibling(to);
|
2680
2278
|
paths.push(path);
|
2681
2279
|
if (this.context?.queue) {
|
2682
|
-
|
2280
|
+
pushContext.call(path, this.context);
|
2683
2281
|
}
|
2684
2282
|
}
|
2685
2283
|
const contexts = _getQueueContexts.call(this);
|
2686
2284
|
for (const path of paths) {
|
2687
|
-
|
2285
|
+
setScope.call(path);
|
2688
2286
|
path.debug("Inserted.");
|
2689
2287
|
for (const context of contexts) {
|
2690
2288
|
context.maybeQueue(path, true);
|
@@ -2774,7 +2372,7 @@ function updateSiblingKeys(fromIndex, incrementBy) {
|
|
2774
2372
|
if (!this.parent) return;
|
2775
2373
|
const paths = getCachedPaths(this.hub, this.parent) || [];
|
2776
2374
|
for (const [, path] of paths) {
|
2777
|
-
if (typeof path.key === "number" && path.key >= fromIndex) {
|
2375
|
+
if (typeof path.key === "number" && path.container === this.container && path.key >= fromIndex) {
|
2778
2376
|
path.key += incrementBy;
|
2779
2377
|
}
|
2780
2378
|
}
|
@@ -2830,10 +2428,6 @@ function pushContainer(listKey, nodes) {
|
|
2830
2428
|
}).setContext(this.context);
|
2831
2429
|
return path.replaceWithMultiple(verifiedNodes);
|
2832
2430
|
}
|
2833
|
-
function hoist(scope = this.scope) {
|
2834
|
-
const hoister = new PathHoister(this, scope);
|
2835
|
-
return hoister.run();
|
2836
|
-
}
|
2837
2431
|
|
2838
2432
|
const {
|
2839
2433
|
FUNCTION_TYPES,
|
@@ -2866,7 +2460,7 @@ const {
|
|
2866
2460
|
yieldExpression
|
2867
2461
|
} = _t;
|
2868
2462
|
function replaceWithMultiple(nodes) {
|
2869
|
-
|
2463
|
+
resync.call(this);
|
2870
2464
|
nodes = _verifyNodeList.call(this, nodes);
|
2871
2465
|
inheritLeadingComments(nodes[0], this.node);
|
2872
2466
|
inheritTrailingComments(nodes[nodes.length - 1], this.node);
|
@@ -2881,7 +2475,7 @@ function replaceWithMultiple(nodes) {
|
|
2881
2475
|
return paths;
|
2882
2476
|
}
|
2883
2477
|
function replaceWithSourceString(replacement) {
|
2884
|
-
|
2478
|
+
resync.call(this);
|
2885
2479
|
let ast;
|
2886
2480
|
try {
|
2887
2481
|
replacement = `(${replacement})`;
|
@@ -2904,7 +2498,7 @@ function replaceWithSourceString(replacement) {
|
|
2904
2498
|
return this.replaceWith(expressionAST);
|
2905
2499
|
}
|
2906
2500
|
function replaceWith(replacementPath) {
|
2907
|
-
|
2501
|
+
resync.call(this);
|
2908
2502
|
if (this.removed) {
|
2909
2503
|
throw new Error("You can't replace this node, we've already removed it");
|
2910
2504
|
}
|
@@ -2943,7 +2537,7 @@ function replaceWith(replacementPath) {
|
|
2943
2537
|
}
|
2944
2538
|
_replaceWith.call(this, replacement);
|
2945
2539
|
this.type = replacement.type;
|
2946
|
-
|
2540
|
+
setScope.call(this);
|
2947
2541
|
this.requeue();
|
2948
2542
|
return [nodePath ? this.get(nodePath) : this];
|
2949
2543
|
}
|
@@ -2961,7 +2555,7 @@ function _replaceWith(node) {
|
|
2961
2555
|
this.node = this.container[this.key] = node;
|
2962
2556
|
}
|
2963
2557
|
function replaceExpressionWithStatements(nodes) {
|
2964
|
-
|
2558
|
+
resync.call(this);
|
2965
2559
|
const declars = [];
|
2966
2560
|
const nodesAsSingleExpression = gatherSequenceExpressions(nodes, declars);
|
2967
2561
|
if (nodesAsSingleExpression) {
|
@@ -2971,8 +2565,8 @@ function replaceExpressionWithStatements(nodes) {
|
|
2971
2565
|
return this.replaceWith(nodesAsSingleExpression)[0].get("expressions");
|
2972
2566
|
}
|
2973
2567
|
const functionParent = this.getFunctionParent();
|
2974
|
-
const isParentAsync = functionParent?.
|
2975
|
-
const isParentGenerator = functionParent?.
|
2568
|
+
const isParentAsync = functionParent?.node.async;
|
2569
|
+
const isParentGenerator = functionParent?.node.generator;
|
2976
2570
|
const container = arrowFunctionExpression$1([], blockStatement$1(nodes));
|
2977
2571
|
this.replaceWith(callExpression$1(container, []));
|
2978
2572
|
const callee = this.get("callee");
|
@@ -3061,7 +2655,7 @@ function gatherSequenceExpressions(nodes, declars) {
|
|
3061
2655
|
}
|
3062
2656
|
}
|
3063
2657
|
function replaceInline(nodes) {
|
3064
|
-
|
2658
|
+
resync.call(this);
|
3065
2659
|
if (Array.isArray(nodes)) {
|
3066
2660
|
if (Array.isArray(this.container)) {
|
3067
2661
|
nodes = _verifyNodeList.call(this, nodes);
|
@@ -3499,7 +3093,7 @@ function ensureBlock() {
|
|
3499
3093
|
}
|
3500
3094
|
this.node.body = blockStatement(statements);
|
3501
3095
|
const parentPath = this.get(stringPath);
|
3502
|
-
|
3096
|
+
setup.call(body, parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key);
|
3503
3097
|
return this.node;
|
3504
3098
|
}
|
3505
3099
|
function unwrapFunctionEnvironment() {
|
@@ -4014,24 +3608,9 @@ const {
|
|
4014
3608
|
function matchesPattern(pattern, allowPartial) {
|
4015
3609
|
return _matchesPattern(this.node, pattern, allowPartial);
|
4016
3610
|
}
|
4017
|
-
function has(key) {
|
4018
|
-
const val = this.node?.[key];
|
4019
|
-
if (val && Array.isArray(val)) {
|
4020
|
-
return !!val.length;
|
4021
|
-
} else {
|
4022
|
-
return !!val;
|
4023
|
-
}
|
4024
|
-
}
|
4025
3611
|
function isStatic() {
|
4026
3612
|
return this.scope.isStatic(this.node);
|
4027
3613
|
}
|
4028
|
-
const is = has;
|
4029
|
-
function isnt(key) {
|
4030
|
-
return !this.has(key);
|
4031
|
-
}
|
4032
|
-
function equals(key, value) {
|
4033
|
-
return this.node[key] === value;
|
4034
|
-
}
|
4035
3614
|
function isNodeType(type) {
|
4036
3615
|
return isType(this.type, type);
|
4037
3616
|
}
|
@@ -4794,7 +4373,7 @@ const NodePath_Final = class NodePath {
|
|
4794
4373
|
path = new NodePath(hub, parent);
|
4795
4374
|
if (targetNode) paths.set(targetNode, path);
|
4796
4375
|
}
|
4797
|
-
|
4376
|
+
setup.call(path, parentPath, container, listKey, key);
|
4798
4377
|
return path;
|
4799
4378
|
}
|
4800
4379
|
getScope(scope) {
|
@@ -4886,11 +4465,7 @@ const methods = {
|
|
4886
4465
|
splitExportDeclaration: splitExportDeclaration,
|
4887
4466
|
ensureFunctionName: ensureFunctionName,
|
4888
4467
|
matchesPattern: matchesPattern,
|
4889
|
-
has: has,
|
4890
4468
|
isStatic: isStatic,
|
4891
|
-
is: is,
|
4892
|
-
isnt: isnt,
|
4893
|
-
equals: equals,
|
4894
4469
|
isNodeType: isNodeType,
|
4895
4470
|
canHaveVariableDeclarationOrExpression: canHaveVariableDeclarationOrExpression,
|
4896
4471
|
canSwapBetweenExpressionAndStatement: canSwapBetweenExpressionAndStatement,
|
@@ -4903,29 +4478,19 @@ const methods = {
|
|
4903
4478
|
resolve: resolve,
|
4904
4479
|
isConstantExpression: isConstantExpression,
|
4905
4480
|
isInStrictMode: isInStrictMode,
|
4906
|
-
call: call,
|
4907
4481
|
isDenylisted: isDenylisted,
|
4908
|
-
isBlacklisted: isDenylisted,
|
4909
4482
|
visit: visit,
|
4910
4483
|
skip: skip,
|
4911
4484
|
skipKey: skipKey,
|
4912
4485
|
stop: stop,
|
4913
|
-
setScope: setScope,
|
4914
4486
|
setContext: setContext,
|
4915
|
-
resync: resync,
|
4916
|
-
popContext: popContext,
|
4917
|
-
pushContext: pushContext,
|
4918
|
-
setup: setup,
|
4919
|
-
setKey: setKey,
|
4920
4487
|
requeue: requeue,
|
4921
4488
|
requeueComputedKeyAndDecorators: requeueComputedKeyAndDecorators,
|
4922
4489
|
remove: remove,
|
4923
4490
|
insertBefore: insertBefore,
|
4924
4491
|
insertAfter: insertAfter,
|
4925
|
-
updateSiblingKeys: updateSiblingKeys,
|
4926
4492
|
unshiftContainer: unshiftContainer,
|
4927
4493
|
pushContainer: pushContainer,
|
4928
|
-
hoist: hoist,
|
4929
4494
|
getOpposite: getOpposite,
|
4930
4495
|
getCompletionRecords: getCompletionRecords,
|
4931
4496
|
getSibling: getSibling,
|
@@ -5032,9 +4597,9 @@ class TraversalContext {
|
|
5032
4597
|
for (; visitIndex < queue.length;) {
|
5033
4598
|
const path = queue[visitIndex];
|
5034
4599
|
visitIndex++;
|
5035
|
-
|
4600
|
+
resync.call(path);
|
5036
4601
|
if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
|
5037
|
-
|
4602
|
+
pushContext.call(path, this);
|
5038
4603
|
}
|
5039
4604
|
if (path.key === null) continue;
|
5040
4605
|
const {
|
@@ -5054,7 +4619,7 @@ class TraversalContext {
|
|
5054
4619
|
}
|
5055
4620
|
}
|
5056
4621
|
for (let i = 0; i < visitIndex; i++) {
|
5057
|
-
queue[i]
|
4622
|
+
popContext.call(queue[i]);
|
5058
4623
|
}
|
5059
4624
|
this.queue = null;
|
5060
4625
|
return stop;
|
@@ -5090,6 +4655,205 @@ function traverseNode(node, opts, scope, state, path, skipKeys, visitSelf) {
|
|
5090
4655
|
return false;
|
5091
4656
|
}
|
5092
4657
|
|
4658
|
+
function call(key) {
|
4659
|
+
const opts = this.opts;
|
4660
|
+
this.debug(key);
|
4661
|
+
if (this.node) {
|
4662
|
+
if (_call.call(this, opts[key])) return true;
|
4663
|
+
}
|
4664
|
+
if (this.node) {
|
4665
|
+
return _call.call(this, opts[this.node.type]?.[key]);
|
4666
|
+
}
|
4667
|
+
return false;
|
4668
|
+
}
|
4669
|
+
function _call(fns) {
|
4670
|
+
if (!fns) return false;
|
4671
|
+
for (const fn of fns) {
|
4672
|
+
if (!fn) continue;
|
4673
|
+
const node = this.node;
|
4674
|
+
if (!node) return true;
|
4675
|
+
const ret = fn.call(this.state, this, this.state);
|
4676
|
+
if (ret && typeof ret === "object" && typeof ret.then === "function") {
|
4677
|
+
throw new Error(`You appear to be using a plugin with an async traversal visitor, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, you may need to upgrade ` + `your @babel/core version.`);
|
4678
|
+
}
|
4679
|
+
if (ret) {
|
4680
|
+
throw new Error(`Unexpected return value from visitor method ${fn}`);
|
4681
|
+
}
|
4682
|
+
if (this.node !== node) return true;
|
4683
|
+
if (this._traverseFlags > 0) return true;
|
4684
|
+
}
|
4685
|
+
return false;
|
4686
|
+
}
|
4687
|
+
function isDenylisted() {
|
4688
|
+
const denylist = this.opts.denylist ?? this.opts.blacklist;
|
4689
|
+
return denylist?.includes(this.node.type);
|
4690
|
+
}
|
4691
|
+
function restoreContext(path, context) {
|
4692
|
+
if (path.context !== context) {
|
4693
|
+
path.context = context;
|
4694
|
+
path.state = context.state;
|
4695
|
+
path.opts = context.opts;
|
4696
|
+
}
|
4697
|
+
}
|
4698
|
+
function visit() {
|
4699
|
+
if (!this.node) {
|
4700
|
+
return false;
|
4701
|
+
}
|
4702
|
+
if (this.isDenylisted()) {
|
4703
|
+
return false;
|
4704
|
+
}
|
4705
|
+
if (this.opts.shouldSkip?.(this)) {
|
4706
|
+
return false;
|
4707
|
+
}
|
4708
|
+
const currentContext = this.context;
|
4709
|
+
if (this.shouldSkip || call.call(this, "enter")) {
|
4710
|
+
this.debug("Skip...");
|
4711
|
+
return this.shouldStop;
|
4712
|
+
}
|
4713
|
+
restoreContext(this, currentContext);
|
4714
|
+
this.debug("Recursing into...");
|
4715
|
+
this.shouldStop = traverseNode(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
|
4716
|
+
restoreContext(this, currentContext);
|
4717
|
+
call.call(this, "exit");
|
4718
|
+
return this.shouldStop;
|
4719
|
+
}
|
4720
|
+
function skip() {
|
4721
|
+
this.shouldSkip = true;
|
4722
|
+
}
|
4723
|
+
function skipKey(key) {
|
4724
|
+
if (this.skipKeys == null) {
|
4725
|
+
this.skipKeys = {};
|
4726
|
+
}
|
4727
|
+
this.skipKeys[key] = true;
|
4728
|
+
}
|
4729
|
+
function stop() {
|
4730
|
+
this._traverseFlags |= SHOULD_SKIP | SHOULD_STOP;
|
4731
|
+
}
|
4732
|
+
function setScope() {
|
4733
|
+
if (this.opts?.noScope) return;
|
4734
|
+
let path = this.parentPath;
|
4735
|
+
if ((this.key === "key" || this.listKey === "decorators") && path.isMethod() || this.key === "discriminant" && path.isSwitchStatement()) {
|
4736
|
+
path = path.parentPath;
|
4737
|
+
}
|
4738
|
+
let target;
|
4739
|
+
while (path && !target) {
|
4740
|
+
if (path.opts?.noScope) return;
|
4741
|
+
target = path.scope;
|
4742
|
+
path = path.parentPath;
|
4743
|
+
}
|
4744
|
+
this.scope = this.getScope(target);
|
4745
|
+
this.scope?.init();
|
4746
|
+
}
|
4747
|
+
function setContext(context) {
|
4748
|
+
if (this.skipKeys != null) {
|
4749
|
+
this.skipKeys = {};
|
4750
|
+
}
|
4751
|
+
this._traverseFlags = 0;
|
4752
|
+
if (context) {
|
4753
|
+
this.context = context;
|
4754
|
+
this.state = context.state;
|
4755
|
+
this.opts = context.opts;
|
4756
|
+
}
|
4757
|
+
setScope.call(this);
|
4758
|
+
return this;
|
4759
|
+
}
|
4760
|
+
function resync() {
|
4761
|
+
if (this.removed) return;
|
4762
|
+
_resyncParent.call(this);
|
4763
|
+
_resyncList.call(this);
|
4764
|
+
_resyncKey.call(this);
|
4765
|
+
}
|
4766
|
+
function _resyncParent() {
|
4767
|
+
if (this.parentPath) {
|
4768
|
+
this.parent = this.parentPath.node;
|
4769
|
+
}
|
4770
|
+
}
|
4771
|
+
function _resyncKey() {
|
4772
|
+
if (!this.container) return;
|
4773
|
+
if (this.node === this.container[this.key]) {
|
4774
|
+
return;
|
4775
|
+
}
|
4776
|
+
if (Array.isArray(this.container)) {
|
4777
|
+
for (let i = 0; i < this.container.length; i++) {
|
4778
|
+
if (this.container[i] === this.node) {
|
4779
|
+
setKey.call(this, i);
|
4780
|
+
return;
|
4781
|
+
}
|
4782
|
+
}
|
4783
|
+
} else {
|
4784
|
+
for (const key of Object.keys(this.container)) {
|
4785
|
+
if (this.container[key] === this.node) {
|
4786
|
+
setKey.call(this, key);
|
4787
|
+
return;
|
4788
|
+
}
|
4789
|
+
}
|
4790
|
+
}
|
4791
|
+
this.key = null;
|
4792
|
+
}
|
4793
|
+
function _resyncList() {
|
4794
|
+
if (!this.parent || !this.inList) return;
|
4795
|
+
const newContainer = this.parent[this.listKey];
|
4796
|
+
if (this.container === newContainer) return;
|
4797
|
+
this.container = newContainer || null;
|
4798
|
+
}
|
4799
|
+
function popContext() {
|
4800
|
+
this.contexts.pop();
|
4801
|
+
if (this.contexts.length > 0) {
|
4802
|
+
this.setContext(this.contexts[this.contexts.length - 1]);
|
4803
|
+
} else {
|
4804
|
+
this.setContext(undefined);
|
4805
|
+
}
|
4806
|
+
}
|
4807
|
+
function pushContext(context) {
|
4808
|
+
this.contexts.push(context);
|
4809
|
+
this.setContext(context);
|
4810
|
+
}
|
4811
|
+
function setup(parentPath, container, listKey, key) {
|
4812
|
+
this.listKey = listKey;
|
4813
|
+
this.container = container;
|
4814
|
+
this.parentPath = parentPath || this.parentPath;
|
4815
|
+
setKey.call(this, key);
|
4816
|
+
}
|
4817
|
+
function setKey(key) {
|
4818
|
+
this.key = key;
|
4819
|
+
this.node = this.container[this.key];
|
4820
|
+
this.type = this.node?.type;
|
4821
|
+
}
|
4822
|
+
function requeue(pathToQueue = this) {
|
4823
|
+
if (pathToQueue.removed) return;
|
4824
|
+
{
|
4825
|
+
pathToQueue.shouldSkip = false;
|
4826
|
+
}
|
4827
|
+
const contexts = this.contexts;
|
4828
|
+
for (const context of contexts) {
|
4829
|
+
context.maybeQueue(pathToQueue);
|
4830
|
+
}
|
4831
|
+
}
|
4832
|
+
function requeueComputedKeyAndDecorators() {
|
4833
|
+
const {
|
4834
|
+
context,
|
4835
|
+
node
|
4836
|
+
} = this;
|
4837
|
+
if (!_t.isPrivate(node) && node.computed) {
|
4838
|
+
context.maybeQueue(this.get("key"));
|
4839
|
+
}
|
4840
|
+
if (node.decorators) {
|
4841
|
+
for (const decorator of this.get("decorators")) {
|
4842
|
+
context.maybeQueue(decorator);
|
4843
|
+
}
|
4844
|
+
}
|
4845
|
+
}
|
4846
|
+
function _getQueueContexts() {
|
4847
|
+
let path = this;
|
4848
|
+
let contexts = this.contexts;
|
4849
|
+
while (!contexts.length) {
|
4850
|
+
path = path.parentPath;
|
4851
|
+
if (!path) break;
|
4852
|
+
contexts = path.contexts;
|
4853
|
+
}
|
4854
|
+
return contexts;
|
4855
|
+
}
|
4856
|
+
|
5093
4857
|
class Hub {
|
5094
4858
|
getCode() {}
|
5095
4859
|
getScope() {}
|