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