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