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

@@ -25,9 +25,9 @@ exports.isInStrictMode = isInStrictMode;
25
25
  exports.is = void 0;
26
26
 
27
27
  function _includes() {
28
- var data = _interopRequireDefault(require("lodash/includes"));
28
+ const data = _interopRequireDefault(require("lodash/includes"));
29
29
 
30
- _includes = function _includes() {
30
+ _includes = function () {
31
31
  return data;
32
32
  };
33
33
 
@@ -35,9 +35,9 @@ function _includes() {
35
35
  }
36
36
 
37
37
  function t() {
38
- var data = _interopRequireWildcard(require("@babel/types"));
38
+ const data = _interopRequireWildcard(require("@babel/types"));
39
39
 
40
- t = function t() {
40
+ t = function () {
41
41
  return data;
42
42
  };
43
43
 
@@ -53,7 +53,7 @@ function matchesPattern(pattern, allowPartial) {
53
53
  }
54
54
 
55
55
  function has(key) {
56
- var val = this.node && this.node[key];
56
+ const val = this.node && this.node[key];
57
57
 
58
58
  if (val && Array.isArray(val)) {
59
59
  return !!val.length;
@@ -66,7 +66,7 @@ function isStatic() {
66
66
  return this.scope.isStatic(this.node);
67
67
  }
68
68
 
69
- var is = has;
69
+ const is = has;
70
70
  exports.is = is;
71
71
 
72
72
  function isnt(key) {
@@ -100,11 +100,11 @@ function canSwapBetweenExpressionAndStatement(replacement) {
100
100
  }
101
101
 
102
102
  function isCompletionRecord(allowInsideFunction) {
103
- var path = this;
104
- var first = true;
103
+ let path = this;
104
+ let first = true;
105
105
 
106
106
  do {
107
- var container = path.container;
107
+ const container = path.container;
108
108
 
109
109
  if (path.isFunction() && !first) {
110
110
  return !!allowInsideFunction;
@@ -130,10 +130,10 @@ function isStatementOrBlock() {
130
130
 
131
131
  function referencesImport(moduleSource, importName) {
132
132
  if (!this.isReferencedIdentifier()) return false;
133
- var binding = this.scope.getBinding(this.node.name);
133
+ const binding = this.scope.getBinding(this.node.name);
134
134
  if (!binding || binding.kind !== "module") return false;
135
- var path = binding.path;
136
- var parent = path.parentPath;
135
+ const path = binding.path;
136
+ const parent = path.parentPath;
137
137
  if (!parent.isImportDeclaration()) return false;
138
138
 
139
139
  if (parent.node.source.value === moduleSource) {
@@ -158,7 +158,7 @@ function referencesImport(moduleSource, importName) {
158
158
  }
159
159
 
160
160
  function getSource() {
161
- var node = this.node;
161
+ const node = this.node;
162
162
 
163
163
  if (node.end) {
164
164
  return this.hub.file.code.slice(node.start, node.end);
@@ -172,11 +172,11 @@ function willIMaybeExecuteBefore(target) {
172
172
  }
173
173
 
174
174
  function _guessExecutionStatusRelativeTo(target) {
175
- var targetFuncParent = target.scope.getFunctionParent() || target.scope.getProgramParent();
176
- var selfFuncParent = this.scope.getFunctionParent() || target.scope.getProgramParent();
175
+ const targetFuncParent = target.scope.getFunctionParent() || target.scope.getProgramParent();
176
+ const selfFuncParent = this.scope.getFunctionParent() || target.scope.getProgramParent();
177
177
 
178
178
  if (targetFuncParent.node !== selfFuncParent.node) {
179
- var status = this._guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent);
179
+ const status = this._guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent);
180
180
 
181
181
  if (status) {
182
182
  return status;
@@ -185,15 +185,15 @@ function _guessExecutionStatusRelativeTo(target) {
185
185
  }
186
186
  }
187
187
 
188
- var targetPaths = target.getAncestry();
188
+ const targetPaths = target.getAncestry();
189
189
  if (targetPaths.indexOf(this) >= 0) return "after";
190
- var selfPaths = this.getAncestry();
191
- var commonPath;
192
- var targetIndex;
193
- var selfIndex;
190
+ const selfPaths = this.getAncestry();
191
+ let commonPath;
192
+ let targetIndex;
193
+ let selfIndex;
194
194
 
195
195
  for (selfIndex = 0; selfIndex < selfPaths.length; selfIndex++) {
196
- var selfPath = selfPaths[selfIndex];
196
+ const selfPath = selfPaths[selfIndex];
197
197
  targetIndex = targetPaths.indexOf(selfPath);
198
198
 
199
199
  if (targetIndex >= 0) {
@@ -206,8 +206,8 @@ function _guessExecutionStatusRelativeTo(target) {
206
206
  return "before";
207
207
  }
208
208
 
209
- var targetRelationship = targetPaths[targetIndex - 1];
210
- var selfRelationship = selfPaths[selfIndex - 1];
209
+ const targetRelationship = targetPaths[targetIndex - 1];
210
+ const selfRelationship = selfPaths[selfIndex - 1];
211
211
 
212
212
  if (!targetRelationship || !selfRelationship) {
213
213
  return "before";
@@ -217,59 +217,32 @@ function _guessExecutionStatusRelativeTo(target) {
217
217
  return targetRelationship.key > selfRelationship.key ? "before" : "after";
218
218
  }
219
219
 
220
- var keys = t().VISITOR_KEYS[commonPath.type];
221
- var targetKeyPosition = keys.indexOf(targetRelationship.key);
222
- var selfKeyPosition = keys.indexOf(selfRelationship.key);
220
+ const keys = t().VISITOR_KEYS[commonPath.type];
221
+ const targetKeyPosition = keys.indexOf(targetRelationship.key);
222
+ const selfKeyPosition = keys.indexOf(selfRelationship.key);
223
223
  return targetKeyPosition > selfKeyPosition ? "before" : "after";
224
224
  }
225
225
 
226
226
  function _guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent) {
227
- var targetFuncPath = targetFuncParent.path;
227
+ const targetFuncPath = targetFuncParent.path;
228
228
  if (!targetFuncPath.isFunctionDeclaration()) return;
229
- var binding = targetFuncPath.scope.getBinding(targetFuncPath.node.id.name);
229
+ const binding = targetFuncPath.scope.getBinding(targetFuncPath.node.id.name);
230
230
  if (!binding.references) return "before";
231
- var referencePaths = binding.referencePaths;
232
-
233
- for (var _iterator = referencePaths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
234
- var _ref;
235
-
236
- if (_isArray) {
237
- if (_i >= _iterator.length) break;
238
- _ref = _iterator[_i++];
239
- } else {
240
- _i = _iterator.next();
241
- if (_i.done) break;
242
- _ref = _i.value;
243
- }
244
-
245
- var path = _ref;
231
+ const referencePaths = binding.referencePaths;
246
232
 
233
+ for (const path of referencePaths) {
247
234
  if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
248
235
  return;
249
236
  }
250
237
  }
251
238
 
252
- var allStatus;
253
-
254
- for (var _iterator2 = referencePaths, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
255
- var _ref2;
239
+ let allStatus;
256
240
 
257
- if (_isArray2) {
258
- if (_i2 >= _iterator2.length) break;
259
- _ref2 = _iterator2[_i2++];
260
- } else {
261
- _i2 = _iterator2.next();
262
- if (_i2.done) break;
263
- _ref2 = _i2.value;
264
- }
265
-
266
- var _path = _ref2;
267
- var childOfFunction = !!_path.find(function (path) {
268
- return path.node === targetFuncPath.node;
269
- });
241
+ for (const path of referencePaths) {
242
+ const childOfFunction = !!path.find(path => path.node === targetFuncPath.node);
270
243
  if (childOfFunction) continue;
271
244
 
272
- var status = this._guessExecutionStatusRelativeTo(_path);
245
+ const status = this._guessExecutionStatusRelativeTo(path);
273
246
 
274
247
  if (allStatus) {
275
248
  if (allStatus !== status) return;
@@ -295,35 +268,31 @@ function _resolve(dangerous, resolved) {
295
268
  return this.get("init").resolve(dangerous, resolved);
296
269
  } else {}
297
270
  } else if (this.isReferencedIdentifier()) {
298
- var binding = this.scope.getBinding(this.node.name);
271
+ const binding = this.scope.getBinding(this.node.name);
299
272
  if (!binding) return;
300
273
  if (!binding.constant) return;
301
274
  if (binding.kind === "module") return;
302
275
 
303
276
  if (binding.path !== this) {
304
- var ret = binding.path.resolve(dangerous, resolved);
305
- if (this.find(function (parent) {
306
- return parent.node === ret.node;
307
- })) return;
277
+ const ret = binding.path.resolve(dangerous, resolved);
278
+ if (this.find(parent => parent.node === ret.node)) return;
308
279
  return ret;
309
280
  }
310
281
  } else if (this.isTypeCastExpression()) {
311
282
  return this.get("expression").resolve(dangerous, resolved);
312
283
  } else if (dangerous && this.isMemberExpression()) {
313
- var targetKey = this.toComputedKey();
284
+ const targetKey = this.toComputedKey();
314
285
  if (!t().isLiteral(targetKey)) return;
315
- var targetName = targetKey.value;
316
- var target = this.get("object").resolve(dangerous, resolved);
286
+ const targetName = targetKey.value;
287
+ const target = this.get("object").resolve(dangerous, resolved);
317
288
 
318
289
  if (target.isObjectExpression()) {
319
- var props = target.get("properties");
320
- var _arr = props;
290
+ const props = target.get("properties");
321
291
 
322
- for (var _i3 = 0; _i3 < _arr.length; _i3++) {
323
- var prop = _arr[_i3];
292
+ for (const prop of props) {
324
293
  if (!prop.isProperty()) continue;
325
- var key = prop.get("key");
326
- var match = prop.isnt("computed") && key.isIdentifier({
294
+ const key = prop.get("key");
295
+ let match = prop.isnt("computed") && key.isIdentifier({
327
296
  name: targetName
328
297
  });
329
298
  match = match || key.isLiteral({
@@ -332,8 +301,8 @@ function _resolve(dangerous, resolved) {
332
301
  if (match) return prop.get("value").resolve(dangerous, resolved);
333
302
  }
334
303
  } else if (target.isArrayExpression() && !isNaN(+targetName)) {
335
- var elems = target.get("elements");
336
- var elem = elems[targetName];
304
+ const elems = target.get("elements");
305
+ const elem = elems[targetName];
337
306
  if (elem) return elem.resolve(dangerous, resolved);
338
307
  }
339
308
  }
@@ -341,7 +310,7 @@ function _resolve(dangerous, resolved) {
341
310
 
342
311
  function isConstantExpression() {
343
312
  if (this.isIdentifier()) {
344
- var binding = this.scope.getBinding(this.node.name);
313
+ const binding = this.scope.getBinding(this.node.name);
345
314
 
346
315
  if (!binding) {
347
316
  return false;
@@ -356,9 +325,7 @@ function isConstantExpression() {
356
325
  }
357
326
 
358
327
  if (this.isTemplateLiteral()) {
359
- return this.get("expressions").every(function (expression) {
360
- return expression.isConstantExpression();
361
- });
328
+ return this.get("expressions").every(expression => expression.isConstantExpression());
362
329
  }
363
330
 
364
331
  return true;
@@ -380,8 +347,8 @@ function isConstantExpression() {
380
347
  }
381
348
 
382
349
  function isInStrictMode() {
383
- var start = this.isProgram() ? this : this.parentPath;
384
- var strictParent = start.find(function (path) {
350
+ const start = this.isProgram() ? this : this.parentPath;
351
+ const strictParent = start.find(path => {
385
352
  if (path.isProgram({
386
353
  sourceType: "module"
387
354
  })) return true;
@@ -392,23 +359,12 @@ function isInStrictMode() {
392
359
  return false;
393
360
  }
394
361
 
395
- var node = path.node;
362
+ let {
363
+ node
364
+ } = path;
396
365
  if (path.isFunction()) node = node.body;
397
366
 
398
- for (var _iterator3 = node.directives, _isArray3 = Array.isArray(_iterator3), _i4 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
399
- var _ref3;
400
-
401
- if (_isArray3) {
402
- if (_i4 >= _iterator3.length) break;
403
- _ref3 = _iterator3[_i4++];
404
- } else {
405
- _i4 = _iterator3.next();
406
- if (_i4.done) break;
407
- _ref3 = _i4.value;
408
- }
409
-
410
- var directive = _ref3;
411
-
367
+ for (const directive of node.directives) {
412
368
  if (directive.value.value === "use strict") {
413
369
  return true;
414
370
  }
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
 
8
8
  function t() {
9
- var data = _interopRequireWildcard(require("@babel/types"));
9
+ const data = _interopRequireWildcard(require("@babel/types"));
10
10
 
11
- t = function t() {
11
+ t = function () {
12
12
  return data;
13
13
  };
14
14
 
@@ -17,14 +17,14 @@ function t() {
17
17
 
18
18
  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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
19
19
 
20
- var referenceVisitor = {
21
- ReferencedIdentifier: function ReferencedIdentifier(path, state) {
20
+ const referenceVisitor = {
21
+ ReferencedIdentifier(path, state) {
22
22
  if (path.isJSXIdentifier() && t().react.isCompatTag(path.node.name) && !path.parentPath.isJSXMemberExpression()) {
23
23
  return;
24
24
  }
25
25
 
26
26
  if (path.node.name === "this") {
27
- var scope = path.scope;
27
+ let scope = path.scope;
28
28
 
29
29
  do {
30
30
  if (scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
@@ -35,15 +35,16 @@ var referenceVisitor = {
35
35
  if (scope) state.breakOnScopePaths.push(scope.path);
36
36
  }
37
37
 
38
- var binding = path.scope.getBinding(path.node.name);
38
+ const binding = path.scope.getBinding(path.node.name);
39
39
  if (!binding) return;
40
40
  if (binding !== state.scope.getBinding(path.node.name)) return;
41
41
  state.bindings[path.node.name] = binding;
42
42
  }
43
+
43
44
  };
44
45
 
45
- var PathHoister = function () {
46
- function PathHoister(path, scope) {
46
+ class PathHoister {
47
+ constructor(path, scope) {
47
48
  this.breakOnScopePaths = [];
48
49
  this.bindings = {};
49
50
  this.scopes = [];
@@ -52,11 +53,9 @@ var PathHoister = function () {
52
53
  this.attachAfter = false;
53
54
  }
54
55
 
55
- var _proto = PathHoister.prototype;
56
-
57
- _proto.isCompatibleScope = function isCompatibleScope(scope) {
58
- for (var key in this.bindings) {
59
- var binding = this.bindings[key];
56
+ isCompatibleScope(scope) {
57
+ for (const key in this.bindings) {
58
+ const binding = this.bindings[key];
60
59
 
61
60
  if (!scope.bindingIdentifierEquals(key, binding.identifier)) {
62
61
  return false;
@@ -64,10 +63,10 @@ var PathHoister = function () {
64
63
  }
65
64
 
66
65
  return true;
67
- };
66
+ }
68
67
 
69
- _proto.getCompatibleScopes = function getCompatibleScopes() {
70
- var scope = this.path.scope;
68
+ getCompatibleScopes() {
69
+ let scope = this.path.scope;
71
70
 
72
71
  do {
73
72
  if (this.isCompatibleScope(scope)) {
@@ -80,37 +79,34 @@ var PathHoister = function () {
80
79
  break;
81
80
  }
82
81
  } while (scope = scope.parent);
83
- };
82
+ }
84
83
 
85
- _proto.getAttachmentPath = function getAttachmentPath() {
86
- var path = this._getAttachmentPath();
84
+ getAttachmentPath() {
85
+ let path = this._getAttachmentPath();
87
86
 
88
87
  if (!path) return;
89
- var targetScope = path.scope;
88
+ let targetScope = path.scope;
90
89
 
91
90
  if (targetScope.path === path) {
92
91
  targetScope = path.scope.parent;
93
92
  }
94
93
 
95
94
  if (targetScope.path.isProgram() || targetScope.path.isFunction()) {
96
- for (var name in this.bindings) {
95
+ for (const name in this.bindings) {
97
96
  if (!targetScope.hasOwnBinding(name)) continue;
98
- var binding = this.bindings[name];
97
+ const binding = this.bindings[name];
99
98
 
100
99
  if (binding.kind === "param" || binding.path.parentKey === "params") {
101
100
  continue;
102
101
  }
103
102
 
104
- var bindingParentPath = this.getAttachmentParentForPath(binding.path);
103
+ const bindingParentPath = this.getAttachmentParentForPath(binding.path);
105
104
 
106
105
  if (bindingParentPath.key >= path.key) {
107
106
  this.attachAfter = true;
108
107
  path = binding.path;
109
- var _arr = binding.constantViolations;
110
-
111
- for (var _i = 0; _i < _arr.length; _i++) {
112
- var violationPath = _arr[_i];
113
108
 
109
+ for (const violationPath of binding.constantViolations) {
114
110
  if (this.getAttachmentParentForPath(violationPath).key > path.key) {
115
111
  path = violationPath;
116
112
  }
@@ -120,19 +116,19 @@ var PathHoister = function () {
120
116
  }
121
117
 
122
118
  return path;
123
- };
119
+ }
124
120
 
125
- _proto._getAttachmentPath = function _getAttachmentPath() {
126
- var scopes = this.scopes;
127
- var scope = scopes.pop();
121
+ _getAttachmentPath() {
122
+ const scopes = this.scopes;
123
+ const scope = scopes.pop();
128
124
  if (!scope) return;
129
125
 
130
126
  if (scope.path.isFunction()) {
131
127
  if (this.hasOwnParamBindings(scope)) {
132
128
  if (this.scope === scope) return;
133
- var bodies = scope.path.get("body").get("body");
129
+ const bodies = scope.path.get("body").get("body");
134
130
 
135
- for (var i = 0; i < bodies.length; i++) {
131
+ for (let i = 0; i < bodies.length; i++) {
136
132
  if (bodies[i].node._blockHoist) continue;
137
133
  return bodies[i];
138
134
  }
@@ -142,45 +138,42 @@ var PathHoister = function () {
142
138
  } else if (scope.path.isProgram()) {
143
139
  return this.getNextScopeAttachmentParent();
144
140
  }
145
- };
141
+ }
146
142
 
147
- _proto.getNextScopeAttachmentParent = function getNextScopeAttachmentParent() {
148
- var scope = this.scopes.pop();
143
+ getNextScopeAttachmentParent() {
144
+ const scope = this.scopes.pop();
149
145
  if (scope) return this.getAttachmentParentForPath(scope.path);
150
- };
146
+ }
151
147
 
152
- _proto.getAttachmentParentForPath = function getAttachmentParentForPath(path) {
148
+ getAttachmentParentForPath(path) {
153
149
  do {
154
150
  if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
155
151
  return path;
156
152
  }
157
153
  } while (path = path.parentPath);
158
- };
154
+ }
159
155
 
160
- _proto.hasOwnParamBindings = function hasOwnParamBindings(scope) {
161
- for (var name in this.bindings) {
156
+ hasOwnParamBindings(scope) {
157
+ for (const name in this.bindings) {
162
158
  if (!scope.hasOwnBinding(name)) continue;
163
- var binding = this.bindings[name];
159
+ const binding = this.bindings[name];
164
160
  if (binding.kind === "param" && binding.constant) return true;
165
161
  }
166
162
 
167
163
  return false;
168
- };
164
+ }
169
165
 
170
- _proto.run = function run() {
166
+ run() {
171
167
  this.path.traverse(referenceVisitor, this);
172
168
  this.getCompatibleScopes();
173
- var attachTo = this.getAttachmentPath();
169
+ const attachTo = this.getAttachmentPath();
174
170
  if (!attachTo) return;
175
171
  if (attachTo.getFunctionParent() === this.path.getFunctionParent()) return;
176
- var uid = attachTo.scope.generateUidIdentifier("ref");
177
- var declarator = t().variableDeclarator(uid, this.path.node);
178
- var insertFn = this.attachAfter ? "insertAfter" : "insertBefore";
179
-
180
- var _attachTo$insertFn = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : t().variableDeclaration("var", [declarator])]),
181
- attached = _attachTo$insertFn[0];
182
-
183
- var parent = this.path.parentPath;
172
+ let uid = attachTo.scope.generateUidIdentifier("ref");
173
+ const declarator = t().variableDeclarator(uid, this.path.node);
174
+ const insertFn = this.attachAfter ? "insertAfter" : "insertBefore";
175
+ const [attached] = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : t().variableDeclaration("var", [declarator])]);
176
+ const parent = this.path.parentPath;
184
177
 
185
178
  if (parent.isJSXElement() && this.path.container === parent.node.children) {
186
179
  uid = t().JSXExpressionContainer(uid);
@@ -188,9 +181,8 @@ var PathHoister = function () {
188
181
 
189
182
  this.path.replaceWith(t().cloneNode(uid));
190
183
  return attachTo.isVariableDeclarator() ? attached.get("init") : attached.get("declarations.0.init");
191
- };
184
+ }
192
185
 
193
- return PathHoister;
194
- }();
186
+ }
195
187
 
196
188
  exports.default = PathHoister;
@@ -4,8 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.hooks = void 0;
7
- var hooks = [function (self, parent) {
8
- var removeParent = self.key === "test" && (parent.isWhile() || parent.isSwitchCase()) || self.key === "declaration" && parent.isExportDeclaration() || self.key === "body" && parent.isLabeledStatement() || self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1 || self.key === "expression" && parent.isExpressionStatement();
7
+ const hooks = [function (self, parent) {
8
+ const removeParent = self.key === "test" && (parent.isWhile() || parent.isSwitchCase()) || self.key === "declaration" && parent.isExportDeclaration() || self.key === "body" && parent.isLabeledStatement() || self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1 || self.key === "expression" && parent.isExpressionStatement();
9
9
 
10
10
  if (removeParent) {
11
11
  parent.remove();