@babel/traverse 7.0.0-beta.46 → 7.0.0-beta.47
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 +2 -2
- package/lib/context.js +34 -38
- package/lib/hub.js +6 -3
- package/lib/index.js +7 -7
- package/lib/path/ancestry.js +25 -31
- package/lib/path/comments.js +8 -8
- package/lib/path/context.js +17 -21
- package/lib/path/conversion.js +119 -126
- package/lib/path/evaluation.js +87 -99
- package/lib/path/family.js +26 -36
- package/lib/path/index.js +59 -77
- package/lib/path/inference/index.js +11 -11
- package/lib/path/inference/inferer-reference.js +36 -38
- package/lib/path/inference/inferers.js +14 -14
- package/lib/path/introspection.js +51 -57
- package/lib/path/lib/hoister.js +48 -50
- package/lib/path/lib/removal-hooks.js +2 -2
- package/lib/path/lib/virtual-types.js +75 -45
- package/lib/path/modification.js +33 -42
- package/lib/path/removal.js +3 -7
- package/lib/path/replacement.js +29 -32
- package/lib/scope/binding.js +20 -22
- package/lib/scope/index.js +271 -284
- package/lib/scope/lib/renamer.js +31 -33
- package/lib/visitors.js +59 -73
- package/package.json +9 -9
package/lib/path/evaluation.js
CHANGED
@@ -5,11 +5,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.evaluateTruthy = evaluateTruthy;
|
7
7
|
exports.evaluate = evaluate;
|
8
|
-
|
9
|
-
|
8
|
+
const VALID_CALLEES = ["String", "Number", "Math"];
|
9
|
+
const INVALID_METHODS = ["random"];
|
10
10
|
|
11
11
|
function evaluateTruthy() {
|
12
|
-
|
12
|
+
const res = this.evaluate();
|
13
13
|
if (res.confident) return !!res.value;
|
14
14
|
}
|
15
15
|
|
@@ -20,11 +20,11 @@ function deopt(path, state) {
|
|
20
20
|
}
|
21
21
|
|
22
22
|
function evaluateCached(path, state) {
|
23
|
-
|
24
|
-
|
23
|
+
const node = path.node;
|
24
|
+
const seen = state.seen;
|
25
25
|
|
26
26
|
if (seen.has(node)) {
|
27
|
-
|
27
|
+
const existing = seen.get(node);
|
28
28
|
|
29
29
|
if (existing.resolved) {
|
30
30
|
return existing.value;
|
@@ -33,12 +33,12 @@ function evaluateCached(path, state) {
|
|
33
33
|
return;
|
34
34
|
}
|
35
35
|
} else {
|
36
|
-
|
36
|
+
const item = {
|
37
37
|
resolved: false
|
38
38
|
};
|
39
39
|
seen.set(node, item);
|
40
40
|
|
41
|
-
|
41
|
+
const val = _evaluate(path, state);
|
42
42
|
|
43
43
|
if (state.confident) {
|
44
44
|
item.resolved = true;
|
@@ -51,10 +51,10 @@ function evaluateCached(path, state) {
|
|
51
51
|
|
52
52
|
function _evaluate(path, state) {
|
53
53
|
if (!state.confident) return;
|
54
|
-
|
54
|
+
const node = path.node;
|
55
55
|
|
56
56
|
if (path.isSequenceExpression()) {
|
57
|
-
|
57
|
+
const exprs = path.get("expressions");
|
58
58
|
return evaluateCached(exprs[exprs.length - 1], state);
|
59
59
|
}
|
60
60
|
|
@@ -71,9 +71,9 @@ function _evaluate(path, state) {
|
|
71
71
|
}
|
72
72
|
|
73
73
|
if (path.isTaggedTemplateExpression() && path.get("tag").isMemberExpression()) {
|
74
|
-
|
75
|
-
|
76
|
-
|
74
|
+
const object = path.get("tag.object");
|
75
|
+
const name = object.node.name;
|
76
|
+
const property = path.get("tag.property");
|
77
77
|
|
78
78
|
if (object.isIdentifier() && name === "String" && !path.scope.getBinding(name, true) && property.isIdentifier && property.node.name === "raw") {
|
79
79
|
return evaluateQuasis(path, node.quasi.quasis, state, true);
|
@@ -81,7 +81,7 @@ function _evaluate(path, state) {
|
|
81
81
|
}
|
82
82
|
|
83
83
|
if (path.isConditionalExpression()) {
|
84
|
-
|
84
|
+
const testResult = evaluateCached(path.get("test"), state);
|
85
85
|
if (!state.confident) return;
|
86
86
|
|
87
87
|
if (testResult) {
|
@@ -98,22 +98,21 @@ function _evaluate(path, state) {
|
|
98
98
|
if (path.isMemberExpression() && !path.parentPath.isCallExpression({
|
99
99
|
callee: node
|
100
100
|
})) {
|
101
|
-
|
101
|
+
const property = path.get("property");
|
102
|
+
const object = path.get("object");
|
102
103
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
var value = _object.node.value;
|
107
|
-
var type = typeof value;
|
104
|
+
if (object.isLiteral() && property.isIdentifier()) {
|
105
|
+
const value = object.node.value;
|
106
|
+
const type = typeof value;
|
108
107
|
|
109
108
|
if (type === "number" || type === "string") {
|
110
|
-
return value[
|
109
|
+
return value[property.node.name];
|
111
110
|
}
|
112
111
|
}
|
113
112
|
}
|
114
113
|
|
115
114
|
if (path.isReferencedIdentifier()) {
|
116
|
-
|
115
|
+
const binding = path.scope.getBinding(node.name);
|
117
116
|
|
118
117
|
if (binding && binding.constantViolations.length > 0) {
|
119
118
|
return deopt(binding.path, state);
|
@@ -134,7 +133,7 @@ function _evaluate(path, state) {
|
|
134
133
|
return binding ? deopt(binding.path, state) : NaN;
|
135
134
|
}
|
136
135
|
|
137
|
-
|
136
|
+
const resolved = path.resolve();
|
138
137
|
|
139
138
|
if (resolved === path) {
|
140
139
|
return deopt(path, state);
|
@@ -151,13 +150,13 @@ function _evaluate(path, state) {
|
|
151
150
|
return undefined;
|
152
151
|
}
|
153
152
|
|
154
|
-
|
153
|
+
const argument = path.get("argument");
|
155
154
|
|
156
155
|
if (node.operator === "typeof" && (argument.isFunction() || argument.isClass())) {
|
157
156
|
return "function";
|
158
157
|
}
|
159
158
|
|
160
|
-
|
159
|
+
const arg = evaluateCached(argument, state);
|
161
160
|
if (!state.confident) return;
|
162
161
|
|
163
162
|
switch (node.operator) {
|
@@ -179,8 +178,8 @@ function _evaluate(path, state) {
|
|
179
178
|
}
|
180
179
|
|
181
180
|
if (path.isArrayExpression()) {
|
182
|
-
|
183
|
-
|
181
|
+
const arr = [];
|
182
|
+
const elems = path.get("elements");
|
184
183
|
|
185
184
|
for (var _iterator = elems, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
186
185
|
var _ref;
|
@@ -194,8 +193,8 @@ function _evaluate(path, state) {
|
|
194
193
|
_ref = _i.value;
|
195
194
|
}
|
196
195
|
|
197
|
-
|
198
|
-
|
196
|
+
const elem = _ref;
|
197
|
+
const elemValue = elem.evaluate();
|
199
198
|
|
200
199
|
if (elemValue.confident) {
|
201
200
|
arr.push(elemValue.value);
|
@@ -208,8 +207,8 @@ function _evaluate(path, state) {
|
|
208
207
|
}
|
209
208
|
|
210
209
|
if (path.isObjectExpression()) {
|
211
|
-
|
212
|
-
|
210
|
+
const obj = {};
|
211
|
+
const props = path.get("properties");
|
213
212
|
|
214
213
|
for (var _iterator2 = props, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
|
215
214
|
var _ref2;
|
@@ -223,14 +222,14 @@ function _evaluate(path, state) {
|
|
223
222
|
_ref2 = _i2.value;
|
224
223
|
}
|
225
224
|
|
226
|
-
|
225
|
+
const prop = _ref2;
|
227
226
|
|
228
227
|
if (prop.isObjectMethod() || prop.isSpreadElement()) {
|
229
228
|
return deopt(prop, state);
|
230
229
|
}
|
231
230
|
|
232
|
-
|
233
|
-
|
231
|
+
const keyPath = prop.get("key");
|
232
|
+
let key = keyPath;
|
234
233
|
|
235
234
|
if (prop.node.computed) {
|
236
235
|
key = key.evaluate();
|
@@ -246,28 +245,27 @@ function _evaluate(path, state) {
|
|
246
245
|
key = key.node.value;
|
247
246
|
}
|
248
247
|
|
249
|
-
|
250
|
-
|
251
|
-
var _value = valuePath.evaluate();
|
248
|
+
const valuePath = prop.get("value");
|
249
|
+
let value = valuePath.evaluate();
|
252
250
|
|
253
|
-
if (!
|
251
|
+
if (!value.confident) {
|
254
252
|
return deopt(valuePath, state);
|
255
253
|
}
|
256
254
|
|
257
|
-
|
258
|
-
obj[key] =
|
255
|
+
value = value.value;
|
256
|
+
obj[key] = value;
|
259
257
|
}
|
260
258
|
|
261
259
|
return obj;
|
262
260
|
}
|
263
261
|
|
264
262
|
if (path.isLogicalExpression()) {
|
265
|
-
|
266
|
-
|
267
|
-
|
263
|
+
const wasConfident = state.confident;
|
264
|
+
const left = evaluateCached(path.get("left"), state);
|
265
|
+
const leftConfident = state.confident;
|
268
266
|
state.confident = wasConfident;
|
269
|
-
|
270
|
-
|
267
|
+
const right = evaluateCached(path.get("right"), state);
|
268
|
+
const rightConfident = state.confident;
|
271
269
|
state.confident = leftConfident && rightConfident;
|
272
270
|
|
273
271
|
switch (node.operator) {
|
@@ -291,110 +289,104 @@ function _evaluate(path, state) {
|
|
291
289
|
}
|
292
290
|
|
293
291
|
if (path.isBinaryExpression()) {
|
294
|
-
|
295
|
-
|
292
|
+
const left = evaluateCached(path.get("left"), state);
|
296
293
|
if (!state.confident) return;
|
297
|
-
|
298
|
-
var _right = evaluateCached(path.get("right"), state);
|
299
|
-
|
294
|
+
const right = evaluateCached(path.get("right"), state);
|
300
295
|
if (!state.confident) return;
|
301
296
|
|
302
297
|
switch (node.operator) {
|
303
298
|
case "-":
|
304
|
-
return
|
299
|
+
return left - right;
|
305
300
|
|
306
301
|
case "+":
|
307
|
-
return
|
302
|
+
return left + right;
|
308
303
|
|
309
304
|
case "/":
|
310
|
-
return
|
305
|
+
return left / right;
|
311
306
|
|
312
307
|
case "*":
|
313
|
-
return
|
308
|
+
return left * right;
|
314
309
|
|
315
310
|
case "%":
|
316
|
-
return
|
311
|
+
return left % right;
|
317
312
|
|
318
313
|
case "**":
|
319
|
-
return Math.pow(
|
314
|
+
return Math.pow(left, right);
|
320
315
|
|
321
316
|
case "<":
|
322
|
-
return
|
317
|
+
return left < right;
|
323
318
|
|
324
319
|
case ">":
|
325
|
-
return
|
320
|
+
return left > right;
|
326
321
|
|
327
322
|
case "<=":
|
328
|
-
return
|
323
|
+
return left <= right;
|
329
324
|
|
330
325
|
case ">=":
|
331
|
-
return
|
326
|
+
return left >= right;
|
332
327
|
|
333
328
|
case "==":
|
334
|
-
return
|
329
|
+
return left == right;
|
335
330
|
|
336
331
|
case "!=":
|
337
|
-
return
|
332
|
+
return left != right;
|
338
333
|
|
339
334
|
case "===":
|
340
|
-
return
|
335
|
+
return left === right;
|
341
336
|
|
342
337
|
case "!==":
|
343
|
-
return
|
338
|
+
return left !== right;
|
344
339
|
|
345
340
|
case "|":
|
346
|
-
return
|
341
|
+
return left | right;
|
347
342
|
|
348
343
|
case "&":
|
349
|
-
return
|
344
|
+
return left & right;
|
350
345
|
|
351
346
|
case "^":
|
352
|
-
return
|
347
|
+
return left ^ right;
|
353
348
|
|
354
349
|
case "<<":
|
355
|
-
return
|
350
|
+
return left << right;
|
356
351
|
|
357
352
|
case ">>":
|
358
|
-
return
|
353
|
+
return left >> right;
|
359
354
|
|
360
355
|
case ">>>":
|
361
|
-
return
|
356
|
+
return left >>> right;
|
362
357
|
}
|
363
358
|
}
|
364
359
|
|
365
360
|
if (path.isCallExpression()) {
|
366
|
-
|
367
|
-
|
368
|
-
|
361
|
+
const callee = path.get("callee");
|
362
|
+
let context;
|
363
|
+
let func;
|
369
364
|
|
370
365
|
if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name, true) && VALID_CALLEES.indexOf(callee.node.name) >= 0) {
|
371
366
|
func = global[node.callee.name];
|
372
367
|
}
|
373
368
|
|
374
369
|
if (callee.isMemberExpression()) {
|
375
|
-
|
376
|
-
|
377
|
-
var _property2 = callee.get("property");
|
370
|
+
const object = callee.get("object");
|
371
|
+
const property = callee.get("property");
|
378
372
|
|
379
|
-
if (
|
380
|
-
context = global[
|
381
|
-
func = context[
|
373
|
+
if (object.isIdentifier() && property.isIdentifier() && VALID_CALLEES.indexOf(object.node.name) >= 0 && INVALID_METHODS.indexOf(property.node.name) < 0) {
|
374
|
+
context = global[object.node.name];
|
375
|
+
func = context[property.node.name];
|
382
376
|
}
|
383
377
|
|
384
|
-
if (
|
385
|
-
|
378
|
+
if (object.isLiteral() && property.isIdentifier()) {
|
379
|
+
const type = typeof object.node.value;
|
386
380
|
|
387
|
-
if (
|
388
|
-
context =
|
389
|
-
func = context[
|
381
|
+
if (type === "string" || type === "number") {
|
382
|
+
context = object.node.value;
|
383
|
+
func = context[property.node.name];
|
390
384
|
}
|
391
385
|
}
|
392
386
|
}
|
393
387
|
|
394
388
|
if (func) {
|
395
|
-
|
396
|
-
return evaluateCached(arg, state);
|
397
|
-
});
|
389
|
+
const args = path.get("arguments").map(arg => evaluateCached(arg, state));
|
398
390
|
if (!state.confident) return;
|
399
391
|
return func.apply(context, args);
|
400
392
|
}
|
@@ -403,14 +395,10 @@ function _evaluate(path, state) {
|
|
403
395
|
deopt(path, state);
|
404
396
|
}
|
405
397
|
|
406
|
-
function evaluateQuasis(path, quasis, state, raw) {
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
var str = "";
|
412
|
-
var i = 0;
|
413
|
-
var exprs = path.get("expressions");
|
398
|
+
function evaluateQuasis(path, quasis, state, raw = false) {
|
399
|
+
let str = "";
|
400
|
+
let i = 0;
|
401
|
+
const exprs = path.get("expressions");
|
414
402
|
|
415
403
|
for (var _iterator3 = quasis, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
|
416
404
|
var _ref3;
|
@@ -424,10 +412,10 @@ function evaluateQuasis(path, quasis, state, raw) {
|
|
424
412
|
_ref3 = _i3.value;
|
425
413
|
}
|
426
414
|
|
427
|
-
|
415
|
+
const elem = _ref3;
|
428
416
|
if (!state.confident) break;
|
429
417
|
str += raw ? elem.value.raw : elem.value.cooked;
|
430
|
-
|
418
|
+
const expr = exprs[i++];
|
431
419
|
if (expr) str += String(evaluateCached(expr, state));
|
432
420
|
}
|
433
421
|
|
@@ -436,12 +424,12 @@ function evaluateQuasis(path, quasis, state, raw) {
|
|
436
424
|
}
|
437
425
|
|
438
426
|
function evaluate() {
|
439
|
-
|
427
|
+
const state = {
|
440
428
|
confident: true,
|
441
429
|
deoptPath: null,
|
442
430
|
seen: new Map()
|
443
431
|
};
|
444
|
-
|
432
|
+
let value = evaluateCached(this, state);
|
445
433
|
if (!state.confident) value = undefined;
|
446
434
|
return {
|
447
435
|
confident: state.confident,
|
package/lib/path/family.js
CHANGED
@@ -21,7 +21,7 @@ exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
|
|
21
21
|
var _index = _interopRequireDefault(require("./index"));
|
22
22
|
|
23
23
|
function t() {
|
24
|
-
|
24
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
25
25
|
|
26
26
|
t = function t() {
|
27
27
|
return data;
|
@@ -48,7 +48,7 @@ function addCompletionRecords(path, paths) {
|
|
48
48
|
}
|
49
49
|
|
50
50
|
function getCompletionRecords() {
|
51
|
-
|
51
|
+
let paths = [];
|
52
52
|
|
53
53
|
if (this.isIfStatement()) {
|
54
54
|
paths = addCompletionRecords(this.get("consequent"), paths);
|
@@ -91,9 +91,9 @@ function getNextSibling() {
|
|
91
91
|
}
|
92
92
|
|
93
93
|
function getAllNextSiblings() {
|
94
|
-
|
95
|
-
|
96
|
-
|
94
|
+
let _key = this.key;
|
95
|
+
let sibling = this.getSibling(++_key);
|
96
|
+
const siblings = [];
|
97
97
|
|
98
98
|
while (sibling.node) {
|
99
99
|
siblings.push(sibling);
|
@@ -104,9 +104,9 @@ function getAllNextSiblings() {
|
|
104
104
|
}
|
105
105
|
|
106
106
|
function getAllPrevSiblings() {
|
107
|
-
|
108
|
-
|
109
|
-
|
107
|
+
let _key = this.key;
|
108
|
+
let sibling = this.getSibling(--_key);
|
109
|
+
const siblings = [];
|
110
110
|
|
111
111
|
while (sibling.node) {
|
112
112
|
siblings.push(sibling);
|
@@ -118,7 +118,7 @@ function getAllPrevSiblings() {
|
|
118
118
|
|
119
119
|
function get(key, context) {
|
120
120
|
if (context === true) context = this.context;
|
121
|
-
|
121
|
+
const parts = key.split(".");
|
122
122
|
|
123
123
|
if (parts.length === 1) {
|
124
124
|
return this._getKey(key, context);
|
@@ -128,16 +128,14 @@ function get(key, context) {
|
|
128
128
|
}
|
129
129
|
|
130
130
|
function _getKey(key, context) {
|
131
|
-
|
132
|
-
|
133
|
-
var node = this.node;
|
134
|
-
var container = node[key];
|
131
|
+
const node = this.node;
|
132
|
+
const container = node[key];
|
135
133
|
|
136
134
|
if (Array.isArray(container)) {
|
137
|
-
return container.map(
|
135
|
+
return container.map((_, i) => {
|
138
136
|
return _index.default.get({
|
139
137
|
listKey: key,
|
140
|
-
parentPath:
|
138
|
+
parentPath: this,
|
141
139
|
parent: node,
|
142
140
|
container: container,
|
143
141
|
key: i
|
@@ -154,11 +152,11 @@ function _getKey(key, context) {
|
|
154
152
|
}
|
155
153
|
|
156
154
|
function _getPattern(parts, context) {
|
157
|
-
|
155
|
+
let path = this;
|
158
156
|
var _arr = parts;
|
159
157
|
|
160
158
|
for (var _i = 0; _i < _arr.length; _i++) {
|
161
|
-
|
159
|
+
const part = _arr[_i];
|
162
160
|
|
163
161
|
if (part === ".") {
|
164
162
|
path = path.parentPath;
|
@@ -182,28 +180,20 @@ function getOuterBindingIdentifiers(duplicates) {
|
|
182
180
|
return t().getOuterBindingIdentifiers(this.node, duplicates);
|
183
181
|
}
|
184
182
|
|
185
|
-
function getBindingIdentifierPaths(duplicates, outerOnly) {
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
if (outerOnly === void 0) {
|
191
|
-
outerOnly = false;
|
192
|
-
}
|
193
|
-
|
194
|
-
var path = this;
|
195
|
-
var search = [].concat(path);
|
196
|
-
var ids = Object.create(null);
|
183
|
+
function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
184
|
+
const path = this;
|
185
|
+
let search = [].concat(path);
|
186
|
+
const ids = Object.create(null);
|
197
187
|
|
198
188
|
while (search.length) {
|
199
|
-
|
189
|
+
const id = search.shift();
|
200
190
|
if (!id) continue;
|
201
191
|
if (!id.node) continue;
|
202
|
-
|
192
|
+
const keys = t().getBindingIdentifiers.keys[id.node.type];
|
203
193
|
|
204
194
|
if (id.isIdentifier()) {
|
205
195
|
if (duplicates) {
|
206
|
-
|
196
|
+
const _ids = ids[id.node.name] = ids[id.node.name] || [];
|
207
197
|
|
208
198
|
_ids.push(id);
|
209
199
|
} else {
|
@@ -214,7 +204,7 @@ function getBindingIdentifierPaths(duplicates, outerOnly) {
|
|
214
204
|
}
|
215
205
|
|
216
206
|
if (id.isExportDeclaration()) {
|
217
|
-
|
207
|
+
const declaration = id.get("declaration");
|
218
208
|
|
219
209
|
if (declaration.isDeclaration()) {
|
220
210
|
search.push(declaration);
|
@@ -235,9 +225,9 @@ function getBindingIdentifierPaths(duplicates, outerOnly) {
|
|
235
225
|
}
|
236
226
|
|
237
227
|
if (keys) {
|
238
|
-
for (
|
239
|
-
|
240
|
-
|
228
|
+
for (let i = 0; i < keys.length; i++) {
|
229
|
+
const key = keys[i];
|
230
|
+
const child = id.get(key);
|
241
231
|
|
242
232
|
if (Array.isArray(child) || child.node) {
|
243
233
|
search = search.concat(child);
|