@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.

@@ -5,11 +5,11 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.evaluateTruthy = evaluateTruthy;
7
7
  exports.evaluate = evaluate;
8
- var VALID_CALLEES = ["String", "Number", "Math"];
9
- var INVALID_METHODS = ["random"];
8
+ const VALID_CALLEES = ["String", "Number", "Math"];
9
+ const INVALID_METHODS = ["random"];
10
10
 
11
11
  function evaluateTruthy() {
12
- var res = this.evaluate();
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
- var node = path.node;
24
- var seen = state.seen;
23
+ const {
24
+ node
25
+ } = path;
26
+ const {
27
+ seen
28
+ } = state;
25
29
 
26
30
  if (seen.has(node)) {
27
- var existing = seen.get(node);
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
- var item = {
40
+ const item = {
37
41
  resolved: false
38
42
  };
39
43
  seen.set(node, item);
40
44
 
41
- var val = _evaluate(path, state);
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
- var node = path.node;
58
+ const {
59
+ node
60
+ } = path;
55
61
 
56
62
  if (path.isSequenceExpression()) {
57
- var exprs = path.get("expressions");
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
- var object = path.get("tag.object");
75
- var name = object.node.name;
76
- var property = path.get("tag.property");
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
- var testResult = evaluateCached(path.get("test"), state);
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
- var _property = path.get("property");
102
-
103
- var _object = path.get("object");
111
+ const property = path.get("property");
112
+ const object = path.get("object");
104
113
 
105
- if (_object.isLiteral() && _property.isIdentifier()) {
106
- var value = _object.node.value;
107
- var type = typeof value;
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[_property.node.name];
119
+ return value[property.node.name];
111
120
  }
112
121
  }
113
122
  }
114
123
 
115
124
  if (path.isReferencedIdentifier()) {
116
- var binding = path.scope.getBinding(node.name);
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
- var resolved = path.resolve();
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
- var argument = path.get("argument");
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
- var arg = evaluateCached(argument, state);
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
- 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
- }
191
+ const arr = [];
192
+ const elems = path.get("elements");
196
193
 
197
- var elem = _ref;
198
- var elemValue = elem.evaluate();
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
- 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;
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
- var keyPath = prop.get("key");
233
- var key = keyPath;
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
- var valuePath = prop.get("value");
233
+ const valuePath = prop.get("value");
234
+ let value = valuePath.evaluate();
250
235
 
251
- var _value = valuePath.evaluate();
252
-
253
- if (!_value.confident) {
236
+ if (!value.confident) {
254
237
  return deopt(valuePath, state);
255
238
  }
256
239
 
257
- _value = _value.value;
258
- obj[key] = _value;
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
- var wasConfident = state.confident;
266
- var left = evaluateCached(path.get("left"), state);
267
- var leftConfident = state.confident;
248
+ const wasConfident = state.confident;
249
+ const left = evaluateCached(path.get("left"), state);
250
+ const leftConfident = state.confident;
268
251
  state.confident = wasConfident;
269
- var right = evaluateCached(path.get("right"), state);
270
- var rightConfident = state.confident;
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
- var _left = evaluateCached(path.get("left"), state);
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 _left - _right;
284
+ return left - right;
305
285
 
306
286
  case "+":
307
- return _left + _right;
287
+ return left + right;
308
288
 
309
289
  case "/":
310
- return _left / _right;
290
+ return left / right;
311
291
 
312
292
  case "*":
313
- return _left * _right;
293
+ return left * right;
314
294
 
315
295
  case "%":
316
- return _left % _right;
296
+ return left % right;
317
297
 
318
298
  case "**":
319
- return Math.pow(_left, _right);
299
+ return Math.pow(left, right);
320
300
 
321
301
  case "<":
322
- return _left < _right;
302
+ return left < right;
323
303
 
324
304
  case ">":
325
- return _left > _right;
305
+ return left > right;
326
306
 
327
307
  case "<=":
328
- return _left <= _right;
308
+ return left <= right;
329
309
 
330
310
  case ">=":
331
- return _left >= _right;
311
+ return left >= right;
332
312
 
333
313
  case "==":
334
- return _left == _right;
314
+ return left == right;
335
315
 
336
316
  case "!=":
337
- return _left != _right;
317
+ return left != right;
338
318
 
339
319
  case "===":
340
- return _left === _right;
320
+ return left === right;
341
321
 
342
322
  case "!==":
343
- return _left !== _right;
323
+ return left !== right;
344
324
 
345
325
  case "|":
346
- return _left | _right;
326
+ return left | right;
347
327
 
348
328
  case "&":
349
- return _left & _right;
329
+ return left & right;
350
330
 
351
331
  case "^":
352
- return _left ^ _right;
332
+ return left ^ right;
353
333
 
354
334
  case "<<":
355
- return _left << _right;
335
+ return left << right;
356
336
 
357
337
  case ">>":
358
- return _left >> _right;
338
+ return left >> right;
359
339
 
360
340
  case ">>>":
361
- return _left >>> _right;
341
+ return left >>> right;
362
342
  }
363
343
  }
364
344
 
365
345
  if (path.isCallExpression()) {
366
- var callee = path.get("callee");
367
- var context;
368
- var func;
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
- var _object2 = callee.get("object");
376
-
377
- var _property2 = callee.get("property");
355
+ const object = callee.get("object");
356
+ const property = callee.get("property");
378
357
 
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];
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 (_object2.isLiteral() && _property2.isIdentifier()) {
385
- var _type = typeof _object2.node.value;
363
+ if (object.isLiteral() && property.isIdentifier()) {
364
+ const type = typeof object.node.value;
386
365
 
387
- if (_type === "string" || _type === "number") {
388
- context = _object2.node.value;
389
- func = context[_property2.node.name];
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
- var args = path.get("arguments").map(function (arg) {
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
- 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
- }
383
+ function evaluateQuasis(path, quasis, state, raw = false) {
384
+ let str = "";
385
+ let i = 0;
386
+ const exprs = path.get("expressions");
426
387
 
427
- var elem = _ref3;
388
+ for (const elem of quasis) {
428
389
  if (!state.confident) break;
429
390
  str += raw ? elem.value.raw : elem.value.cooked;
430
- var expr = exprs[i++];
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
- var state = {
400
+ const state = {
440
401
  confident: true,
441
402
  deoptPath: null,
442
403
  seen: new Map()
443
404
  };
444
- var value = evaluateCached(this, state);
405
+ let value = evaluateCached(this, state);
445
406
  if (!state.confident) value = undefined;
446
407
  return {
447
408
  confident: state.confident,
@@ -21,9 +21,9 @@ exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
21
21
  var _index = _interopRequireDefault(require("./index"));
22
22
 
23
23
  function t() {
24
- var data = _interopRequireWildcard(require("@babel/types"));
24
+ const data = _interopRequireWildcard(require("@babel/types"));
25
25
 
26
- t = function t() {
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
- var paths = [];
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
- var _key = this.key;
95
- var sibling = this.getSibling(++_key);
96
- var siblings = [];
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
- var _key = this.key;
108
- var sibling = this.getSibling(--_key);
109
- var siblings = [];
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
- var parts = key.split(".");
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
- var _this = this;
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(function (_, i) {
135
+ return container.map((_, i) => {
138
136
  return _index.default.get({
139
137
  listKey: key,
140
- parentPath: _this,
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
- var path = this;
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
- 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);
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
- var id = search.shift();
186
+ const id = search.shift();
200
187
  if (!id) continue;
201
188
  if (!id.node) continue;
202
- var keys = t().getBindingIdentifiers.keys[id.node.type];
189
+ const keys = t().getBindingIdentifiers.keys[id.node.type];
203
190
 
204
191
  if (id.isIdentifier()) {
205
192
  if (duplicates) {
206
- var _ids = ids[id.node.name] = ids[id.node.name] || [];
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
- var declaration = id.get("declaration");
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 (var i = 0; i < keys.length; i++) {
239
- var key = keys[i];
240
- var child = id.get(key);
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);