@babel/traverse 7.19.6 → 7.20.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/cache.js +0 -3
- package/lib/cache.js.map +1 -1
- package/lib/context.js +7 -21
- package/lib/context.js.map +1 -1
- package/lib/hub.js +0 -6
- package/lib/hub.js.map +1 -1
- package/lib/index.js +3 -21
- package/lib/index.js.map +1 -1
- package/lib/path/ancestry.js +3 -23
- package/lib/path/ancestry.js.map +1 -1
- package/lib/path/comments.js +0 -4
- package/lib/path/comments.js.map +1 -1
- package/lib/path/context.js +11 -52
- package/lib/path/context.js.map +1 -1
- package/lib/path/conversion.js +16 -75
- package/lib/path/conversion.js.map +1 -1
- package/lib/path/evaluation.js +9 -85
- package/lib/path/evaluation.js.map +1 -1
- package/lib/path/family.js +6 -73
- package/lib/path/family.js.map +1 -1
- package/lib/path/index.js +2 -61
- package/lib/path/index.js.map +1 -1
- package/lib/path/inference/index.js +2 -27
- package/lib/path/inference/index.js.map +1 -1
- package/lib/path/inference/inferer-reference.js +10 -32
- package/lib/path/inference/inferer-reference.js.map +1 -1
- package/lib/path/inference/inferers.js +4 -44
- package/lib/path/inference/inferers.js.map +1 -1
- package/lib/path/inference/util.js +0 -4
- package/lib/path/inference/util.js.map +1 -1
- package/lib/path/introspection.js +17 -59
- package/lib/path/introspection.js.map +1 -1
- package/lib/path/lib/hoister.js +7 -23
- package/lib/path/lib/hoister.js.map +1 -1
- package/lib/path/lib/removal-hooks.js +7 -3
- package/lib/path/lib/removal-hooks.js.map +1 -1
- package/lib/path/lib/virtual-types-validator.js +0 -23
- package/lib/path/lib/virtual-types-validator.js.map +1 -1
- package/lib/path/lib/virtual-types.js +1 -0
- package/lib/path/lib/virtual-types.js.map +1 -1
- package/lib/path/modification.js +12 -47
- package/lib/path/modification.js.map +1 -1
- package/lib/path/removal.js +0 -16
- package/lib/path/removal.js.map +1 -1
- package/lib/path/replacement.js +8 -50
- package/lib/path/replacement.js.map +1 -1
- package/lib/scope/binding.js +2 -14
- package/lib/scope/binding.js.map +1 -1
- package/lib/scope/index.js +22 -184
- package/lib/scope/index.js.map +1 -1
- package/lib/scope/lib/renamer.js +2 -25
- package/lib/scope/lib/renamer.js.map +1 -1
- package/lib/traverse-node.js +0 -7
- package/lib/traverse-node.js.map +1 -1
- package/lib/visitors.js +13 -44
- package/lib/visitors.js.map +1 -1
- package/package.json +4 -4
- package/scripts/generators/asserts.js +0 -25
- package/scripts/generators/validators.js +0 -27
- package/scripts/package.json +0 -1
package/lib/scope/index.js
CHANGED
@@ -4,19 +4,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
|
8
7
|
var _renamer = require("./lib/renamer");
|
9
|
-
|
10
8
|
var _index = require("../index");
|
11
|
-
|
12
9
|
var _binding = require("./binding");
|
13
|
-
|
14
10
|
var _globals = require("globals");
|
15
|
-
|
16
11
|
var _t = require("@babel/types");
|
17
|
-
|
18
12
|
var _cache = require("../cache");
|
19
|
-
|
20
13
|
const {
|
21
14
|
NOT_LOCAL_BINDING,
|
22
15
|
callExpression,
|
@@ -63,7 +56,6 @@ const {
|
|
63
56
|
isMetaProperty,
|
64
57
|
isPrivateName
|
65
58
|
} = _t;
|
66
|
-
|
67
59
|
function gatherNodeParts(node, parts) {
|
68
60
|
switch (node == null ? void 0 : node.type) {
|
69
61
|
default:
|
@@ -80,40 +72,32 @@ function gatherNodeParts(node, parts) {
|
|
80
72
|
} else if (isLiteral(node) && !isNullLiteral(node) && !isRegExpLiteral(node) && !isTemplateLiteral(node)) {
|
81
73
|
parts.push(node.value);
|
82
74
|
}
|
83
|
-
|
84
75
|
break;
|
85
|
-
|
86
76
|
case "MemberExpression":
|
87
77
|
case "OptionalMemberExpression":
|
88
78
|
case "JSXMemberExpression":
|
89
79
|
gatherNodeParts(node.object, parts);
|
90
80
|
gatherNodeParts(node.property, parts);
|
91
81
|
break;
|
92
|
-
|
93
82
|
case "Identifier":
|
94
83
|
case "JSXIdentifier":
|
95
84
|
parts.push(node.name);
|
96
85
|
break;
|
97
|
-
|
98
86
|
case "CallExpression":
|
99
87
|
case "OptionalCallExpression":
|
100
88
|
case "NewExpression":
|
101
89
|
gatherNodeParts(node.callee, parts);
|
102
90
|
break;
|
103
|
-
|
104
91
|
case "ObjectExpression":
|
105
92
|
case "ObjectPattern":
|
106
93
|
for (const e of node.properties) {
|
107
94
|
gatherNodeParts(e, parts);
|
108
95
|
}
|
109
|
-
|
110
96
|
break;
|
111
|
-
|
112
97
|
case "SpreadElement":
|
113
98
|
case "RestElement":
|
114
99
|
gatherNodeParts(node.argument, parts);
|
115
100
|
break;
|
116
|
-
|
117
101
|
case "ObjectProperty":
|
118
102
|
case "ObjectMethod":
|
119
103
|
case "ClassProperty":
|
@@ -122,82 +106,64 @@ function gatherNodeParts(node, parts) {
|
|
122
106
|
case "ClassPrivateMethod":
|
123
107
|
gatherNodeParts(node.key, parts);
|
124
108
|
break;
|
125
|
-
|
126
109
|
case "ThisExpression":
|
127
110
|
parts.push("this");
|
128
111
|
break;
|
129
|
-
|
130
112
|
case "Super":
|
131
113
|
parts.push("super");
|
132
114
|
break;
|
133
|
-
|
134
115
|
case "Import":
|
135
116
|
parts.push("import");
|
136
117
|
break;
|
137
|
-
|
138
118
|
case "DoExpression":
|
139
119
|
parts.push("do");
|
140
120
|
break;
|
141
|
-
|
142
121
|
case "YieldExpression":
|
143
122
|
parts.push("yield");
|
144
123
|
gatherNodeParts(node.argument, parts);
|
145
124
|
break;
|
146
|
-
|
147
125
|
case "AwaitExpression":
|
148
126
|
parts.push("await");
|
149
127
|
gatherNodeParts(node.argument, parts);
|
150
128
|
break;
|
151
|
-
|
152
129
|
case "AssignmentExpression":
|
153
130
|
gatherNodeParts(node.left, parts);
|
154
131
|
break;
|
155
|
-
|
156
132
|
case "VariableDeclarator":
|
157
133
|
gatherNodeParts(node.id, parts);
|
158
134
|
break;
|
159
|
-
|
160
135
|
case "FunctionExpression":
|
161
136
|
case "FunctionDeclaration":
|
162
137
|
case "ClassExpression":
|
163
138
|
case "ClassDeclaration":
|
164
139
|
gatherNodeParts(node.id, parts);
|
165
140
|
break;
|
166
|
-
|
167
141
|
case "PrivateName":
|
168
142
|
gatherNodeParts(node.id, parts);
|
169
143
|
break;
|
170
|
-
|
171
144
|
case "ParenthesizedExpression":
|
172
145
|
gatherNodeParts(node.expression, parts);
|
173
146
|
break;
|
174
|
-
|
175
147
|
case "UnaryExpression":
|
176
148
|
case "UpdateExpression":
|
177
149
|
gatherNodeParts(node.argument, parts);
|
178
150
|
break;
|
179
|
-
|
180
151
|
case "MetaProperty":
|
181
152
|
gatherNodeParts(node.meta, parts);
|
182
153
|
gatherNodeParts(node.property, parts);
|
183
154
|
break;
|
184
|
-
|
185
155
|
case "JSXElement":
|
186
156
|
gatherNodeParts(node.openingElement, parts);
|
187
157
|
break;
|
188
|
-
|
189
158
|
case "JSXOpeningElement":
|
190
159
|
gatherNodeParts(node.name, parts);
|
191
160
|
break;
|
192
|
-
|
193
161
|
case "JSXFragment":
|
194
162
|
gatherNodeParts(node.openingFragment, parts);
|
195
163
|
break;
|
196
|
-
|
197
164
|
case "JSXOpeningFragment":
|
198
165
|
parts.push("Fragment");
|
199
166
|
break;
|
200
|
-
|
201
167
|
case "JSXNamespacedName":
|
202
168
|
gatherNodeParts(node.namespace, parts);
|
203
169
|
gatherNodeParts(node.name, parts);
|
@@ -208,7 +174,6 @@ function gatherNodeParts(node, parts) {
|
|
208
174
|
const collectorVisitor = {
|
209
175
|
ForStatement(path) {
|
210
176
|
const declar = path.get("init");
|
211
|
-
|
212
177
|
if (declar.isVar()) {
|
213
178
|
const {
|
214
179
|
scope
|
@@ -217,30 +182,29 @@ const collectorVisitor = {
|
|
217
182
|
parentScope.registerBinding("var", declar);
|
218
183
|
}
|
219
184
|
},
|
220
|
-
|
221
185
|
Declaration(path) {
|
222
186
|
if (path.isBlockScoped()) return;
|
187
|
+
|
223
188
|
if (path.isImportDeclaration()) return;
|
189
|
+
|
224
190
|
if (path.isExportDeclaration()) return;
|
191
|
+
|
225
192
|
const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
226
193
|
parent.registerDeclaration(path);
|
227
194
|
},
|
228
|
-
|
229
195
|
ImportDeclaration(path) {
|
230
196
|
const parent = path.scope.getBlockParent();
|
231
197
|
parent.registerDeclaration(path);
|
232
198
|
},
|
233
|
-
|
234
199
|
ReferencedIdentifier(path, state) {
|
235
200
|
state.references.push(path);
|
236
201
|
},
|
237
|
-
|
238
202
|
ForXStatement(path, state) {
|
239
203
|
const left = path.get("left");
|
240
|
-
|
241
204
|
if (left.isPattern() || left.isIdentifier()) {
|
242
205
|
state.constantViolations.push(path);
|
243
|
-
}
|
206
|
+
}
|
207
|
+
else if (left.isVar()) {
|
244
208
|
const {
|
245
209
|
scope
|
246
210
|
} = path;
|
@@ -248,7 +212,6 @@ const collectorVisitor = {
|
|
248
212
|
parentScope.registerBinding("var", left);
|
249
213
|
}
|
250
214
|
},
|
251
|
-
|
252
215
|
ExportDeclaration: {
|
253
216
|
exit(path) {
|
254
217
|
const {
|
@@ -257,7 +220,6 @@ const collectorVisitor = {
|
|
257
220
|
} = path;
|
258
221
|
if (isExportAllDeclaration(node)) return;
|
259
222
|
const declar = node.declaration;
|
260
|
-
|
261
223
|
if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
|
262
224
|
const id = declar.id;
|
263
225
|
if (!id) return;
|
@@ -272,27 +234,21 @@ const collectorVisitor = {
|
|
272
234
|
}
|
273
235
|
}
|
274
236
|
}
|
275
|
-
|
276
237
|
},
|
277
|
-
|
278
238
|
LabeledStatement(path) {
|
279
239
|
path.scope.getBlockParent().registerDeclaration(path);
|
280
240
|
},
|
281
|
-
|
282
241
|
AssignmentExpression(path, state) {
|
283
242
|
state.assignments.push(path);
|
284
243
|
},
|
285
|
-
|
286
244
|
UpdateExpression(path, state) {
|
287
245
|
state.constantViolations.push(path);
|
288
246
|
},
|
289
|
-
|
290
247
|
UnaryExpression(path, state) {
|
291
248
|
if (path.node.operator === "delete") {
|
292
249
|
state.constantViolations.push(path);
|
293
250
|
}
|
294
251
|
},
|
295
|
-
|
296
252
|
BlockScoped(path) {
|
297
253
|
let scope = path.scope;
|
298
254
|
if (scope.path === path) scope = scope.parent;
|
@@ -305,32 +261,28 @@ const collectorVisitor = {
|
|
305
261
|
path.scope.bindings[name] = path.scope.parent.getBinding(name);
|
306
262
|
}
|
307
263
|
},
|
308
|
-
|
309
264
|
CatchClause(path) {
|
310
265
|
path.scope.registerBinding("let", path);
|
311
266
|
},
|
312
|
-
|
313
267
|
Function(path) {
|
314
268
|
const params = path.get("params");
|
315
|
-
|
316
269
|
for (const param of params) {
|
317
270
|
path.scope.registerBinding("param", param);
|
318
271
|
}
|
319
272
|
|
320
|
-
if (path.isFunctionExpression() && path.has("id") &&
|
273
|
+
if (path.isFunctionExpression() && path.has("id") &&
|
274
|
+
!path.get("id").node[NOT_LOCAL_BINDING]) {
|
321
275
|
path.scope.registerBinding("local", path.get("id"), path);
|
322
276
|
}
|
323
277
|
},
|
324
|
-
|
325
278
|
ClassExpression(path) {
|
326
|
-
if (path.has("id") &&
|
279
|
+
if (path.has("id") &&
|
280
|
+
!path.get("id").node[NOT_LOCAL_BINDING]) {
|
327
281
|
path.scope.registerBinding("local", path);
|
328
282
|
}
|
329
283
|
}
|
330
|
-
|
331
284
|
};
|
332
285
|
let uid = 0;
|
333
|
-
|
334
286
|
class Scope {
|
335
287
|
constructor(path) {
|
336
288
|
this.uid = void 0;
|
@@ -347,15 +299,11 @@ class Scope {
|
|
347
299
|
const {
|
348
300
|
node
|
349
301
|
} = path;
|
350
|
-
|
351
302
|
const cached = _cache.scope.get(node);
|
352
|
-
|
353
303
|
if ((cached == null ? void 0 : cached.path) === path) {
|
354
304
|
return cached;
|
355
305
|
}
|
356
|
-
|
357
306
|
_cache.scope.set(node, this);
|
358
|
-
|
359
307
|
this.uid = uid++;
|
360
308
|
this.block = node;
|
361
309
|
this.path = path;
|
@@ -365,28 +313,22 @@ class Scope {
|
|
365
313
|
|
366
314
|
get parent() {
|
367
315
|
var _parent;
|
368
|
-
|
369
316
|
let parent,
|
370
|
-
|
371
|
-
|
317
|
+
path = this.path;
|
372
318
|
do {
|
373
319
|
const shouldSkip = path.key === "key" || path.listKey === "decorators";
|
374
320
|
path = path.parentPath;
|
375
321
|
if (shouldSkip && path.isMethod()) path = path.parentPath;
|
376
322
|
if (path && path.isScope()) parent = path;
|
377
323
|
} while (path && !parent);
|
378
|
-
|
379
324
|
return (_parent = parent) == null ? void 0 : _parent.scope;
|
380
325
|
}
|
381
|
-
|
382
326
|
get parentBlock() {
|
383
327
|
return this.path.parent;
|
384
328
|
}
|
385
|
-
|
386
329
|
get hub() {
|
387
330
|
return this.path.hub;
|
388
331
|
}
|
389
|
-
|
390
332
|
traverse(node, opts, state) {
|
391
333
|
(0, _index.default)(node, opts, this, state, this.path);
|
392
334
|
}
|
@@ -407,12 +349,10 @@ class Scope {
|
|
407
349
|
name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
408
350
|
let uid;
|
409
351
|
let i = 1;
|
410
|
-
|
411
352
|
do {
|
412
353
|
uid = this._generateUid(name, i);
|
413
354
|
i++;
|
414
355
|
} while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
|
415
|
-
|
416
356
|
const program = this.getProgramParent();
|
417
357
|
program.references[uid] = true;
|
418
358
|
program.uids[uid] = true;
|
@@ -424,7 +364,6 @@ class Scope {
|
|
424
364
|
if (i > 1) id += i;
|
425
365
|
return `_${id}`;
|
426
366
|
}
|
427
|
-
|
428
367
|
generateUidBasedOnNode(node, defaultName) {
|
429
368
|
const parts = [];
|
430
369
|
gatherNodeParts(node, parts);
|
@@ -441,17 +380,14 @@ class Scope {
|
|
441
380
|
if (isThisExpression(node) || isSuper(node) || isTopicReference(node)) {
|
442
381
|
return true;
|
443
382
|
}
|
444
|
-
|
445
383
|
if (isIdentifier(node)) {
|
446
384
|
const binding = this.getBinding(node.name);
|
447
|
-
|
448
385
|
if (binding) {
|
449
386
|
return binding.constant;
|
450
387
|
} else {
|
451
388
|
return this.hasBinding(node.name);
|
452
389
|
}
|
453
390
|
}
|
454
|
-
|
455
391
|
return false;
|
456
392
|
}
|
457
393
|
|
@@ -460,31 +396,28 @@ class Scope {
|
|
460
396
|
return null;
|
461
397
|
} else {
|
462
398
|
const id = this.generateUidIdentifierBasedOnNode(node);
|
463
|
-
|
464
399
|
if (!dontPush) {
|
465
400
|
this.push({
|
466
401
|
id
|
467
402
|
});
|
468
403
|
return cloneNode(id);
|
469
404
|
}
|
470
|
-
|
471
405
|
return id;
|
472
406
|
}
|
473
407
|
}
|
474
|
-
|
475
408
|
checkBlockScopedCollisions(local, kind, name, id) {
|
476
409
|
if (kind === "param") return;
|
477
|
-
if (local.kind === "local") return;
|
478
|
-
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && kind === "const";
|
479
410
|
|
411
|
+
if (local.kind === "local") return;
|
412
|
+
const duplicate =
|
413
|
+
kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" ||
|
414
|
+
local.kind === "param" && kind === "const";
|
480
415
|
if (duplicate) {
|
481
416
|
throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
|
482
417
|
}
|
483
418
|
}
|
484
|
-
|
485
419
|
rename(oldName, newName, block) {
|
486
420
|
const binding = this.getBinding(oldName);
|
487
|
-
|
488
421
|
if (binding) {
|
489
422
|
newName = newName || this.generateUidIdentifier(oldName).name;
|
490
423
|
return new _renamer.default(binding, oldName, newName).rename(block);
|
@@ -497,15 +430,12 @@ class Scope {
|
|
497
430
|
map[oldName] = null;
|
498
431
|
}
|
499
432
|
}
|
500
|
-
|
501
433
|
dump() {
|
502
434
|
const sep = "-".repeat(60);
|
503
435
|
console.log(sep);
|
504
436
|
let scope = this;
|
505
|
-
|
506
437
|
do {
|
507
438
|
console.log("#", scope.block.type);
|
508
|
-
|
509
439
|
for (const name of Object.keys(scope.bindings)) {
|
510
440
|
const binding = scope.bindings[name];
|
511
441
|
console.log(" -", name, {
|
@@ -516,41 +446,35 @@ class Scope {
|
|
516
446
|
});
|
517
447
|
}
|
518
448
|
} while (scope = scope.parent);
|
519
|
-
|
520
449
|
console.log(sep);
|
521
450
|
}
|
522
451
|
|
523
452
|
toArray(node, i, arrayLikeIsIterable) {
|
524
453
|
if (isIdentifier(node)) {
|
525
454
|
const binding = this.getBinding(node.name);
|
526
|
-
|
527
455
|
if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
|
528
456
|
return node;
|
529
457
|
}
|
530
458
|
}
|
531
|
-
|
532
459
|
if (isArrayExpression(node)) {
|
533
460
|
return node;
|
534
461
|
}
|
535
|
-
|
536
462
|
if (isIdentifier(node, {
|
537
463
|
name: "arguments"
|
538
464
|
})) {
|
539
465
|
return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
|
540
466
|
}
|
541
|
-
|
542
467
|
let helperName;
|
543
468
|
const args = [node];
|
544
|
-
|
545
469
|
if (i === true) {
|
546
470
|
helperName = "toConsumableArray";
|
547
471
|
} else if (i) {
|
548
472
|
args.push(numericLiteral(i));
|
473
|
+
|
549
474
|
helperName = "slicedToArray";
|
550
475
|
} else {
|
551
476
|
helperName = "toArray";
|
552
477
|
}
|
553
|
-
|
554
478
|
if (arrayLikeIsIterable) {
|
555
479
|
args.unshift(this.hub.addHelper(helperName));
|
556
480
|
helperName = "maybeArrayLike";
|
@@ -558,19 +482,15 @@ class Scope {
|
|
558
482
|
|
559
483
|
return callExpression(this.hub.addHelper(helperName), args);
|
560
484
|
}
|
561
|
-
|
562
485
|
hasLabel(name) {
|
563
486
|
return !!this.getLabel(name);
|
564
487
|
}
|
565
|
-
|
566
488
|
getLabel(name) {
|
567
489
|
return this.labels.get(name);
|
568
490
|
}
|
569
|
-
|
570
491
|
registerLabel(path) {
|
571
492
|
this.labels.set(path.node.label.name, path);
|
572
493
|
}
|
573
|
-
|
574
494
|
registerDeclaration(path) {
|
575
495
|
if (path.isLabeledStatement()) {
|
576
496
|
this.registerLabel(path);
|
@@ -578,22 +498,22 @@ class Scope {
|
|
578
498
|
this.registerBinding("hoisted", path.get("id"), path);
|
579
499
|
} else if (path.isVariableDeclaration()) {
|
580
500
|
const declarations = path.get("declarations");
|
581
|
-
|
501
|
+
const {
|
502
|
+
kind
|
503
|
+
} = path.node;
|
582
504
|
for (const declar of declarations) {
|
583
|
-
this.registerBinding(
|
505
|
+
this.registerBinding(kind === "using" ? "const" : kind, declar);
|
584
506
|
}
|
585
507
|
} else if (path.isClassDeclaration()) {
|
586
508
|
if (path.node.declare) return;
|
587
509
|
this.registerBinding("let", path);
|
588
510
|
} else if (path.isImportDeclaration()) {
|
589
511
|
const specifiers = path.get("specifiers");
|
590
|
-
|
591
512
|
for (const specifier of specifiers) {
|
592
513
|
this.registerBinding("module", specifier);
|
593
514
|
}
|
594
515
|
} else if (path.isExportDeclaration()) {
|
595
516
|
const declar = path.get("declaration");
|
596
|
-
|
597
517
|
if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
|
598
518
|
this.registerDeclaration(declar);
|
599
519
|
}
|
@@ -601,42 +521,31 @@ class Scope {
|
|
601
521
|
this.registerBinding("unknown", path);
|
602
522
|
}
|
603
523
|
}
|
604
|
-
|
605
524
|
buildUndefinedNode() {
|
606
525
|
return unaryExpression("void", numericLiteral(0), true);
|
607
526
|
}
|
608
|
-
|
609
527
|
registerConstantViolation(path) {
|
610
528
|
const ids = path.getBindingIdentifiers();
|
611
|
-
|
612
529
|
for (const name of Object.keys(ids)) {
|
613
530
|
const binding = this.getBinding(name);
|
614
531
|
if (binding) binding.reassign(path);
|
615
532
|
}
|
616
533
|
}
|
617
|
-
|
618
534
|
registerBinding(kind, path, bindingPath = path) {
|
619
535
|
if (!kind) throw new ReferenceError("no `kind`");
|
620
|
-
|
621
536
|
if (path.isVariableDeclaration()) {
|
622
537
|
const declarators = path.get("declarations");
|
623
|
-
|
624
538
|
for (const declar of declarators) {
|
625
539
|
this.registerBinding(kind, declar);
|
626
540
|
}
|
627
|
-
|
628
541
|
return;
|
629
542
|
}
|
630
|
-
|
631
543
|
const parent = this.getProgramParent();
|
632
544
|
const ids = path.getOuterBindingIdentifiers(true);
|
633
|
-
|
634
545
|
for (const name of Object.keys(ids)) {
|
635
546
|
parent.references[name] = true;
|
636
|
-
|
637
547
|
for (const id of ids[name]) {
|
638
548
|
const local = this.getOwnBinding(name);
|
639
|
-
|
640
549
|
if (local) {
|
641
550
|
if (local.identifier === id) continue;
|
642
551
|
this.checkBlockScopedCollisions(local, kind, name, id);
|
@@ -655,35 +564,26 @@ class Scope {
|
|
655
564
|
}
|
656
565
|
}
|
657
566
|
}
|
658
|
-
|
659
567
|
addGlobal(node) {
|
660
568
|
this.globals[node.name] = node;
|
661
569
|
}
|
662
|
-
|
663
570
|
hasUid(name) {
|
664
571
|
let scope = this;
|
665
|
-
|
666
572
|
do {
|
667
573
|
if (scope.uids[name]) return true;
|
668
574
|
} while (scope = scope.parent);
|
669
|
-
|
670
575
|
return false;
|
671
576
|
}
|
672
|
-
|
673
577
|
hasGlobal(name) {
|
674
578
|
let scope = this;
|
675
|
-
|
676
579
|
do {
|
677
580
|
if (scope.globals[name]) return true;
|
678
581
|
} while (scope = scope.parent);
|
679
|
-
|
680
582
|
return false;
|
681
583
|
}
|
682
|
-
|
683
584
|
hasReference(name) {
|
684
585
|
return !!this.getProgramParent().references[name];
|
685
586
|
}
|
686
|
-
|
687
587
|
isPure(node, constantsOnly) {
|
688
588
|
if (isIdentifier(node)) {
|
689
589
|
const binding = this.getBinding(node.name);
|
@@ -694,21 +594,17 @@ class Scope {
|
|
694
594
|
return true;
|
695
595
|
} else if (isClass(node)) {
|
696
596
|
var _node$decorators;
|
697
|
-
|
698
597
|
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
|
699
598
|
return false;
|
700
599
|
}
|
701
|
-
|
702
600
|
if (((_node$decorators = node.decorators) == null ? void 0 : _node$decorators.length) > 0) {
|
703
601
|
return false;
|
704
602
|
}
|
705
|
-
|
706
603
|
return this.isPure(node.body, constantsOnly);
|
707
604
|
} else if (isClassBody(node)) {
|
708
605
|
for (const method of node.body) {
|
709
606
|
if (!this.isPure(method, constantsOnly)) return false;
|
710
607
|
}
|
711
|
-
|
712
608
|
return true;
|
713
609
|
} else if (isBinary(node)) {
|
714
610
|
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
@@ -716,39 +612,30 @@ class Scope {
|
|
716
612
|
for (const elem of node.elements) {
|
717
613
|
if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
|
718
614
|
}
|
719
|
-
|
720
615
|
return true;
|
721
616
|
} else if (isObjectExpression(node) || isRecordExpression(node)) {
|
722
617
|
for (const prop of node.properties) {
|
723
618
|
if (!this.isPure(prop, constantsOnly)) return false;
|
724
619
|
}
|
725
|
-
|
726
620
|
return true;
|
727
621
|
} else if (isMethod(node)) {
|
728
622
|
var _node$decorators2;
|
729
|
-
|
730
623
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
731
|
-
|
732
624
|
if (((_node$decorators2 = node.decorators) == null ? void 0 : _node$decorators2.length) > 0) {
|
733
625
|
return false;
|
734
626
|
}
|
735
|
-
|
736
627
|
return true;
|
737
628
|
} else if (isProperty(node)) {
|
738
629
|
var _node$decorators3;
|
739
|
-
|
740
630
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
741
|
-
|
742
631
|
if (((_node$decorators3 = node.decorators) == null ? void 0 : _node$decorators3.length) > 0) {
|
743
632
|
return false;
|
744
633
|
}
|
745
|
-
|
746
634
|
if (isObjectProperty(node) || node.static) {
|
747
635
|
if (node.value !== null && !this.isPure(node.value, constantsOnly)) {
|
748
636
|
return false;
|
749
637
|
}
|
750
638
|
}
|
751
|
-
|
752
639
|
return true;
|
753
640
|
} else if (isUnaryExpression(node)) {
|
754
641
|
return this.isPure(node.argument, constantsOnly);
|
@@ -758,7 +645,6 @@ class Scope {
|
|
758
645
|
for (const expression of node.expressions) {
|
759
646
|
if (!this.isPure(expression, constantsOnly)) return false;
|
760
647
|
}
|
761
|
-
|
762
648
|
return true;
|
763
649
|
} else {
|
764
650
|
return isPureish(node);
|
@@ -771,7 +657,6 @@ class Scope {
|
|
771
657
|
|
772
658
|
getData(key) {
|
773
659
|
let scope = this;
|
774
|
-
|
775
660
|
do {
|
776
661
|
const data = scope.data[key];
|
777
662
|
if (data != null) return data;
|
@@ -780,20 +665,17 @@ class Scope {
|
|
780
665
|
|
781
666
|
removeData(key) {
|
782
667
|
let scope = this;
|
783
|
-
|
784
668
|
do {
|
785
669
|
const data = scope.data[key];
|
786
670
|
if (data != null) scope.data[key] = null;
|
787
671
|
} while (scope = scope.parent);
|
788
672
|
}
|
789
|
-
|
790
673
|
init() {
|
791
674
|
if (!this.inited) {
|
792
675
|
this.inited = true;
|
793
676
|
this.crawl();
|
794
677
|
}
|
795
678
|
}
|
796
|
-
|
797
679
|
crawl() {
|
798
680
|
const path = this.path;
|
799
681
|
this.references = Object.create(null);
|
@@ -809,27 +691,22 @@ class Scope {
|
|
809
691
|
assignments: []
|
810
692
|
};
|
811
693
|
this.crawling = true;
|
812
|
-
|
813
694
|
if (path.type !== "Program" && collectorVisitor._exploded) {
|
814
695
|
for (const visit of collectorVisitor.enter) {
|
815
696
|
visit(path, state);
|
816
697
|
}
|
817
|
-
|
818
698
|
const typeVisitors = collectorVisitor[path.type];
|
819
|
-
|
820
699
|
if (typeVisitors) {
|
821
700
|
for (const visit of typeVisitors.enter) {
|
822
701
|
visit(path, state);
|
823
702
|
}
|
824
703
|
}
|
825
704
|
}
|
826
|
-
|
827
705
|
path.traverse(collectorVisitor, state);
|
828
706
|
this.crawling = false;
|
829
707
|
|
830
708
|
for (const path of state.assignments) {
|
831
709
|
const ids = path.getBindingIdentifiers();
|
832
|
-
|
833
710
|
for (const name of Object.keys(ids)) {
|
834
711
|
if (path.scope.getBinding(name)) continue;
|
835
712
|
programParent.addGlobal(ids[name]);
|
@@ -840,7 +717,6 @@ class Scope {
|
|
840
717
|
|
841
718
|
for (const ref of state.references) {
|
842
719
|
const binding = ref.scope.getBinding(ref.node.name);
|
843
|
-
|
844
720
|
if (binding) {
|
845
721
|
binding.reference(ref);
|
846
722
|
} else {
|
@@ -852,38 +728,31 @@ class Scope {
|
|
852
728
|
path.scope.registerConstantViolation(path);
|
853
729
|
}
|
854
730
|
}
|
855
|
-
|
856
731
|
push(opts) {
|
857
732
|
let path = this.path;
|
858
|
-
|
859
733
|
if (path.isPattern()) {
|
860
734
|
path = this.getPatternParent().path;
|
861
735
|
} else if (!path.isBlockStatement() && !path.isProgram()) {
|
862
736
|
path = this.getBlockParent().path;
|
863
737
|
}
|
864
|
-
|
865
738
|
if (path.isSwitchStatement()) {
|
866
739
|
path = (this.getFunctionParent() || this.getProgramParent()).path;
|
867
740
|
}
|
868
|
-
|
869
741
|
if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
|
870
742
|
path.ensureBlock();
|
871
743
|
path = path.get("body");
|
872
744
|
}
|
873
|
-
|
874
745
|
const unique = opts.unique;
|
875
746
|
const kind = opts.kind || "var";
|
876
747
|
const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
|
877
748
|
const dataKey = `declaration:${kind}:${blockHoist}`;
|
878
749
|
let declarPath = !unique && path.getData(dataKey);
|
879
|
-
|
880
750
|
if (!declarPath) {
|
881
751
|
const declar = variableDeclaration(kind, []);
|
882
752
|
declar._blockHoist = blockHoist;
|
883
753
|
[declarPath] = path.unshiftContainer("body", [declar]);
|
884
754
|
if (!unique) path.setData(dataKey, declarPath);
|
885
755
|
}
|
886
|
-
|
887
756
|
const declarator = variableDeclarator(opts.id, opts.init);
|
888
757
|
const len = declarPath.node.declarations.push(declarator);
|
889
758
|
path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
|
@@ -891,120 +760,99 @@ class Scope {
|
|
891
760
|
|
892
761
|
getProgramParent() {
|
893
762
|
let scope = this;
|
894
|
-
|
895
763
|
do {
|
896
764
|
if (scope.path.isProgram()) {
|
897
765
|
return scope;
|
898
766
|
}
|
899
767
|
} while (scope = scope.parent);
|
900
|
-
|
901
768
|
throw new Error("Couldn't find a Program");
|
902
769
|
}
|
903
770
|
|
904
771
|
getFunctionParent() {
|
905
772
|
let scope = this;
|
906
|
-
|
907
773
|
do {
|
908
774
|
if (scope.path.isFunctionParent()) {
|
909
775
|
return scope;
|
910
776
|
}
|
911
777
|
} while (scope = scope.parent);
|
912
|
-
|
913
778
|
return null;
|
914
779
|
}
|
915
780
|
|
916
781
|
getBlockParent() {
|
917
782
|
let scope = this;
|
918
|
-
|
919
783
|
do {
|
920
784
|
if (scope.path.isBlockParent()) {
|
921
785
|
return scope;
|
922
786
|
}
|
923
787
|
} while (scope = scope.parent);
|
924
|
-
|
925
788
|
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
926
789
|
}
|
927
790
|
|
928
791
|
getPatternParent() {
|
929
792
|
let scope = this;
|
930
|
-
|
931
793
|
do {
|
932
794
|
if (!scope.path.isPattern()) {
|
933
795
|
return scope.getBlockParent();
|
934
796
|
}
|
935
797
|
} while (scope = scope.parent.parent);
|
936
|
-
|
937
798
|
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
938
799
|
}
|
939
800
|
|
940
801
|
getAllBindings() {
|
941
802
|
const ids = Object.create(null);
|
942
803
|
let scope = this;
|
943
|
-
|
944
804
|
do {
|
945
805
|
for (const key of Object.keys(scope.bindings)) {
|
946
806
|
if (key in ids === false) {
|
947
807
|
ids[key] = scope.bindings[key];
|
948
808
|
}
|
949
809
|
}
|
950
|
-
|
951
810
|
scope = scope.parent;
|
952
811
|
} while (scope);
|
953
|
-
|
954
812
|
return ids;
|
955
813
|
}
|
956
814
|
|
957
815
|
getAllBindingsOfKind(...kinds) {
|
958
816
|
const ids = Object.create(null);
|
959
|
-
|
960
817
|
for (const kind of kinds) {
|
961
818
|
let scope = this;
|
962
|
-
|
963
819
|
do {
|
964
820
|
for (const name of Object.keys(scope.bindings)) {
|
965
821
|
const binding = scope.bindings[name];
|
966
822
|
if (binding.kind === kind) ids[name] = binding;
|
967
823
|
}
|
968
|
-
|
969
824
|
scope = scope.parent;
|
970
825
|
} while (scope);
|
971
826
|
}
|
972
|
-
|
973
827
|
return ids;
|
974
828
|
}
|
975
|
-
|
976
829
|
bindingIdentifierEquals(name, node) {
|
977
830
|
return this.getBindingIdentifier(name) === node;
|
978
831
|
}
|
979
|
-
|
980
832
|
getBinding(name) {
|
981
833
|
let scope = this;
|
982
834
|
let previousPath;
|
983
|
-
|
984
835
|
do {
|
985
836
|
const binding = scope.getOwnBinding(name);
|
986
|
-
|
987
837
|
if (binding) {
|
988
838
|
var _previousPath;
|
989
839
|
|
990
|
-
if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {
|
840
|
+
if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {
|
841
|
+
} else {
|
991
842
|
return binding;
|
992
843
|
}
|
993
844
|
} else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
994
845
|
break;
|
995
846
|
}
|
996
|
-
|
997
847
|
previousPath = scope.path;
|
998
848
|
} while (scope = scope.parent);
|
999
849
|
}
|
1000
|
-
|
1001
850
|
getOwnBinding(name) {
|
1002
851
|
return this.bindings[name];
|
1003
852
|
}
|
1004
853
|
|
1005
854
|
getBindingIdentifier(name) {
|
1006
855
|
var _this$getBinding;
|
1007
|
-
|
1008
856
|
return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
|
1009
857
|
}
|
1010
858
|
|
@@ -1012,11 +860,9 @@ class Scope {
|
|
1012
860
|
const binding = this.bindings[name];
|
1013
861
|
return binding == null ? void 0 : binding.identifier;
|
1014
862
|
}
|
1015
|
-
|
1016
863
|
hasOwnBinding(name) {
|
1017
864
|
return !!this.getOwnBinding(name);
|
1018
865
|
}
|
1019
|
-
|
1020
866
|
hasBinding(name, noGlobals) {
|
1021
867
|
if (!name) return false;
|
1022
868
|
if (this.hasOwnBinding(name)) return true;
|
@@ -1026,42 +872,34 @@ class Scope {
|
|
1026
872
|
if (!noGlobals && Scope.contextVariables.includes(name)) return true;
|
1027
873
|
return false;
|
1028
874
|
}
|
1029
|
-
|
1030
875
|
parentHasBinding(name, noGlobals) {
|
1031
876
|
var _this$parent;
|
1032
|
-
|
1033
877
|
return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, noGlobals);
|
1034
878
|
}
|
1035
879
|
|
1036
880
|
moveBindingTo(name, scope) {
|
1037
881
|
const info = this.getBinding(name);
|
1038
|
-
|
1039
882
|
if (info) {
|
1040
883
|
info.scope.removeOwnBinding(name);
|
1041
884
|
info.scope = scope;
|
1042
885
|
scope.bindings[name] = info;
|
1043
886
|
}
|
1044
887
|
}
|
1045
|
-
|
1046
888
|
removeOwnBinding(name) {
|
1047
889
|
delete this.bindings[name];
|
1048
890
|
}
|
1049
|
-
|
1050
891
|
removeBinding(name) {
|
1051
892
|
var _this$getBinding2;
|
1052
|
-
|
1053
893
|
(_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
|
1054
|
-
let scope = this;
|
1055
894
|
|
895
|
+
let scope = this;
|
1056
896
|
do {
|
1057
897
|
if (scope.uids[name]) {
|
1058
898
|
scope.uids[name] = false;
|
1059
899
|
}
|
1060
900
|
} while (scope = scope.parent);
|
1061
901
|
}
|
1062
|
-
|
1063
902
|
}
|
1064
|
-
|
1065
903
|
exports.default = Scope;
|
1066
904
|
Scope.globals = Object.keys(_globals.builtin);
|
1067
905
|
Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
|