@babel/traverse 7.20.0 → 7.20.5
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 +14 -54
- package/lib/path/context.js.map +1 -1
- package/lib/path/conversion.js +23 -78
- 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 -31
- 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 +19 -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/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
|
-
} else if (i) {
|
471
|
+
} else if (typeof i === "number") {
|
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);
|
@@ -581,7 +501,6 @@ class Scope {
|
|
581
501
|
const {
|
582
502
|
kind
|
583
503
|
} = path.node;
|
584
|
-
|
585
504
|
for (const declar of declarations) {
|
586
505
|
this.registerBinding(kind === "using" ? "const" : kind, declar);
|
587
506
|
}
|
@@ -590,13 +509,11 @@ class Scope {
|
|
590
509
|
this.registerBinding("let", path);
|
591
510
|
} else if (path.isImportDeclaration()) {
|
592
511
|
const specifiers = path.get("specifiers");
|
593
|
-
|
594
512
|
for (const specifier of specifiers) {
|
595
513
|
this.registerBinding("module", specifier);
|
596
514
|
}
|
597
515
|
} else if (path.isExportDeclaration()) {
|
598
516
|
const declar = path.get("declaration");
|
599
|
-
|
600
517
|
if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
|
601
518
|
this.registerDeclaration(declar);
|
602
519
|
}
|
@@ -604,42 +521,31 @@ class Scope {
|
|
604
521
|
this.registerBinding("unknown", path);
|
605
522
|
}
|
606
523
|
}
|
607
|
-
|
608
524
|
buildUndefinedNode() {
|
609
525
|
return unaryExpression("void", numericLiteral(0), true);
|
610
526
|
}
|
611
|
-
|
612
527
|
registerConstantViolation(path) {
|
613
528
|
const ids = path.getBindingIdentifiers();
|
614
|
-
|
615
529
|
for (const name of Object.keys(ids)) {
|
616
530
|
const binding = this.getBinding(name);
|
617
531
|
if (binding) binding.reassign(path);
|
618
532
|
}
|
619
533
|
}
|
620
|
-
|
621
534
|
registerBinding(kind, path, bindingPath = path) {
|
622
535
|
if (!kind) throw new ReferenceError("no `kind`");
|
623
|
-
|
624
536
|
if (path.isVariableDeclaration()) {
|
625
537
|
const declarators = path.get("declarations");
|
626
|
-
|
627
538
|
for (const declar of declarators) {
|
628
539
|
this.registerBinding(kind, declar);
|
629
540
|
}
|
630
|
-
|
631
541
|
return;
|
632
542
|
}
|
633
|
-
|
634
543
|
const parent = this.getProgramParent();
|
635
544
|
const ids = path.getOuterBindingIdentifiers(true);
|
636
|
-
|
637
545
|
for (const name of Object.keys(ids)) {
|
638
546
|
parent.references[name] = true;
|
639
|
-
|
640
547
|
for (const id of ids[name]) {
|
641
548
|
const local = this.getOwnBinding(name);
|
642
|
-
|
643
549
|
if (local) {
|
644
550
|
if (local.identifier === id) continue;
|
645
551
|
this.checkBlockScopedCollisions(local, kind, name, id);
|
@@ -658,35 +564,26 @@ class Scope {
|
|
658
564
|
}
|
659
565
|
}
|
660
566
|
}
|
661
|
-
|
662
567
|
addGlobal(node) {
|
663
568
|
this.globals[node.name] = node;
|
664
569
|
}
|
665
|
-
|
666
570
|
hasUid(name) {
|
667
571
|
let scope = this;
|
668
|
-
|
669
572
|
do {
|
670
573
|
if (scope.uids[name]) return true;
|
671
574
|
} while (scope = scope.parent);
|
672
|
-
|
673
575
|
return false;
|
674
576
|
}
|
675
|
-
|
676
577
|
hasGlobal(name) {
|
677
578
|
let scope = this;
|
678
|
-
|
679
579
|
do {
|
680
580
|
if (scope.globals[name]) return true;
|
681
581
|
} while (scope = scope.parent);
|
682
|
-
|
683
582
|
return false;
|
684
583
|
}
|
685
|
-
|
686
584
|
hasReference(name) {
|
687
585
|
return !!this.getProgramParent().references[name];
|
688
586
|
}
|
689
|
-
|
690
587
|
isPure(node, constantsOnly) {
|
691
588
|
if (isIdentifier(node)) {
|
692
589
|
const binding = this.getBinding(node.name);
|
@@ -697,21 +594,17 @@ class Scope {
|
|
697
594
|
return true;
|
698
595
|
} else if (isClass(node)) {
|
699
596
|
var _node$decorators;
|
700
|
-
|
701
597
|
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
|
702
598
|
return false;
|
703
599
|
}
|
704
|
-
|
705
600
|
if (((_node$decorators = node.decorators) == null ? void 0 : _node$decorators.length) > 0) {
|
706
601
|
return false;
|
707
602
|
}
|
708
|
-
|
709
603
|
return this.isPure(node.body, constantsOnly);
|
710
604
|
} else if (isClassBody(node)) {
|
711
605
|
for (const method of node.body) {
|
712
606
|
if (!this.isPure(method, constantsOnly)) return false;
|
713
607
|
}
|
714
|
-
|
715
608
|
return true;
|
716
609
|
} else if (isBinary(node)) {
|
717
610
|
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
@@ -719,39 +612,30 @@ class Scope {
|
|
719
612
|
for (const elem of node.elements) {
|
720
613
|
if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
|
721
614
|
}
|
722
|
-
|
723
615
|
return true;
|
724
616
|
} else if (isObjectExpression(node) || isRecordExpression(node)) {
|
725
617
|
for (const prop of node.properties) {
|
726
618
|
if (!this.isPure(prop, constantsOnly)) return false;
|
727
619
|
}
|
728
|
-
|
729
620
|
return true;
|
730
621
|
} else if (isMethod(node)) {
|
731
622
|
var _node$decorators2;
|
732
|
-
|
733
623
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
734
|
-
|
735
624
|
if (((_node$decorators2 = node.decorators) == null ? void 0 : _node$decorators2.length) > 0) {
|
736
625
|
return false;
|
737
626
|
}
|
738
|
-
|
739
627
|
return true;
|
740
628
|
} else if (isProperty(node)) {
|
741
629
|
var _node$decorators3;
|
742
|
-
|
743
630
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
744
|
-
|
745
631
|
if (((_node$decorators3 = node.decorators) == null ? void 0 : _node$decorators3.length) > 0) {
|
746
632
|
return false;
|
747
633
|
}
|
748
|
-
|
749
634
|
if (isObjectProperty(node) || node.static) {
|
750
635
|
if (node.value !== null && !this.isPure(node.value, constantsOnly)) {
|
751
636
|
return false;
|
752
637
|
}
|
753
638
|
}
|
754
|
-
|
755
639
|
return true;
|
756
640
|
} else if (isUnaryExpression(node)) {
|
757
641
|
return this.isPure(node.argument, constantsOnly);
|
@@ -761,7 +645,6 @@ class Scope {
|
|
761
645
|
for (const expression of node.expressions) {
|
762
646
|
if (!this.isPure(expression, constantsOnly)) return false;
|
763
647
|
}
|
764
|
-
|
765
648
|
return true;
|
766
649
|
} else {
|
767
650
|
return isPureish(node);
|
@@ -774,7 +657,6 @@ class Scope {
|
|
774
657
|
|
775
658
|
getData(key) {
|
776
659
|
let scope = this;
|
777
|
-
|
778
660
|
do {
|
779
661
|
const data = scope.data[key];
|
780
662
|
if (data != null) return data;
|
@@ -783,20 +665,17 @@ class Scope {
|
|
783
665
|
|
784
666
|
removeData(key) {
|
785
667
|
let scope = this;
|
786
|
-
|
787
668
|
do {
|
788
669
|
const data = scope.data[key];
|
789
670
|
if (data != null) scope.data[key] = null;
|
790
671
|
} while (scope = scope.parent);
|
791
672
|
}
|
792
|
-
|
793
673
|
init() {
|
794
674
|
if (!this.inited) {
|
795
675
|
this.inited = true;
|
796
676
|
this.crawl();
|
797
677
|
}
|
798
678
|
}
|
799
|
-
|
800
679
|
crawl() {
|
801
680
|
const path = this.path;
|
802
681
|
this.references = Object.create(null);
|
@@ -812,27 +691,22 @@ class Scope {
|
|
812
691
|
assignments: []
|
813
692
|
};
|
814
693
|
this.crawling = true;
|
815
|
-
|
816
694
|
if (path.type !== "Program" && collectorVisitor._exploded) {
|
817
695
|
for (const visit of collectorVisitor.enter) {
|
818
696
|
visit(path, state);
|
819
697
|
}
|
820
|
-
|
821
698
|
const typeVisitors = collectorVisitor[path.type];
|
822
|
-
|
823
699
|
if (typeVisitors) {
|
824
700
|
for (const visit of typeVisitors.enter) {
|
825
701
|
visit(path, state);
|
826
702
|
}
|
827
703
|
}
|
828
704
|
}
|
829
|
-
|
830
705
|
path.traverse(collectorVisitor, state);
|
831
706
|
this.crawling = false;
|
832
707
|
|
833
708
|
for (const path of state.assignments) {
|
834
709
|
const ids = path.getBindingIdentifiers();
|
835
|
-
|
836
710
|
for (const name of Object.keys(ids)) {
|
837
711
|
if (path.scope.getBinding(name)) continue;
|
838
712
|
programParent.addGlobal(ids[name]);
|
@@ -843,7 +717,6 @@ class Scope {
|
|
843
717
|
|
844
718
|
for (const ref of state.references) {
|
845
719
|
const binding = ref.scope.getBinding(ref.node.name);
|
846
|
-
|
847
720
|
if (binding) {
|
848
721
|
binding.reference(ref);
|
849
722
|
} else {
|
@@ -855,38 +728,31 @@ class Scope {
|
|
855
728
|
path.scope.registerConstantViolation(path);
|
856
729
|
}
|
857
730
|
}
|
858
|
-
|
859
731
|
push(opts) {
|
860
732
|
let path = this.path;
|
861
|
-
|
862
733
|
if (path.isPattern()) {
|
863
734
|
path = this.getPatternParent().path;
|
864
735
|
} else if (!path.isBlockStatement() && !path.isProgram()) {
|
865
736
|
path = this.getBlockParent().path;
|
866
737
|
}
|
867
|
-
|
868
738
|
if (path.isSwitchStatement()) {
|
869
739
|
path = (this.getFunctionParent() || this.getProgramParent()).path;
|
870
740
|
}
|
871
|
-
|
872
741
|
if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
|
873
742
|
path.ensureBlock();
|
874
743
|
path = path.get("body");
|
875
744
|
}
|
876
|
-
|
877
745
|
const unique = opts.unique;
|
878
746
|
const kind = opts.kind || "var";
|
879
747
|
const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
|
880
748
|
const dataKey = `declaration:${kind}:${blockHoist}`;
|
881
749
|
let declarPath = !unique && path.getData(dataKey);
|
882
|
-
|
883
750
|
if (!declarPath) {
|
884
751
|
const declar = variableDeclaration(kind, []);
|
885
752
|
declar._blockHoist = blockHoist;
|
886
753
|
[declarPath] = path.unshiftContainer("body", [declar]);
|
887
754
|
if (!unique) path.setData(dataKey, declarPath);
|
888
755
|
}
|
889
|
-
|
890
756
|
const declarator = variableDeclarator(opts.id, opts.init);
|
891
757
|
const len = declarPath.node.declarations.push(declarator);
|
892
758
|
path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
|
@@ -894,120 +760,99 @@ class Scope {
|
|
894
760
|
|
895
761
|
getProgramParent() {
|
896
762
|
let scope = this;
|
897
|
-
|
898
763
|
do {
|
899
764
|
if (scope.path.isProgram()) {
|
900
765
|
return scope;
|
901
766
|
}
|
902
767
|
} while (scope = scope.parent);
|
903
|
-
|
904
768
|
throw new Error("Couldn't find a Program");
|
905
769
|
}
|
906
770
|
|
907
771
|
getFunctionParent() {
|
908
772
|
let scope = this;
|
909
|
-
|
910
773
|
do {
|
911
774
|
if (scope.path.isFunctionParent()) {
|
912
775
|
return scope;
|
913
776
|
}
|
914
777
|
} while (scope = scope.parent);
|
915
|
-
|
916
778
|
return null;
|
917
779
|
}
|
918
780
|
|
919
781
|
getBlockParent() {
|
920
782
|
let scope = this;
|
921
|
-
|
922
783
|
do {
|
923
784
|
if (scope.path.isBlockParent()) {
|
924
785
|
return scope;
|
925
786
|
}
|
926
787
|
} while (scope = scope.parent);
|
927
|
-
|
928
788
|
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
929
789
|
}
|
930
790
|
|
931
791
|
getPatternParent() {
|
932
792
|
let scope = this;
|
933
|
-
|
934
793
|
do {
|
935
794
|
if (!scope.path.isPattern()) {
|
936
795
|
return scope.getBlockParent();
|
937
796
|
}
|
938
797
|
} while (scope = scope.parent.parent);
|
939
|
-
|
940
798
|
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
941
799
|
}
|
942
800
|
|
943
801
|
getAllBindings() {
|
944
802
|
const ids = Object.create(null);
|
945
803
|
let scope = this;
|
946
|
-
|
947
804
|
do {
|
948
805
|
for (const key of Object.keys(scope.bindings)) {
|
949
806
|
if (key in ids === false) {
|
950
807
|
ids[key] = scope.bindings[key];
|
951
808
|
}
|
952
809
|
}
|
953
|
-
|
954
810
|
scope = scope.parent;
|
955
811
|
} while (scope);
|
956
|
-
|
957
812
|
return ids;
|
958
813
|
}
|
959
814
|
|
960
815
|
getAllBindingsOfKind(...kinds) {
|
961
816
|
const ids = Object.create(null);
|
962
|
-
|
963
817
|
for (const kind of kinds) {
|
964
818
|
let scope = this;
|
965
|
-
|
966
819
|
do {
|
967
820
|
for (const name of Object.keys(scope.bindings)) {
|
968
821
|
const binding = scope.bindings[name];
|
969
822
|
if (binding.kind === kind) ids[name] = binding;
|
970
823
|
}
|
971
|
-
|
972
824
|
scope = scope.parent;
|
973
825
|
} while (scope);
|
974
826
|
}
|
975
|
-
|
976
827
|
return ids;
|
977
828
|
}
|
978
|
-
|
979
829
|
bindingIdentifierEquals(name, node) {
|
980
830
|
return this.getBindingIdentifier(name) === node;
|
981
831
|
}
|
982
|
-
|
983
832
|
getBinding(name) {
|
984
833
|
let scope = this;
|
985
834
|
let previousPath;
|
986
|
-
|
987
835
|
do {
|
988
836
|
const binding = scope.getOwnBinding(name);
|
989
|
-
|
990
837
|
if (binding) {
|
991
838
|
var _previousPath;
|
992
839
|
|
993
|
-
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 {
|
994
842
|
return binding;
|
995
843
|
}
|
996
844
|
} else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
997
845
|
break;
|
998
846
|
}
|
999
|
-
|
1000
847
|
previousPath = scope.path;
|
1001
848
|
} while (scope = scope.parent);
|
1002
849
|
}
|
1003
|
-
|
1004
850
|
getOwnBinding(name) {
|
1005
851
|
return this.bindings[name];
|
1006
852
|
}
|
1007
853
|
|
1008
854
|
getBindingIdentifier(name) {
|
1009
855
|
var _this$getBinding;
|
1010
|
-
|
1011
856
|
return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
|
1012
857
|
}
|
1013
858
|
|
@@ -1015,11 +860,9 @@ class Scope {
|
|
1015
860
|
const binding = this.bindings[name];
|
1016
861
|
return binding == null ? void 0 : binding.identifier;
|
1017
862
|
}
|
1018
|
-
|
1019
863
|
hasOwnBinding(name) {
|
1020
864
|
return !!this.getOwnBinding(name);
|
1021
865
|
}
|
1022
|
-
|
1023
866
|
hasBinding(name, noGlobals) {
|
1024
867
|
if (!name) return false;
|
1025
868
|
if (this.hasOwnBinding(name)) return true;
|
@@ -1029,42 +872,34 @@ class Scope {
|
|
1029
872
|
if (!noGlobals && Scope.contextVariables.includes(name)) return true;
|
1030
873
|
return false;
|
1031
874
|
}
|
1032
|
-
|
1033
875
|
parentHasBinding(name, noGlobals) {
|
1034
876
|
var _this$parent;
|
1035
|
-
|
1036
877
|
return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, noGlobals);
|
1037
878
|
}
|
1038
879
|
|
1039
880
|
moveBindingTo(name, scope) {
|
1040
881
|
const info = this.getBinding(name);
|
1041
|
-
|
1042
882
|
if (info) {
|
1043
883
|
info.scope.removeOwnBinding(name);
|
1044
884
|
info.scope = scope;
|
1045
885
|
scope.bindings[name] = info;
|
1046
886
|
}
|
1047
887
|
}
|
1048
|
-
|
1049
888
|
removeOwnBinding(name) {
|
1050
889
|
delete this.bindings[name];
|
1051
890
|
}
|
1052
|
-
|
1053
891
|
removeBinding(name) {
|
1054
892
|
var _this$getBinding2;
|
1055
|
-
|
1056
893
|
(_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
|
1057
|
-
let scope = this;
|
1058
894
|
|
895
|
+
let scope = this;
|
1059
896
|
do {
|
1060
897
|
if (scope.uids[name]) {
|
1061
898
|
scope.uids[name] = false;
|
1062
899
|
}
|
1063
900
|
} while (scope = scope.parent);
|
1064
901
|
}
|
1065
|
-
|
1066
902
|
}
|
1067
|
-
|
1068
903
|
exports.default = Scope;
|
1069
904
|
Scope.globals = Object.keys(_globals.builtin);
|
1070
905
|
Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
|