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