@babel/traverse 7.0.0-beta.45 → 7.0.0-beta.49
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/.npmignore +3 -0
- package/README.md +3 -3
- package/lib/cache.js +2 -2
- package/lib/context.js +35 -75
- package/lib/hub.js +6 -3
- package/lib/index.js +12 -24
- package/lib/path/ancestry.js +26 -40
- package/lib/path/comments.js +9 -9
- package/lib/path/context.js +17 -45
- package/lib/path/conversion.js +122 -128
- package/lib/path/evaluation.js +97 -136
- package/lib/path/family.js +27 -40
- package/lib/path/index.js +61 -81
- package/lib/path/inference/index.js +12 -16
- package/lib/path/inference/inferer-reference.js +37 -41
- package/lib/path/inference/inferers.js +18 -16
- package/lib/path/introspection.js +55 -99
- package/lib/path/lib/hoister.js +48 -56
- package/lib/path/lib/removal-hooks.js +2 -2
- package/lib/path/lib/virtual-types.js +76 -46
- package/lib/path/modification.js +40 -56
- package/lib/path/removal.js +3 -10
- package/lib/path/replacement.js +34 -52
- package/lib/scope/binding.js +20 -22
- package/lib/scope/index.js +280 -402
- package/lib/scope/lib/renamer.js +37 -35
- package/lib/visitors.js +62 -120
- package/package.json +9 -9
package/lib/scope/index.js
CHANGED
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
exports.default = void 0;
|
7
7
|
|
8
8
|
function _includes() {
|
9
|
-
|
9
|
+
const data = _interopRequireDefault(require("lodash/includes"));
|
10
10
|
|
11
|
-
_includes = function
|
11
|
+
_includes = function () {
|
12
12
|
return data;
|
13
13
|
};
|
14
14
|
|
@@ -16,9 +16,9 @@ function _includes() {
|
|
16
16
|
}
|
17
17
|
|
18
18
|
function _repeat() {
|
19
|
-
|
19
|
+
const data = _interopRequireDefault(require("lodash/repeat"));
|
20
20
|
|
21
|
-
_repeat = function
|
21
|
+
_repeat = function () {
|
22
22
|
return data;
|
23
23
|
};
|
24
24
|
|
@@ -30,21 +30,21 @@ var _renamer = _interopRequireDefault(require("./lib/renamer"));
|
|
30
30
|
var _index = _interopRequireDefault(require("../index"));
|
31
31
|
|
32
32
|
function _defaults() {
|
33
|
-
|
33
|
+
const data = _interopRequireDefault(require("lodash/defaults"));
|
34
34
|
|
35
|
-
_defaults = function
|
35
|
+
_defaults = function () {
|
36
36
|
return data;
|
37
37
|
};
|
38
38
|
|
39
39
|
return data;
|
40
40
|
}
|
41
41
|
|
42
|
-
var
|
42
|
+
var _binding = _interopRequireDefault(require("./binding"));
|
43
43
|
|
44
44
|
function _globals() {
|
45
|
-
|
45
|
+
const data = _interopRequireDefault(require("globals"));
|
46
46
|
|
47
|
-
_globals = function
|
47
|
+
_globals = function () {
|
48
48
|
return data;
|
49
49
|
};
|
50
50
|
|
@@ -52,9 +52,9 @@ function _globals() {
|
|
52
52
|
}
|
53
53
|
|
54
54
|
function t() {
|
55
|
-
|
55
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
56
56
|
|
57
|
-
t = function
|
57
|
+
t = function () {
|
58
58
|
return data;
|
59
59
|
};
|
60
60
|
|
@@ -67,19 +67,12 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
|
|
67
67
|
|
68
68
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
69
69
|
|
70
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
71
|
-
|
72
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
73
|
-
|
74
70
|
function gatherNodeParts(node, parts) {
|
75
71
|
if (t().isModuleDeclaration(node)) {
|
76
72
|
if (node.source) {
|
77
73
|
gatherNodeParts(node.source, parts);
|
78
74
|
} else if (node.specifiers && node.specifiers.length) {
|
79
|
-
|
80
|
-
|
81
|
-
for (var _i = 0; _i < _arr.length; _i++) {
|
82
|
-
var specifier = _arr[_i];
|
75
|
+
for (const specifier of node.specifiers) {
|
83
76
|
gatherNodeParts(specifier, parts);
|
84
77
|
}
|
85
78
|
} else if (node.declaration) {
|
@@ -97,10 +90,7 @@ function gatherNodeParts(node, parts) {
|
|
97
90
|
} else if (t().isCallExpression(node)) {
|
98
91
|
gatherNodeParts(node.callee, parts);
|
99
92
|
} else if (t().isObjectExpression(node) || t().isObjectPattern(node)) {
|
100
|
-
|
101
|
-
|
102
|
-
for (var _i2 = 0; _i2 < _arr2.length; _i2++) {
|
103
|
-
var prop = _arr2[_i2];
|
93
|
+
for (const prop of node.properties) {
|
104
94
|
gatherNodeParts(prop.key || prop.argument, parts);
|
105
95
|
}
|
106
96
|
} else if (t().isPrivateName(node)) {
|
@@ -112,113 +102,120 @@ function gatherNodeParts(node, parts) {
|
|
112
102
|
}
|
113
103
|
}
|
114
104
|
|
115
|
-
|
116
|
-
For
|
117
|
-
|
118
|
-
|
119
|
-
for (var _i3 = 0; _i3 < _arr3.length; _i3++) {
|
120
|
-
var key = _arr3[_i3];
|
121
|
-
var declar = path.get(key);
|
105
|
+
const collectorVisitor = {
|
106
|
+
For(path) {
|
107
|
+
for (const key of t().FOR_INIT_KEYS) {
|
108
|
+
const declar = path.get(key);
|
122
109
|
|
123
110
|
if (declar.isVar()) {
|
124
|
-
|
111
|
+
const parentScope = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
125
112
|
parentScope.registerBinding("var", declar);
|
126
113
|
}
|
127
114
|
}
|
128
115
|
},
|
129
|
-
|
116
|
+
|
117
|
+
Declaration(path) {
|
130
118
|
if (path.isBlockScoped()) return;
|
131
119
|
|
132
120
|
if (path.isExportDeclaration() && path.get("declaration").isDeclaration()) {
|
133
121
|
return;
|
134
122
|
}
|
135
123
|
|
136
|
-
|
124
|
+
const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
137
125
|
parent.registerDeclaration(path);
|
138
126
|
},
|
139
|
-
|
127
|
+
|
128
|
+
ReferencedIdentifier(path, state) {
|
140
129
|
state.references.push(path);
|
141
130
|
},
|
142
|
-
|
143
|
-
|
131
|
+
|
132
|
+
ForXStatement(path, state) {
|
133
|
+
const left = path.get("left");
|
144
134
|
|
145
135
|
if (left.isPattern() || left.isIdentifier()) {
|
146
136
|
state.constantViolations.push(path);
|
147
137
|
}
|
148
138
|
},
|
139
|
+
|
149
140
|
ExportDeclaration: {
|
150
|
-
exit
|
151
|
-
|
152
|
-
|
153
|
-
|
141
|
+
exit(path) {
|
142
|
+
const {
|
143
|
+
node,
|
144
|
+
scope
|
145
|
+
} = path;
|
146
|
+
const declar = node.declaration;
|
154
147
|
|
155
148
|
if (t().isClassDeclaration(declar) || t().isFunctionDeclaration(declar)) {
|
156
|
-
|
157
|
-
if (!
|
158
|
-
|
149
|
+
const id = declar.id;
|
150
|
+
if (!id) return;
|
151
|
+
const binding = scope.getBinding(id.name);
|
159
152
|
if (binding) binding.reference(path);
|
160
153
|
} else if (t().isVariableDeclaration(declar)) {
|
161
|
-
|
162
|
-
|
163
|
-
for (var _i4 = 0; _i4 < _arr4.length; _i4++) {
|
164
|
-
var decl = _arr4[_i4];
|
165
|
-
var ids = t().getBindingIdentifiers(decl);
|
166
|
-
|
167
|
-
for (var name in ids) {
|
168
|
-
var _binding = scope.getBinding(name);
|
154
|
+
for (const decl of declar.declarations) {
|
155
|
+
const ids = t().getBindingIdentifiers(decl);
|
169
156
|
|
170
|
-
|
157
|
+
for (const name in ids) {
|
158
|
+
const binding = scope.getBinding(name);
|
159
|
+
if (binding) binding.reference(path);
|
171
160
|
}
|
172
161
|
}
|
173
162
|
}
|
174
163
|
}
|
164
|
+
|
175
165
|
},
|
176
|
-
|
166
|
+
|
167
|
+
LabeledStatement(path) {
|
177
168
|
path.scope.getProgramParent().addGlobal(path.node);
|
178
169
|
path.scope.getBlockParent().registerDeclaration(path);
|
179
170
|
},
|
180
|
-
|
171
|
+
|
172
|
+
AssignmentExpression(path, state) {
|
181
173
|
state.assignments.push(path);
|
182
174
|
},
|
183
|
-
|
175
|
+
|
176
|
+
UpdateExpression(path, state) {
|
184
177
|
state.constantViolations.push(path);
|
185
178
|
},
|
186
|
-
|
179
|
+
|
180
|
+
UnaryExpression(path, state) {
|
187
181
|
if (path.node.operator === "delete") {
|
188
182
|
state.constantViolations.push(path);
|
189
183
|
}
|
190
184
|
},
|
191
|
-
|
192
|
-
|
185
|
+
|
186
|
+
BlockScoped(path) {
|
187
|
+
let scope = path.scope;
|
193
188
|
if (scope.path === path) scope = scope.parent;
|
194
189
|
scope.getBlockParent().registerDeclaration(path);
|
195
190
|
},
|
196
|
-
|
197
|
-
|
191
|
+
|
192
|
+
ClassDeclaration(path) {
|
193
|
+
const id = path.node.id;
|
198
194
|
if (!id) return;
|
199
|
-
|
195
|
+
const name = id.name;
|
200
196
|
path.scope.bindings[name] = path.scope.getBinding(name);
|
201
197
|
},
|
202
|
-
Block: function Block(path) {
|
203
|
-
var paths = path.get("body");
|
204
|
-
var _arr5 = paths;
|
205
198
|
|
206
|
-
|
207
|
-
|
199
|
+
Block(path) {
|
200
|
+
const paths = path.get("body");
|
208
201
|
|
202
|
+
for (const bodyPath of paths) {
|
209
203
|
if (bodyPath.isFunctionDeclaration()) {
|
210
204
|
path.scope.getBlockParent().registerDeclaration(bodyPath);
|
211
205
|
}
|
212
206
|
}
|
213
207
|
}
|
208
|
+
|
214
209
|
};
|
215
|
-
|
210
|
+
let uid = 0;
|
216
211
|
|
217
|
-
|
218
|
-
|
219
|
-
|
212
|
+
class Scope {
|
213
|
+
constructor(path) {
|
214
|
+
const {
|
215
|
+
node
|
216
|
+
} = path;
|
220
217
|
|
221
|
-
|
218
|
+
const cached = _cache.scope.get(node);
|
222
219
|
|
223
220
|
if (cached && cached.path === path) {
|
224
221
|
return cached;
|
@@ -232,52 +229,59 @@ var Scope = function () {
|
|
232
229
|
this.labels = new Map();
|
233
230
|
}
|
234
231
|
|
235
|
-
|
232
|
+
get parent() {
|
233
|
+
const parent = this.path.findParent(p => p.isScope());
|
234
|
+
return parent && parent.scope;
|
235
|
+
}
|
236
236
|
|
237
|
-
|
237
|
+
get parentBlock() {
|
238
|
+
return this.path.parent;
|
239
|
+
}
|
240
|
+
|
241
|
+
get hub() {
|
242
|
+
return this.path.hub;
|
243
|
+
}
|
244
|
+
|
245
|
+
traverse(node, opts, state) {
|
238
246
|
(0, _index.default)(node, opts, this, state, this.path);
|
239
|
-
}
|
247
|
+
}
|
240
248
|
|
241
|
-
|
242
|
-
|
249
|
+
generateDeclaredUidIdentifier(name) {
|
250
|
+
const id = this.generateUidIdentifier(name);
|
243
251
|
this.push({
|
244
|
-
id
|
252
|
+
id
|
245
253
|
});
|
246
254
|
return t().cloneNode(id);
|
247
|
-
}
|
255
|
+
}
|
248
256
|
|
249
|
-
|
257
|
+
generateUidIdentifier(name) {
|
250
258
|
return t().identifier(this.generateUid(name));
|
251
|
-
}
|
252
|
-
|
253
|
-
_proto.generateUid = function generateUid(name) {
|
254
|
-
if (name === void 0) {
|
255
|
-
name = "temp";
|
256
|
-
}
|
259
|
+
}
|
257
260
|
|
261
|
+
generateUid(name = "temp") {
|
258
262
|
name = t().toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
259
|
-
|
260
|
-
|
263
|
+
let uid;
|
264
|
+
let i = 0;
|
261
265
|
|
262
266
|
do {
|
263
267
|
uid = this._generateUid(name, i);
|
264
268
|
i++;
|
265
269
|
} while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
|
266
270
|
|
267
|
-
|
271
|
+
const program = this.getProgramParent();
|
268
272
|
program.references[uid] = true;
|
269
273
|
program.uids[uid] = true;
|
270
274
|
return uid;
|
271
|
-
}
|
275
|
+
}
|
272
276
|
|
273
|
-
|
274
|
-
|
277
|
+
_generateUid(name, i) {
|
278
|
+
let id = name;
|
275
279
|
if (i > 1) id += i;
|
276
|
-
return
|
277
|
-
}
|
280
|
+
return `_${id}`;
|
281
|
+
}
|
278
282
|
|
279
|
-
|
280
|
-
|
283
|
+
generateUidBasedOnNode(parent, defaultName) {
|
284
|
+
let node = parent;
|
281
285
|
|
282
286
|
if (t().isAssignmentExpression(parent)) {
|
283
287
|
node = parent.left;
|
@@ -287,24 +291,24 @@ var Scope = function () {
|
|
287
291
|
node = node.key;
|
288
292
|
}
|
289
293
|
|
290
|
-
|
294
|
+
const parts = [];
|
291
295
|
gatherNodeParts(node, parts);
|
292
|
-
|
296
|
+
let id = parts.join("$");
|
293
297
|
id = id.replace(/^_/, "") || defaultName || "ref";
|
294
298
|
return this.generateUid(id.slice(0, 20));
|
295
|
-
}
|
299
|
+
}
|
296
300
|
|
297
|
-
|
301
|
+
generateUidIdentifierBasedOnNode(parent, defaultName) {
|
298
302
|
return t().identifier(this.generateUidBasedOnNode(parent, defaultName));
|
299
|
-
}
|
303
|
+
}
|
300
304
|
|
301
|
-
|
305
|
+
isStatic(node) {
|
302
306
|
if (t().isThisExpression(node) || t().isSuper(node)) {
|
303
307
|
return true;
|
304
308
|
}
|
305
309
|
|
306
310
|
if (t().isIdentifier(node)) {
|
307
|
-
|
311
|
+
const binding = this.getBinding(node.name);
|
308
312
|
|
309
313
|
if (binding) {
|
310
314
|
return binding.constant;
|
@@ -314,62 +318,62 @@ var Scope = function () {
|
|
314
318
|
}
|
315
319
|
|
316
320
|
return false;
|
317
|
-
}
|
321
|
+
}
|
318
322
|
|
319
|
-
|
323
|
+
maybeGenerateMemoised(node, dontPush) {
|
320
324
|
if (this.isStatic(node)) {
|
321
325
|
return null;
|
322
326
|
} else {
|
323
|
-
|
327
|
+
const id = this.generateUidIdentifierBasedOnNode(node);
|
324
328
|
|
325
329
|
if (!dontPush) {
|
326
330
|
this.push({
|
327
|
-
id
|
331
|
+
id
|
328
332
|
});
|
329
|
-
return t().cloneNode(
|
333
|
+
return t().cloneNode(id);
|
330
334
|
}
|
331
335
|
|
332
|
-
return
|
336
|
+
return id;
|
333
337
|
}
|
334
|
-
}
|
338
|
+
}
|
335
339
|
|
336
|
-
|
340
|
+
checkBlockScopedCollisions(local, kind, name, id) {
|
337
341
|
if (kind === "param") return;
|
338
342
|
if (local.kind === "local") return;
|
339
343
|
if (kind === "hoisted" && local.kind === "let") return;
|
340
|
-
|
344
|
+
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && (kind === "let" || kind === "const");
|
341
345
|
|
342
346
|
if (duplicate) {
|
343
|
-
throw this.hub.file.buildCodeFrameError(id,
|
347
|
+
throw this.hub.file.buildCodeFrameError(id, `Duplicate declaration "${name}"`, TypeError);
|
344
348
|
}
|
345
|
-
}
|
349
|
+
}
|
346
350
|
|
347
|
-
|
348
|
-
|
351
|
+
rename(oldName, newName, block) {
|
352
|
+
const binding = this.getBinding(oldName);
|
349
353
|
|
350
354
|
if (binding) {
|
351
355
|
newName = newName || this.generateUidIdentifier(oldName).name;
|
352
356
|
return new _renamer.default(binding, oldName, newName).rename(block);
|
353
357
|
}
|
354
|
-
}
|
358
|
+
}
|
355
359
|
|
356
|
-
|
360
|
+
_renameFromMap(map, oldName, newName, value) {
|
357
361
|
if (map[oldName]) {
|
358
362
|
map[newName] = value;
|
359
363
|
map[oldName] = null;
|
360
364
|
}
|
361
|
-
}
|
365
|
+
}
|
362
366
|
|
363
|
-
|
364
|
-
|
367
|
+
dump() {
|
368
|
+
const sep = (0, _repeat().default)("-", 60);
|
365
369
|
console.log(sep);
|
366
|
-
|
370
|
+
let scope = this;
|
367
371
|
|
368
372
|
do {
|
369
373
|
console.log("#", scope.block.type);
|
370
374
|
|
371
|
-
for (
|
372
|
-
|
375
|
+
for (const name in scope.bindings) {
|
376
|
+
const binding = scope.bindings[name];
|
373
377
|
console.log(" -", name, {
|
374
378
|
constant: binding.constant,
|
375
379
|
references: binding.references,
|
@@ -380,13 +384,13 @@ var Scope = function () {
|
|
380
384
|
} while (scope = scope.parent);
|
381
385
|
|
382
386
|
console.log(sep);
|
383
|
-
}
|
387
|
+
}
|
384
388
|
|
385
|
-
|
386
|
-
|
389
|
+
toArray(node, i) {
|
390
|
+
const file = this.hub.file;
|
387
391
|
|
388
392
|
if (t().isIdentifier(node)) {
|
389
|
-
|
393
|
+
const binding = this.getBinding(node.name);
|
390
394
|
|
391
395
|
if (binding && binding.constant && binding.path.isGenericType("Array")) {
|
392
396
|
return node;
|
@@ -403,8 +407,8 @@ var Scope = function () {
|
|
403
407
|
return t().callExpression(t().memberExpression(t().memberExpression(t().memberExpression(t().identifier("Array"), t().identifier("prototype")), t().identifier("slice")), t().identifier("call")), [node]);
|
404
408
|
}
|
405
409
|
|
406
|
-
|
407
|
-
|
410
|
+
let helperName;
|
411
|
+
const args = [node];
|
408
412
|
|
409
413
|
if (i === true) {
|
410
414
|
helperName = "toConsumableArray";
|
@@ -416,115 +420,90 @@ var Scope = function () {
|
|
416
420
|
}
|
417
421
|
|
418
422
|
return t().callExpression(file.addHelper(helperName), args);
|
419
|
-
}
|
423
|
+
}
|
420
424
|
|
421
|
-
|
425
|
+
hasLabel(name) {
|
422
426
|
return !!this.getLabel(name);
|
423
|
-
}
|
427
|
+
}
|
424
428
|
|
425
|
-
|
429
|
+
getLabel(name) {
|
426
430
|
return this.labels.get(name);
|
427
|
-
}
|
431
|
+
}
|
428
432
|
|
429
|
-
|
433
|
+
registerLabel(path) {
|
430
434
|
this.labels.set(path.node.label.name, path);
|
431
|
-
}
|
432
|
-
|
433
|
-
_proto.registerDeclaration = function registerDeclaration(path) {
|
434
|
-
if (path.isFlow()) return;
|
435
|
+
}
|
435
436
|
|
437
|
+
registerDeclaration(path) {
|
436
438
|
if (path.isLabeledStatement()) {
|
437
439
|
this.registerLabel(path);
|
438
440
|
} else if (path.isFunctionDeclaration()) {
|
439
441
|
this.registerBinding("hoisted", path.get("id"), path);
|
440
442
|
} else if (path.isVariableDeclaration()) {
|
441
|
-
|
442
|
-
var _arr6 = declarations;
|
443
|
+
const declarations = path.get("declarations");
|
443
444
|
|
444
|
-
for (
|
445
|
-
var declar = _arr6[_i6];
|
445
|
+
for (const declar of declarations) {
|
446
446
|
this.registerBinding(path.node.kind, declar);
|
447
447
|
}
|
448
448
|
} else if (path.isClassDeclaration()) {
|
449
449
|
this.registerBinding("let", path);
|
450
450
|
} else if (path.isImportDeclaration()) {
|
451
|
-
|
452
|
-
var _arr7 = specifiers;
|
451
|
+
const specifiers = path.get("specifiers");
|
453
452
|
|
454
|
-
for (
|
455
|
-
var specifier = _arr7[_i7];
|
453
|
+
for (const specifier of specifiers) {
|
456
454
|
this.registerBinding("module", specifier);
|
457
455
|
}
|
458
456
|
} else if (path.isExportDeclaration()) {
|
459
|
-
|
457
|
+
const declar = path.get("declaration");
|
460
458
|
|
461
|
-
if (
|
462
|
-
this.registerDeclaration(
|
459
|
+
if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
|
460
|
+
this.registerDeclaration(declar);
|
463
461
|
}
|
464
462
|
} else {
|
465
463
|
this.registerBinding("unknown", path);
|
466
464
|
}
|
467
|
-
}
|
465
|
+
}
|
468
466
|
|
469
|
-
|
467
|
+
buildUndefinedNode() {
|
470
468
|
if (this.hasBinding("undefined")) {
|
471
469
|
return t().unaryExpression("void", t().numericLiteral(0), true);
|
472
470
|
} else {
|
473
471
|
return t().identifier("undefined");
|
474
472
|
}
|
475
|
-
}
|
473
|
+
}
|
476
474
|
|
477
|
-
|
478
|
-
|
475
|
+
registerConstantViolation(path) {
|
476
|
+
const ids = path.getBindingIdentifiers();
|
479
477
|
|
480
|
-
for (
|
481
|
-
|
478
|
+
for (const name in ids) {
|
479
|
+
const binding = this.getBinding(name);
|
482
480
|
if (binding) binding.reassign(path);
|
483
481
|
}
|
484
|
-
}
|
485
|
-
|
486
|
-
_proto.registerBinding = function registerBinding(kind, path, bindingPath) {
|
487
|
-
if (bindingPath === void 0) {
|
488
|
-
bindingPath = path;
|
489
|
-
}
|
482
|
+
}
|
490
483
|
|
484
|
+
registerBinding(kind, path, bindingPath = path) {
|
491
485
|
if (!kind) throw new ReferenceError("no `kind`");
|
492
486
|
|
493
487
|
if (path.isVariableDeclaration()) {
|
494
|
-
|
495
|
-
|
496
|
-
for (var _iterator = declarators, _isArray = Array.isArray(_iterator), _i8 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
497
|
-
var _ref;
|
498
|
-
|
499
|
-
if (_isArray) {
|
500
|
-
if (_i8 >= _iterator.length) break;
|
501
|
-
_ref = _iterator[_i8++];
|
502
|
-
} else {
|
503
|
-
_i8 = _iterator.next();
|
504
|
-
if (_i8.done) break;
|
505
|
-
_ref = _i8.value;
|
506
|
-
}
|
488
|
+
const declarators = path.get("declarations");
|
507
489
|
|
508
|
-
|
490
|
+
for (const declar of declarators) {
|
509
491
|
this.registerBinding(kind, declar);
|
510
492
|
}
|
511
493
|
|
512
494
|
return;
|
513
495
|
}
|
514
496
|
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
for (var name in ids) {
|
519
|
-
var _arr8 = ids[name];
|
497
|
+
const parent = this.getProgramParent();
|
498
|
+
const ids = path.getBindingIdentifiers(true);
|
520
499
|
|
521
|
-
|
522
|
-
|
523
|
-
|
500
|
+
for (const name in ids) {
|
501
|
+
for (const id of ids[name]) {
|
502
|
+
const local = this.getOwnBinding(name);
|
524
503
|
|
525
504
|
if (local) {
|
526
|
-
if (local.identifier ===
|
527
|
-
this.checkBlockScopedCollisions(local, kind, name,
|
505
|
+
if (local.identifier === id) continue;
|
506
|
+
this.checkBlockScopedCollisions(local, kind, name, id);
|
528
507
|
}
|
529
508
|
|
530
509
|
parent.references[name] = true;
|
@@ -532,8 +511,8 @@ var Scope = function () {
|
|
532
511
|
if (local) {
|
533
512
|
this.registerConstantViolation(bindingPath);
|
534
513
|
} else {
|
535
|
-
this.bindings[name] = new
|
536
|
-
identifier:
|
514
|
+
this.bindings[name] = new _binding.default({
|
515
|
+
identifier: id,
|
537
516
|
scope: this,
|
538
517
|
path: bindingPath,
|
539
518
|
kind: kind
|
@@ -541,45 +520,45 @@ var Scope = function () {
|
|
541
520
|
}
|
542
521
|
}
|
543
522
|
}
|
544
|
-
}
|
523
|
+
}
|
545
524
|
|
546
|
-
|
525
|
+
addGlobal(node) {
|
547
526
|
this.globals[node.name] = node;
|
548
|
-
}
|
527
|
+
}
|
549
528
|
|
550
|
-
|
551
|
-
|
529
|
+
hasUid(name) {
|
530
|
+
let scope = this;
|
552
531
|
|
553
532
|
do {
|
554
533
|
if (scope.uids[name]) return true;
|
555
534
|
} while (scope = scope.parent);
|
556
535
|
|
557
536
|
return false;
|
558
|
-
}
|
537
|
+
}
|
559
538
|
|
560
|
-
|
561
|
-
|
539
|
+
hasGlobal(name) {
|
540
|
+
let scope = this;
|
562
541
|
|
563
542
|
do {
|
564
543
|
if (scope.globals[name]) return true;
|
565
544
|
} while (scope = scope.parent);
|
566
545
|
|
567
546
|
return false;
|
568
|
-
}
|
547
|
+
}
|
569
548
|
|
570
|
-
|
571
|
-
|
549
|
+
hasReference(name) {
|
550
|
+
let scope = this;
|
572
551
|
|
573
552
|
do {
|
574
553
|
if (scope.references[name]) return true;
|
575
554
|
} while (scope = scope.parent);
|
576
555
|
|
577
556
|
return false;
|
578
|
-
}
|
557
|
+
}
|
579
558
|
|
580
|
-
|
559
|
+
isPure(node, constantsOnly) {
|
581
560
|
if (t().isIdentifier(node)) {
|
582
|
-
|
561
|
+
const binding = this.getBinding(node.name);
|
583
562
|
if (!binding) return false;
|
584
563
|
if (constantsOnly) return binding.constant;
|
585
564
|
return true;
|
@@ -590,19 +569,7 @@ var Scope = function () {
|
|
590
569
|
|
591
570
|
return this.isPure(node.body, constantsOnly);
|
592
571
|
} else if (t().isClassBody(node)) {
|
593
|
-
for (
|
594
|
-
var _ref2;
|
595
|
-
|
596
|
-
if (_isArray2) {
|
597
|
-
if (_i10 >= _iterator2.length) break;
|
598
|
-
_ref2 = _iterator2[_i10++];
|
599
|
-
} else {
|
600
|
-
_i10 = _iterator2.next();
|
601
|
-
if (_i10.done) break;
|
602
|
-
_ref2 = _i10.value;
|
603
|
-
}
|
604
|
-
|
605
|
-
var method = _ref2;
|
572
|
+
for (const method of node.body) {
|
606
573
|
if (!this.isPure(method, constantsOnly)) return false;
|
607
574
|
}
|
608
575
|
|
@@ -610,19 +577,13 @@ var Scope = function () {
|
|
610
577
|
} else if (t().isBinary(node)) {
|
611
578
|
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
612
579
|
} else if (t().isArrayExpression(node)) {
|
613
|
-
|
614
|
-
|
615
|
-
for (var _i11 = 0; _i11 < _arr9.length; _i11++) {
|
616
|
-
var elem = _arr9[_i11];
|
580
|
+
for (const elem of node.elements) {
|
617
581
|
if (!this.isPure(elem, constantsOnly)) return false;
|
618
582
|
}
|
619
583
|
|
620
584
|
return true;
|
621
585
|
} else if (t().isObjectExpression(node)) {
|
622
|
-
|
623
|
-
|
624
|
-
for (var _i12 = 0; _i12 < _arr10.length; _i12++) {
|
625
|
-
var prop = _arr10[_i12];
|
586
|
+
for (const prop of node.properties) {
|
626
587
|
if (!this.isPure(prop, constantsOnly)) return false;
|
627
588
|
}
|
628
589
|
|
@@ -639,10 +600,7 @@ var Scope = function () {
|
|
639
600
|
} else if (t().isTaggedTemplateExpression(node)) {
|
640
601
|
return t().matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
|
641
602
|
} else if (t().isTemplateLiteral(node)) {
|
642
|
-
|
643
|
-
|
644
|
-
for (var _i13 = 0; _i13 < _arr11.length; _i13++) {
|
645
|
-
var expression = _arr11[_i13];
|
603
|
+
for (const expression of node.expressions) {
|
646
604
|
if (!this.isPure(expression, constantsOnly)) return false;
|
647
605
|
}
|
648
606
|
|
@@ -650,36 +608,36 @@ var Scope = function () {
|
|
650
608
|
} else {
|
651
609
|
return t().isPureish(node);
|
652
610
|
}
|
653
|
-
}
|
611
|
+
}
|
654
612
|
|
655
|
-
|
613
|
+
setData(key, val) {
|
656
614
|
return this.data[key] = val;
|
657
|
-
}
|
615
|
+
}
|
658
616
|
|
659
|
-
|
660
|
-
|
617
|
+
getData(key) {
|
618
|
+
let scope = this;
|
661
619
|
|
662
620
|
do {
|
663
|
-
|
621
|
+
const data = scope.data[key];
|
664
622
|
if (data != null) return data;
|
665
623
|
} while (scope = scope.parent);
|
666
|
-
}
|
624
|
+
}
|
667
625
|
|
668
|
-
|
669
|
-
|
626
|
+
removeData(key) {
|
627
|
+
let scope = this;
|
670
628
|
|
671
629
|
do {
|
672
|
-
|
630
|
+
const data = scope.data[key];
|
673
631
|
if (data != null) scope.data[key] = null;
|
674
632
|
} while (scope = scope.parent);
|
675
|
-
}
|
633
|
+
}
|
676
634
|
|
677
|
-
|
635
|
+
init() {
|
678
636
|
if (!this.references) this.crawl();
|
679
|
-
}
|
637
|
+
}
|
680
638
|
|
681
|
-
|
682
|
-
|
639
|
+
crawl() {
|
640
|
+
const path = this.path;
|
683
641
|
this.references = Object.create(null);
|
684
642
|
this.bindings = Object.create(null);
|
685
643
|
this.globals = Object.create(null);
|
@@ -687,11 +645,8 @@ var Scope = function () {
|
|
687
645
|
this.data = Object.create(null);
|
688
646
|
|
689
647
|
if (path.isLoop()) {
|
690
|
-
|
691
|
-
|
692
|
-
for (var _i14 = 0; _i14 < _arr12.length; _i14++) {
|
693
|
-
var key = _arr12[_i14];
|
694
|
-
var node = path.get(key);
|
648
|
+
for (const key of t().FOR_INIT_KEYS) {
|
649
|
+
const node = path.get(key);
|
695
650
|
if (node.isBlockScoped()) this.registerBinding(node.node.kind, node);
|
696
651
|
}
|
697
652
|
}
|
@@ -709,21 +664,9 @@ var Scope = function () {
|
|
709
664
|
}
|
710
665
|
|
711
666
|
if (path.isFunction()) {
|
712
|
-
|
713
|
-
|
714
|
-
for (var _iterator3 = params, _isArray3 = Array.isArray(_iterator3), _i15 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
|
715
|
-
var _ref3;
|
667
|
+
const params = path.get("params");
|
716
668
|
|
717
|
-
|
718
|
-
if (_i15 >= _iterator3.length) break;
|
719
|
-
_ref3 = _iterator3[_i15++];
|
720
|
-
} else {
|
721
|
-
_i15 = _iterator3.next();
|
722
|
-
if (_i15.done) break;
|
723
|
-
_ref3 = _i15.value;
|
724
|
-
}
|
725
|
-
|
726
|
-
var param = _ref3;
|
669
|
+
for (const param of params) {
|
727
670
|
this.registerBinding("param", param);
|
728
671
|
}
|
729
672
|
}
|
@@ -732,9 +675,9 @@ var Scope = function () {
|
|
732
675
|
this.registerBinding("let", path);
|
733
676
|
}
|
734
677
|
|
735
|
-
|
678
|
+
const parent = this.getProgramParent();
|
736
679
|
if (parent.crawling) return;
|
737
|
-
|
680
|
+
const state = {
|
738
681
|
references: [],
|
739
682
|
constantViolations: [],
|
740
683
|
assignments: []
|
@@ -743,47 +686,21 @@ var Scope = function () {
|
|
743
686
|
path.traverse(collectorVisitor, state);
|
744
687
|
this.crawling = false;
|
745
688
|
|
746
|
-
for (
|
747
|
-
|
748
|
-
|
749
|
-
if (_isArray4) {
|
750
|
-
if (_i16 >= _iterator4.length) break;
|
751
|
-
_ref4 = _iterator4[_i16++];
|
752
|
-
} else {
|
753
|
-
_i16 = _iterator4.next();
|
754
|
-
if (_i16.done) break;
|
755
|
-
_ref4 = _i16.value;
|
756
|
-
}
|
757
|
-
|
758
|
-
var _path = _ref4;
|
759
|
-
|
760
|
-
var ids = _path.getBindingIdentifiers();
|
689
|
+
for (const path of state.assignments) {
|
690
|
+
const ids = path.getBindingIdentifiers();
|
691
|
+
let programParent;
|
761
692
|
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
if (_path.scope.getBinding(name)) continue;
|
766
|
-
programParent = programParent || _path.scope.getProgramParent();
|
693
|
+
for (const name in ids) {
|
694
|
+
if (path.scope.getBinding(name)) continue;
|
695
|
+
programParent = programParent || path.scope.getProgramParent();
|
767
696
|
programParent.addGlobal(ids[name]);
|
768
697
|
}
|
769
698
|
|
770
|
-
|
699
|
+
path.scope.registerConstantViolation(path);
|
771
700
|
}
|
772
701
|
|
773
|
-
for (
|
774
|
-
|
775
|
-
|
776
|
-
if (_isArray5) {
|
777
|
-
if (_i17 >= _iterator5.length) break;
|
778
|
-
_ref5 = _iterator5[_i17++];
|
779
|
-
} else {
|
780
|
-
_i17 = _iterator5.next();
|
781
|
-
if (_i17.done) break;
|
782
|
-
_ref5 = _i17.value;
|
783
|
-
}
|
784
|
-
|
785
|
-
var ref = _ref5;
|
786
|
-
var binding = ref.scope.getBinding(ref.node.name);
|
702
|
+
for (const ref of state.references) {
|
703
|
+
const binding = ref.scope.getBinding(ref.node.name);
|
787
704
|
|
788
705
|
if (binding) {
|
789
706
|
binding.reference(ref);
|
@@ -792,26 +709,13 @@ var Scope = function () {
|
|
792
709
|
}
|
793
710
|
}
|
794
711
|
|
795
|
-
for (
|
796
|
-
|
797
|
-
|
798
|
-
if (_isArray6) {
|
799
|
-
if (_i18 >= _iterator6.length) break;
|
800
|
-
_ref6 = _iterator6[_i18++];
|
801
|
-
} else {
|
802
|
-
_i18 = _iterator6.next();
|
803
|
-
if (_i18.done) break;
|
804
|
-
_ref6 = _i18.value;
|
805
|
-
}
|
806
|
-
|
807
|
-
var _path2 = _ref6;
|
808
|
-
|
809
|
-
_path2.scope.registerConstantViolation(_path2);
|
712
|
+
for (const path of state.constantViolations) {
|
713
|
+
path.scope.registerConstantViolation(path);
|
810
714
|
}
|
811
|
-
}
|
715
|
+
}
|
812
716
|
|
813
|
-
|
814
|
-
|
717
|
+
push(opts) {
|
718
|
+
let path = this.path;
|
815
719
|
|
816
720
|
if (!path.isBlockStatement() && !path.isProgram()) {
|
817
721
|
path = this.getBlockParent().path;
|
@@ -826,29 +730,26 @@ var Scope = function () {
|
|
826
730
|
path = path.get("body");
|
827
731
|
}
|
828
732
|
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
733
|
+
const unique = opts.unique;
|
734
|
+
const kind = opts.kind || "var";
|
735
|
+
const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
|
736
|
+
const dataKey = `declaration:${kind}:${blockHoist}`;
|
737
|
+
let declarPath = !unique && path.getData(dataKey);
|
834
738
|
|
835
739
|
if (!declarPath) {
|
836
|
-
|
740
|
+
const declar = t().variableDeclaration(kind, []);
|
837
741
|
declar._blockHoist = blockHoist;
|
838
|
-
|
839
|
-
var _path$unshiftContaine = path.unshiftContainer("body", [declar]);
|
840
|
-
|
841
|
-
declarPath = _path$unshiftContaine[0];
|
742
|
+
[declarPath] = path.unshiftContainer("body", [declar]);
|
842
743
|
if (!unique) path.setData(dataKey, declarPath);
|
843
744
|
}
|
844
745
|
|
845
|
-
|
746
|
+
const declarator = t().variableDeclarator(opts.id, opts.init);
|
846
747
|
declarPath.node.declarations.push(declarator);
|
847
748
|
this.registerBinding(kind, declarPath.get("declarations").pop());
|
848
|
-
}
|
749
|
+
}
|
849
750
|
|
850
|
-
|
851
|
-
|
751
|
+
getProgramParent() {
|
752
|
+
let scope = this;
|
852
753
|
|
853
754
|
do {
|
854
755
|
if (scope.path.isProgram()) {
|
@@ -857,10 +758,10 @@ var Scope = function () {
|
|
857
758
|
} while (scope = scope.parent);
|
858
759
|
|
859
760
|
throw new Error("Couldn't find a Program");
|
860
|
-
}
|
761
|
+
}
|
861
762
|
|
862
|
-
|
863
|
-
|
763
|
+
getFunctionParent() {
|
764
|
+
let scope = this;
|
864
765
|
|
865
766
|
do {
|
866
767
|
if (scope.path.isFunctionParent()) {
|
@@ -869,10 +770,10 @@ var Scope = function () {
|
|
869
770
|
} while (scope = scope.parent);
|
870
771
|
|
871
772
|
return null;
|
872
|
-
}
|
773
|
+
}
|
873
774
|
|
874
|
-
|
875
|
-
|
775
|
+
getBlockParent() {
|
776
|
+
let scope = this;
|
876
777
|
|
877
778
|
do {
|
878
779
|
if (scope.path.isBlockParent()) {
|
@@ -881,11 +782,11 @@ var Scope = function () {
|
|
881
782
|
} while (scope = scope.parent);
|
882
783
|
|
883
784
|
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
884
|
-
}
|
785
|
+
}
|
885
786
|
|
886
|
-
|
887
|
-
|
888
|
-
|
787
|
+
getAllBindings() {
|
788
|
+
const ids = Object.create(null);
|
789
|
+
let scope = this;
|
889
790
|
|
890
791
|
do {
|
891
792
|
(0, _defaults().default)(ids, scope.bindings);
|
@@ -893,19 +794,17 @@ var Scope = function () {
|
|
893
794
|
} while (scope);
|
894
795
|
|
895
796
|
return ids;
|
896
|
-
}
|
797
|
+
}
|
897
798
|
|
898
|
-
|
899
|
-
|
900
|
-
var _arr13 = arguments;
|
799
|
+
getAllBindingsOfKind() {
|
800
|
+
const ids = Object.create(null);
|
901
801
|
|
902
|
-
for (
|
903
|
-
|
904
|
-
var scope = this;
|
802
|
+
for (const kind of arguments) {
|
803
|
+
let scope = this;
|
905
804
|
|
906
805
|
do {
|
907
|
-
for (
|
908
|
-
|
806
|
+
for (const name in scope.bindings) {
|
807
|
+
const binding = scope.bindings[name];
|
909
808
|
if (binding.kind === kind) ids[name] = binding;
|
910
809
|
}
|
911
810
|
|
@@ -914,40 +813,40 @@ var Scope = function () {
|
|
914
813
|
}
|
915
814
|
|
916
815
|
return ids;
|
917
|
-
}
|
816
|
+
}
|
918
817
|
|
919
|
-
|
818
|
+
bindingIdentifierEquals(name, node) {
|
920
819
|
return this.getBindingIdentifier(name) === node;
|
921
|
-
}
|
820
|
+
}
|
922
821
|
|
923
|
-
|
924
|
-
|
822
|
+
getBinding(name) {
|
823
|
+
let scope = this;
|
925
824
|
|
926
825
|
do {
|
927
|
-
|
826
|
+
const binding = scope.getOwnBinding(name);
|
928
827
|
if (binding) return binding;
|
929
828
|
} while (scope = scope.parent);
|
930
|
-
}
|
829
|
+
}
|
931
830
|
|
932
|
-
|
831
|
+
getOwnBinding(name) {
|
933
832
|
return this.bindings[name];
|
934
|
-
}
|
833
|
+
}
|
935
834
|
|
936
|
-
|
937
|
-
|
835
|
+
getBindingIdentifier(name) {
|
836
|
+
const info = this.getBinding(name);
|
938
837
|
return info && info.identifier;
|
939
|
-
}
|
838
|
+
}
|
940
839
|
|
941
|
-
|
942
|
-
|
840
|
+
getOwnBindingIdentifier(name) {
|
841
|
+
const binding = this.bindings[name];
|
943
842
|
return binding && binding.identifier;
|
944
|
-
}
|
843
|
+
}
|
945
844
|
|
946
|
-
|
845
|
+
hasOwnBinding(name) {
|
947
846
|
return !!this.getOwnBinding(name);
|
948
|
-
}
|
847
|
+
}
|
949
848
|
|
950
|
-
|
849
|
+
hasBinding(name, noGlobals) {
|
951
850
|
if (!name) return false;
|
952
851
|
if (this.hasOwnBinding(name)) return true;
|
953
852
|
if (this.parentHasBinding(name, noGlobals)) return true;
|
@@ -955,64 +854,43 @@ var Scope = function () {
|
|
955
854
|
if (!noGlobals && (0, _includes().default)(Scope.globals, name)) return true;
|
956
855
|
if (!noGlobals && (0, _includes().default)(Scope.contextVariables, name)) return true;
|
957
856
|
return false;
|
958
|
-
}
|
857
|
+
}
|
959
858
|
|
960
|
-
|
859
|
+
parentHasBinding(name, noGlobals) {
|
961
860
|
return this.parent && this.parent.hasBinding(name, noGlobals);
|
962
|
-
}
|
861
|
+
}
|
963
862
|
|
964
|
-
|
965
|
-
|
863
|
+
moveBindingTo(name, scope) {
|
864
|
+
const info = this.getBinding(name);
|
966
865
|
|
967
866
|
if (info) {
|
968
867
|
info.scope.removeOwnBinding(name);
|
969
868
|
info.scope = scope;
|
970
869
|
scope.bindings[name] = info;
|
971
870
|
}
|
972
|
-
}
|
871
|
+
}
|
973
872
|
|
974
|
-
|
873
|
+
removeOwnBinding(name) {
|
975
874
|
delete this.bindings[name];
|
976
|
-
}
|
875
|
+
}
|
977
876
|
|
978
|
-
|
979
|
-
|
877
|
+
removeBinding(name) {
|
878
|
+
const info = this.getBinding(name);
|
980
879
|
|
981
880
|
if (info) {
|
982
881
|
info.scope.removeOwnBinding(name);
|
983
882
|
}
|
984
883
|
|
985
|
-
|
884
|
+
let scope = this;
|
986
885
|
|
987
886
|
do {
|
988
887
|
if (scope.uids[name]) {
|
989
888
|
scope.uids[name] = false;
|
990
889
|
}
|
991
890
|
} while (scope = scope.parent);
|
992
|
-
}
|
993
|
-
|
994
|
-
_createClass(Scope, [{
|
995
|
-
key: "parent",
|
996
|
-
get: function get() {
|
997
|
-
var parent = this.path.findParent(function (p) {
|
998
|
-
return p.isScope();
|
999
|
-
});
|
1000
|
-
return parent && parent.scope;
|
1001
|
-
}
|
1002
|
-
}, {
|
1003
|
-
key: "parentBlock",
|
1004
|
-
get: function get() {
|
1005
|
-
return this.path.parent;
|
1006
|
-
}
|
1007
|
-
}, {
|
1008
|
-
key: "hub",
|
1009
|
-
get: function get() {
|
1010
|
-
return this.path.hub;
|
1011
|
-
}
|
1012
|
-
}]);
|
891
|
+
}
|
1013
892
|
|
1014
|
-
|
1015
|
-
}();
|
893
|
+
}
|
1016
894
|
|
1017
895
|
exports.default = Scope;
|
1018
896
|
Scope.globals = Object.keys(_globals().default.builtin);
|