@babel/traverse 7.15.0 → 7.16.5

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.

@@ -3,16 +3,46 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.toComputedKey = toComputedKey;
7
- exports.ensureBlock = ensureBlock;
6
+ exports.arrowFunctionToExpression = arrowFunctionToExpression;
8
7
  exports.arrowFunctionToShadowed = arrowFunctionToShadowed;
8
+ exports.ensureBlock = ensureBlock;
9
+ exports.toComputedKey = toComputedKey;
9
10
  exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
10
- exports.arrowFunctionToExpression = arrowFunctionToExpression;
11
11
 
12
- var t = require("@babel/types");
12
+ var _t = require("@babel/types");
13
+
14
+ var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
13
15
 
14
16
  var _helperFunctionName = require("@babel/helper-function-name");
15
17
 
18
+ var _visitors = require("../visitors");
19
+
20
+ const {
21
+ arrowFunctionExpression,
22
+ assignmentExpression,
23
+ binaryExpression,
24
+ blockStatement,
25
+ callExpression,
26
+ conditionalExpression,
27
+ expressionStatement,
28
+ identifier,
29
+ isIdentifier,
30
+ jsxIdentifier,
31
+ memberExpression,
32
+ metaProperty,
33
+ numericLiteral,
34
+ objectExpression,
35
+ restElement,
36
+ returnStatement,
37
+ sequenceExpression,
38
+ spreadElement,
39
+ stringLiteral,
40
+ super: _super,
41
+ thisExpression,
42
+ toExpression,
43
+ unaryExpression
44
+ } = _t;
45
+
16
46
  function toComputedKey() {
17
47
  let key;
18
48
 
@@ -25,7 +55,7 @@ function toComputedKey() {
25
55
  }
26
56
 
27
57
  if (!this.node.computed) {
28
- if (t.isIdentifier(key)) key = t.stringLiteral(key.name);
58
+ if (isIdentifier(key)) key = stringLiteral(key.name);
29
59
  }
30
60
 
31
61
  return key;
@@ -61,14 +91,14 @@ function ensureBlock() {
61
91
 
62
92
  if (this.isFunction()) {
63
93
  key = "argument";
64
- statements.push(t.returnStatement(body.node));
94
+ statements.push(returnStatement(body.node));
65
95
  } else {
66
96
  key = "expression";
67
- statements.push(t.expressionStatement(body.node));
97
+ statements.push(expressionStatement(body.node));
68
98
  }
69
99
  }
70
100
 
71
- this.node.body = t.blockStatement(statements);
101
+ this.node.body = blockStatement(statements);
72
102
  const parentPath = this.get(stringPath);
73
103
  body.setup(parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key);
74
104
  return this.node;
@@ -96,35 +126,68 @@ function arrowFunctionToExpression({
96
126
  throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression.");
97
127
  }
98
128
 
99
- const thisBinding = hoistFunctionEnvironment(this, noNewArrows, allowInsertArrow);
100
- this.ensureBlock();
101
- this.node.type = "FunctionExpression";
129
+ const {
130
+ thisBinding,
131
+ fnPath: fn
132
+ } = hoistFunctionEnvironment(this, noNewArrows, allowInsertArrow);
133
+ fn.ensureBlock();
134
+ fn.node.type = "FunctionExpression";
102
135
 
103
136
  if (!noNewArrows) {
104
- const checkBinding = thisBinding ? null : this.parentPath.scope.generateUidIdentifier("arrowCheckId");
137
+ const checkBinding = thisBinding ? null : fn.scope.generateUidIdentifier("arrowCheckId");
105
138
 
106
139
  if (checkBinding) {
107
- this.parentPath.scope.push({
140
+ fn.parentPath.scope.push({
108
141
  id: checkBinding,
109
- init: t.objectExpression([])
142
+ init: objectExpression([])
110
143
  });
111
144
  }
112
145
 
113
- this.get("body").unshiftContainer("body", t.expressionStatement(t.callExpression(this.hub.addHelper("newArrowCheck"), [t.thisExpression(), checkBinding ? t.identifier(checkBinding.name) : t.identifier(thisBinding)])));
114
- this.replaceWith(t.callExpression(t.memberExpression((0, _helperFunctionName.default)(this, true) || this.node, t.identifier("bind")), [checkBinding ? t.identifier(checkBinding.name) : t.thisExpression()]));
146
+ fn.get("body").unshiftContainer("body", expressionStatement(callExpression(this.hub.addHelper("newArrowCheck"), [thisExpression(), checkBinding ? identifier(checkBinding.name) : identifier(thisBinding)])));
147
+ fn.replaceWith(callExpression(memberExpression((0, _helperFunctionName.default)(this, true) || fn.node, identifier("bind")), [checkBinding ? identifier(checkBinding.name) : thisExpression()]));
115
148
  }
116
149
  }
117
150
 
151
+ const getSuperCallsVisitor = (0, _visitors.merge)([{
152
+ CallExpression(child, {
153
+ allSuperCalls
154
+ }) {
155
+ if (!child.get("callee").isSuper()) return;
156
+ allSuperCalls.push(child);
157
+ }
158
+
159
+ }, _helperEnvironmentVisitor.default]);
160
+
118
161
  function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow = true) {
119
- const thisEnvFn = fnPath.findParent(p => {
120
- return p.isFunction() && !p.isArrowFunctionExpression() || p.isProgram() || p.isClassProperty({
162
+ let arrowParent;
163
+ let thisEnvFn = fnPath.findParent(p => {
164
+ if (p.isArrowFunctionExpression()) {
165
+ var _arrowParent;
166
+
167
+ (_arrowParent = arrowParent) != null ? _arrowParent : arrowParent = p;
168
+ return false;
169
+ }
170
+
171
+ return p.isFunction() || p.isProgram() || p.isClassProperty({
172
+ static: false
173
+ }) || p.isClassPrivateProperty({
121
174
  static: false
122
175
  });
123
176
  });
124
- const inConstructor = (thisEnvFn == null ? void 0 : thisEnvFn.node.kind) === "constructor";
177
+ const inConstructor = thisEnvFn.isClassMethod({
178
+ kind: "constructor"
179
+ });
125
180
 
126
- if (thisEnvFn.isClassProperty()) {
127
- throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
181
+ if (thisEnvFn.isClassProperty() || thisEnvFn.isClassPrivateProperty()) {
182
+ if (arrowParent) {
183
+ thisEnvFn = arrowParent;
184
+ } else if (allowInsertArrow) {
185
+ fnPath.replaceWith(callExpression(arrowFunctionExpression([], toExpression(fnPath.node)), []));
186
+ thisEnvFn = fnPath.get("callee");
187
+ fnPath = thisEnvFn.get("body");
188
+ } else {
189
+ throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
190
+ }
128
191
  }
129
192
 
130
193
  const {
@@ -141,25 +204,12 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
141
204
  }
142
205
 
143
206
  const allSuperCalls = [];
144
- thisEnvFn.traverse({
145
- Function(child) {
146
- if (child.isArrowFunctionExpression()) return;
147
- child.skip();
148
- },
149
-
150
- ClassProperty(child) {
151
- child.skip();
152
- },
153
-
154
- CallExpression(child) {
155
- if (!child.get("callee").isSuper()) return;
156
- allSuperCalls.push(child);
157
- }
158
-
207
+ thisEnvFn.traverse(getSuperCallsVisitor, {
208
+ allSuperCalls
159
209
  });
160
210
  const superBinding = getSuperBinding(thisEnvFn);
161
211
  allSuperCalls.forEach(superCall => {
162
- const callee = t.identifier(superBinding);
212
+ const callee = identifier(superBinding);
163
213
  callee.loc = superCall.node.callee.loc;
164
214
  superCall.get("callee").replaceWith(callee);
165
215
  });
@@ -167,25 +217,25 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
167
217
 
168
218
  if (argumentsPaths.length > 0) {
169
219
  const argumentsBinding = getBinding(thisEnvFn, "arguments", () => {
170
- const args = () => t.identifier("arguments");
220
+ const args = () => identifier("arguments");
171
221
 
172
222
  if (thisEnvFn.scope.path.isProgram()) {
173
- return t.conditionalExpression(t.binaryExpression("===", t.unaryExpression("typeof", args()), t.stringLiteral("undefined")), thisEnvFn.scope.buildUndefinedNode(), args());
223
+ return conditionalExpression(binaryExpression("===", unaryExpression("typeof", args()), stringLiteral("undefined")), thisEnvFn.scope.buildUndefinedNode(), args());
174
224
  } else {
175
225
  return args();
176
226
  }
177
227
  });
178
228
  argumentsPaths.forEach(argumentsChild => {
179
- const argsRef = t.identifier(argumentsBinding);
229
+ const argsRef = identifier(argumentsBinding);
180
230
  argsRef.loc = argumentsChild.node.loc;
181
231
  argumentsChild.replaceWith(argsRef);
182
232
  });
183
233
  }
184
234
 
185
235
  if (newTargetPaths.length > 0) {
186
- const newTargetBinding = getBinding(thisEnvFn, "newtarget", () => t.metaProperty(t.identifier("new"), t.identifier("target")));
236
+ const newTargetBinding = getBinding(thisEnvFn, "newtarget", () => metaProperty(identifier("new"), identifier("target")));
187
237
  newTargetPaths.forEach(targetChild => {
188
- const targetRef = t.identifier(newTargetBinding);
238
+ const targetRef = identifier(newTargetBinding);
189
239
  targetRef.loc = targetChild.node.loc;
190
240
  targetChild.replaceWith(targetRef);
191
241
  });
@@ -217,11 +267,11 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
217
267
  args.push(value);
218
268
  }
219
269
 
220
- const call = t.callExpression(t.identifier(superBinding), args);
270
+ const call = callExpression(identifier(superBinding), args);
221
271
 
222
272
  if (isCall) {
223
- superProp.parentPath.unshiftContainer("arguments", t.thisExpression());
224
- superProp.replaceWith(t.memberExpression(call, t.identifier("call")));
273
+ superProp.parentPath.unshiftContainer("arguments", thisExpression());
274
+ superProp.replaceWith(memberExpression(call, identifier("call")));
225
275
  thisPaths.push(superProp.parentPath.get("arguments.0"));
226
276
  } else if (isAssignment) {
227
277
  superProp.parentPath.replaceWith(call);
@@ -238,7 +288,7 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
238
288
 
239
289
  if (noNewArrows || inConstructor && hasSuperClass(thisEnvFn)) {
240
290
  thisPaths.forEach(thisChild => {
241
- const thisRef = thisChild.isJSX() ? t.jsxIdentifier(thisBinding) : t.identifier(thisBinding);
291
+ const thisRef = thisChild.isJSX() ? jsxIdentifier(thisBinding) : identifier(thisBinding);
242
292
  thisRef.loc = thisChild.node.loc;
243
293
  thisChild.replaceWith(thisRef);
244
294
  });
@@ -246,7 +296,10 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
246
296
  }
247
297
  }
248
298
 
249
- return thisBinding;
299
+ return {
300
+ thisBinding,
301
+ fnPath
302
+ };
250
303
  }
251
304
 
252
305
  function standardizeSuperProperty(superProp) {
@@ -258,11 +311,11 @@ function standardizeSuperProperty(superProp) {
258
311
 
259
312
  if (superProp.node.computed) {
260
313
  const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
261
- assignmentPath.get("left").replaceWith(t.memberExpression(superProp.node.object, t.assignmentExpression("=", tmp, superProp.node.property), true));
262
- assignmentPath.get("right").replaceWith(t.binaryExpression(op, t.memberExpression(superProp.node.object, t.identifier(tmp.name), true), value));
314
+ assignmentPath.get("left").replaceWith(memberExpression(superProp.node.object, assignmentExpression("=", tmp, superProp.node.property), true));
315
+ assignmentPath.get("right").replaceWith(binaryExpression(op, memberExpression(superProp.node.object, identifier(tmp.name), true), value));
263
316
  } else {
264
- assignmentPath.get("left").replaceWith(t.memberExpression(superProp.node.object, superProp.node.property));
265
- assignmentPath.get("right").replaceWith(t.binaryExpression(op, t.memberExpression(superProp.node.object, t.identifier(superProp.node.property.name)), value));
317
+ assignmentPath.get("left").replaceWith(memberExpression(superProp.node.object, superProp.node.property));
318
+ assignmentPath.get("right").replaceWith(binaryExpression(op, memberExpression(superProp.node.object, identifier(superProp.node.property.name)), value));
266
319
  }
267
320
 
268
321
  return [assignmentPath.get("left"), assignmentPath.get("right").get("left")];
@@ -270,13 +323,13 @@ function standardizeSuperProperty(superProp) {
270
323
  const updateExpr = superProp.parentPath;
271
324
  const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
272
325
  const computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null;
273
- const parts = [t.assignmentExpression("=", tmp, t.memberExpression(superProp.node.object, computedKey ? t.assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), t.assignmentExpression("=", t.memberExpression(superProp.node.object, computedKey ? t.identifier(computedKey.name) : superProp.node.property, superProp.node.computed), t.binaryExpression("+", t.identifier(tmp.name), t.numericLiteral(1)))];
326
+ const parts = [assignmentExpression("=", tmp, memberExpression(superProp.node.object, computedKey ? assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), assignmentExpression("=", memberExpression(superProp.node.object, computedKey ? identifier(computedKey.name) : superProp.node.property, superProp.node.computed), binaryExpression("+", identifier(tmp.name), numericLiteral(1)))];
274
327
 
275
328
  if (!superProp.parentPath.node.prefix) {
276
- parts.push(t.identifier(tmp.name));
329
+ parts.push(identifier(tmp.name));
277
330
  }
278
331
 
279
- updateExpr.replaceWith(t.sequenceExpression(parts));
332
+ updateExpr.replaceWith(sequenceExpression(parts));
280
333
  const left = updateExpr.get("expressions.0.right");
281
334
  const right = updateExpr.get("expressions.1.left");
282
335
  return [left, right];
@@ -289,27 +342,25 @@ function hasSuperClass(thisEnvFn) {
289
342
  return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
290
343
  }
291
344
 
345
+ const assignSuperThisVisitor = (0, _visitors.merge)([{
346
+ CallExpression(child, {
347
+ supers,
348
+ thisBinding
349
+ }) {
350
+ if (!child.get("callee").isSuper()) return;
351
+ if (supers.has(child.node)) return;
352
+ supers.add(child.node);
353
+ child.replaceWithMultiple([child.node, assignmentExpression("=", identifier(thisBinding), identifier("this"))]);
354
+ }
355
+
356
+ }, _helperEnvironmentVisitor.default]);
357
+
292
358
  function getThisBinding(thisEnvFn, inConstructor) {
293
359
  return getBinding(thisEnvFn, "this", thisBinding => {
294
- if (!inConstructor || !hasSuperClass(thisEnvFn)) return t.thisExpression();
295
- const supers = new WeakSet();
296
- thisEnvFn.traverse({
297
- Function(child) {
298
- if (child.isArrowFunctionExpression()) return;
299
- child.skip();
300
- },
301
-
302
- ClassProperty(child) {
303
- child.skip();
304
- },
305
-
306
- CallExpression(child) {
307
- if (!child.get("callee").isSuper()) return;
308
- if (supers.has(child.node)) return;
309
- supers.add(child.node);
310
- child.replaceWithMultiple([child.node, t.assignmentExpression("=", t.identifier(thisBinding), t.identifier("this"))]);
311
- }
312
-
360
+ if (!inConstructor || !hasSuperClass(thisEnvFn)) return thisExpression();
361
+ thisEnvFn.traverse(assignSuperThisVisitor, {
362
+ supers: new WeakSet(),
363
+ thisBinding
313
364
  });
314
365
  });
315
366
  }
@@ -317,7 +368,7 @@ function getThisBinding(thisEnvFn, inConstructor) {
317
368
  function getSuperBinding(thisEnvFn) {
318
369
  return getBinding(thisEnvFn, "supercall", () => {
319
370
  const argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
320
- return t.arrowFunctionExpression([t.restElement(argsBinding)], t.callExpression(t.super(), [t.spreadElement(t.identifier(argsBinding.name))]));
371
+ return arrowFunctionExpression([restElement(argsBinding)], callExpression(_super(), [spreadElement(identifier(argsBinding.name))]));
321
372
  });
322
373
  }
323
374
 
@@ -328,20 +379,20 @@ function getSuperPropBinding(thisEnvFn, isAssignment, propName) {
328
379
  let fnBody;
329
380
 
330
381
  if (propName) {
331
- fnBody = t.memberExpression(t.super(), t.identifier(propName));
382
+ fnBody = memberExpression(_super(), identifier(propName));
332
383
  } else {
333
384
  const method = thisEnvFn.scope.generateUidIdentifier("prop");
334
385
  argsList.unshift(method);
335
- fnBody = t.memberExpression(t.super(), t.identifier(method.name), true);
386
+ fnBody = memberExpression(_super(), identifier(method.name), true);
336
387
  }
337
388
 
338
389
  if (isAssignment) {
339
390
  const valueIdent = thisEnvFn.scope.generateUidIdentifier("value");
340
391
  argsList.push(valueIdent);
341
- fnBody = t.assignmentExpression("=", fnBody, t.identifier(valueIdent.name));
392
+ fnBody = assignmentExpression("=", fnBody, identifier(valueIdent.name));
342
393
  }
343
394
 
344
- return t.arrowFunctionExpression(argsList, fnBody);
395
+ return arrowFunctionExpression(argsList, fnBody);
345
396
  });
346
397
  }
347
398
 
@@ -362,76 +413,89 @@ function getBinding(thisEnvFn, key, init) {
362
413
  return data;
363
414
  }
364
415
 
416
+ const getScopeInformationVisitor = (0, _visitors.merge)([{
417
+ ThisExpression(child, {
418
+ thisPaths
419
+ }) {
420
+ thisPaths.push(child);
421
+ },
422
+
423
+ JSXIdentifier(child, {
424
+ thisPaths
425
+ }) {
426
+ if (child.node.name !== "this") return;
427
+
428
+ if (!child.parentPath.isJSXMemberExpression({
429
+ object: child.node
430
+ }) && !child.parentPath.isJSXOpeningElement({
431
+ name: child.node
432
+ })) {
433
+ return;
434
+ }
435
+
436
+ thisPaths.push(child);
437
+ },
438
+
439
+ CallExpression(child, {
440
+ superCalls
441
+ }) {
442
+ if (child.get("callee").isSuper()) superCalls.push(child);
443
+ },
444
+
445
+ MemberExpression(child, {
446
+ superProps
447
+ }) {
448
+ if (child.get("object").isSuper()) superProps.push(child);
449
+ },
450
+
451
+ Identifier(child, {
452
+ argumentsPaths
453
+ }) {
454
+ if (!child.isReferencedIdentifier({
455
+ name: "arguments"
456
+ })) return;
457
+ let curr = child.scope;
458
+
459
+ do {
460
+ if (curr.hasOwnBinding("arguments")) {
461
+ curr.rename("arguments");
462
+ return;
463
+ }
464
+
465
+ if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
466
+ break;
467
+ }
468
+ } while (curr = curr.parent);
469
+
470
+ argumentsPaths.push(child);
471
+ },
472
+
473
+ MetaProperty(child, {
474
+ newTargetPaths
475
+ }) {
476
+ if (!child.get("meta").isIdentifier({
477
+ name: "new"
478
+ })) return;
479
+ if (!child.get("property").isIdentifier({
480
+ name: "target"
481
+ })) return;
482
+ newTargetPaths.push(child);
483
+ }
484
+
485
+ }, _helperEnvironmentVisitor.default]);
486
+
365
487
  function getScopeInformation(fnPath) {
366
488
  const thisPaths = [];
367
489
  const argumentsPaths = [];
368
490
  const newTargetPaths = [];
369
491
  const superProps = [];
370
492
  const superCalls = [];
371
- fnPath.traverse({
372
- ClassProperty(child) {
373
- child.skip();
374
- },
375
-
376
- Function(child) {
377
- if (child.isArrowFunctionExpression()) return;
378
- child.skip();
379
- },
380
-
381
- ThisExpression(child) {
382
- thisPaths.push(child);
383
- },
384
-
385
- JSXIdentifier(child) {
386
- if (child.node.name !== "this") return;
387
-
388
- if (!child.parentPath.isJSXMemberExpression({
389
- object: child.node
390
- }) && !child.parentPath.isJSXOpeningElement({
391
- name: child.node
392
- })) {
393
- return;
394
- }
395
-
396
- thisPaths.push(child);
397
- },
398
-
399
- CallExpression(child) {
400
- if (child.get("callee").isSuper()) superCalls.push(child);
401
- },
402
-
403
- MemberExpression(child) {
404
- if (child.get("object").isSuper()) superProps.push(child);
405
- },
406
-
407
- ReferencedIdentifier(child) {
408
- if (child.node.name !== "arguments") return;
409
- let curr = child.scope;
410
-
411
- do {
412
- if (curr.hasOwnBinding("arguments")) {
413
- curr.rename("arguments");
414
- return;
415
- }
416
-
417
- if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
418
- break;
419
- }
420
- } while (curr = curr.parent);
421
-
422
- argumentsPaths.push(child);
423
- },
424
-
425
- MetaProperty(child) {
426
- if (!child.get("meta").isIdentifier({
427
- name: "new"
428
- })) return;
429
- if (!child.get("property").isIdentifier({
430
- name: "target"
431
- })) return;
432
- newTargetPaths.push(child);
433
- }
434
-
493
+ fnPath.traverse(getScopeInformationVisitor, {
494
+ thisPaths,
495
+ argumentsPaths,
496
+ newTargetPaths,
497
+ superProps,
498
+ superCalls
435
499
  });
436
500
  return {
437
501
  thisPaths,
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.evaluateTruthy = evaluateTruthy;
7
6
  exports.evaluate = evaluate;
7
+ exports.evaluateTruthy = evaluateTruthy;
8
8
  const VALID_CALLEES = ["String", "Number", "Math"];
9
9
  const INVALID_METHODS = ["random"];
10
10