@babel/traverse 7.18.0 → 7.18.6

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/context.js CHANGED
@@ -31,17 +31,19 @@ class TraversalContext {
31
31
  if (!(keys != null && keys.length)) return false;
32
32
 
33
33
  for (const key of keys) {
34
- if (node[key]) return true;
34
+ if (node[key]) {
35
+ return true;
36
+ }
35
37
  }
36
38
 
37
39
  return false;
38
40
  }
39
41
 
40
- create(node, obj, key, listKey) {
42
+ create(node, container, key, listKey) {
41
43
  return _path.default.get({
42
44
  parentPath: this.parentPath,
43
45
  parent: node,
44
- container: obj,
46
+ container,
45
47
  key: key,
46
48
  listKey
47
49
  });
@@ -129,7 +129,11 @@ function stop() {
129
129
  function setScope() {
130
130
  if (this.opts && this.opts.noScope) return;
131
131
  let path = this.parentPath;
132
- if (this.key === "key" && path.isMethod()) path = path.parentPath;
132
+
133
+ if ((this.key === "key" || this.listKey === "decorators") && path.isMethod()) {
134
+ path = path.parentPath;
135
+ }
136
+
133
137
  let target;
134
138
 
135
139
  while (path && !target) {
@@ -177,7 +181,10 @@ function _resyncParent() {
177
181
 
178
182
  function _resyncKey() {
179
183
  if (!this.container) return;
180
- if (this.node === this.container[this.key]) return;
184
+
185
+ if (this.node === this.container[this.key]) {
186
+ return;
187
+ }
181
188
 
182
189
  if (Array.isArray(this.container)) {
183
190
  for (let i = 0; i < this.container.length; i++) {
@@ -251,10 +251,11 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
251
251
  const flatSuperProps = superProps.reduce((acc, superProp) => acc.concat(standardizeSuperProperty(superProp)), []);
252
252
  flatSuperProps.forEach(superProp => {
253
253
  const key = superProp.node.computed ? "" : superProp.get("property").node.name;
254
- const isAssignment = superProp.parentPath.isAssignmentExpression({
254
+ const superParentPath = superProp.parentPath;
255
+ const isAssignment = superParentPath.isAssignmentExpression({
255
256
  left: superProp.node
256
257
  });
257
- const isCall = superProp.parentPath.isCallExpression({
258
+ const isCall = superParentPath.isCallExpression({
258
259
  callee: superProp.node
259
260
  });
260
261
  const superBinding = getSuperPropBinding(thisEnvFn, isAssignment, key);
@@ -265,18 +266,18 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
265
266
  }
266
267
 
267
268
  if (isAssignment) {
268
- const value = superProp.parentPath.node.right;
269
+ const value = superParentPath.node.right;
269
270
  args.push(value);
270
271
  }
271
272
 
272
273
  const call = callExpression(identifier(superBinding), args);
273
274
 
274
275
  if (isCall) {
275
- superProp.parentPath.unshiftContainer("arguments", thisExpression());
276
+ superParentPath.unshiftContainer("arguments", thisExpression());
276
277
  superProp.replaceWith(memberExpression(call, identifier("call")));
277
- thisPaths.push(superProp.parentPath.get("arguments.0"));
278
+ thisPaths.push(superParentPath.get("arguments.0"));
278
279
  } else if (isAssignment) {
279
- superProp.parentPath.replaceWith(call);
280
+ superParentPath.replaceWith(call);
280
281
  } else {
281
282
  superProp.replaceWith(call);
282
283
  }
@@ -8,6 +8,14 @@ exports.evaluateTruthy = evaluateTruthy;
8
8
  const VALID_CALLEES = ["String", "Number", "Math"];
9
9
  const INVALID_METHODS = ["random"];
10
10
 
11
+ function isValidCallee(val) {
12
+ return VALID_CALLEES.includes(val);
13
+ }
14
+
15
+ function isInvalidMethod(val) {
16
+ return INVALID_METHODS.includes(val);
17
+ }
18
+
11
19
  function evaluateTruthy() {
12
20
  const res = this.evaluate();
13
21
  if (res.confident) return !!res.value;
@@ -336,7 +344,7 @@ function _evaluate(path, state) {
336
344
  let context;
337
345
  let func;
338
346
 
339
- if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name) && VALID_CALLEES.indexOf(callee.node.name) >= 0) {
347
+ if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name) && isValidCallee(callee.node.name)) {
340
348
  func = global[callee.node.name];
341
349
  }
342
350
 
@@ -344,7 +352,7 @@ function _evaluate(path, state) {
344
352
  const object = callee.get("object");
345
353
  const property = callee.get("property");
346
354
 
347
- if (object.isIdentifier() && property.isIdentifier() && VALID_CALLEES.indexOf(object.node.name) >= 0 && INVALID_METHODS.indexOf(property.node.name) < 0) {
355
+ if (object.isIdentifier() && property.isIdentifier() && isValidCallee(object.node.name) && !isInvalidMethod(property.node.name)) {
348
356
  context = global[object.node.name];
349
357
  func = context[property.node.name];
350
358
  }
package/lib/path/index.js CHANGED
@@ -227,6 +227,9 @@ class NodePath {
227
227
  }
228
228
 
229
229
  Object.assign(NodePath.prototype, NodePath_ancestry, NodePath_inference, NodePath_replacement, NodePath_evaluation, NodePath_conversion, NodePath_introspection, NodePath_context, NodePath_removal, NodePath_modification, NodePath_family, NodePath_comments);
230
+ {
231
+ NodePath.prototype._guessExecutionStatusRelativeToDifferentFunctions = NodePath_introspection._guessExecutionStatusRelativeTo;
232
+ }
230
233
 
231
234
  for (const type of t.TYPES) {
232
235
  const typeKey = `is${type}`;
@@ -33,10 +33,16 @@ const {
33
33
  } = _t;
34
34
 
35
35
  function getTypeAnnotation() {
36
- if (this.typeAnnotation) return this.typeAnnotation;
37
- let type = this._getTypeAnnotation() || anyTypeAnnotation();
36
+ let type = this.getData("typeAnnotation");
37
+
38
+ if (type != null) {
39
+ return type;
40
+ }
41
+
42
+ type = this._getTypeAnnotation() || anyTypeAnnotation();
38
43
  if (isTypeAnnotation(type)) type = type.typeAnnotation;
39
- return this.typeAnnotation = type;
44
+ this.setData("typeAnnotation", type);
45
+ return type;
40
46
  }
41
47
 
42
48
  const typeAnnotationInferringNodes = new WeakSet();
@@ -202,5 +202,5 @@ function getConditionalAnnotation(binding, path, name) {
202
202
  };
203
203
  }
204
204
 
205
- return getConditionalAnnotation(ifStatement, name);
205
+ return getConditionalAnnotation(binding, ifStatement, name);
206
206
  }
@@ -87,7 +87,7 @@ function TypeCastExpression(node) {
87
87
  TypeCastExpression.validParent = true;
88
88
 
89
89
  function NewExpression(node) {
90
- if (this.get("callee").isIdentifier()) {
90
+ if (node.callee.type === "Identifier") {
91
91
  return genericTypeAnnotation(node.callee);
92
92
  }
93
93
  }
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports._guessExecutionStatusRelativeTo = _guessExecutionStatusRelativeTo;
7
- exports._guessExecutionStatusRelativeToDifferentFunctions = _guessExecutionStatusRelativeToDifferentFunctions;
8
7
  exports._resolve = _resolve;
9
8
  exports.canHaveVariableDeclarationOrExpression = canHaveVariableDeclarationOrExpression;
10
9
  exports.canSwapBetweenExpressionAndStatement = canSwapBetweenExpressionAndStatement;
@@ -232,20 +231,24 @@ function isExecutionUncertainInList(paths, maxIndex) {
232
231
  }
233
232
 
234
233
  function _guessExecutionStatusRelativeTo(target) {
234
+ return _guessExecutionStatusRelativeToCached(this, target, new Map());
235
+ }
236
+
237
+ function _guessExecutionStatusRelativeToCached(base, target, cache) {
235
238
  const funcParent = {
236
- this: getOuterFunction(this),
239
+ this: getOuterFunction(base),
237
240
  target: getOuterFunction(target)
238
241
  };
239
242
 
240
243
  if (funcParent.target.node !== funcParent.this.node) {
241
- return this._guessExecutionStatusRelativeToDifferentFunctions(funcParent.target);
244
+ return _guessExecutionStatusRelativeToDifferentFunctionsCached(base, funcParent.target, cache);
242
245
  }
243
246
 
244
247
  const paths = {
245
248
  target: target.getAncestry(),
246
- this: this.getAncestry()
249
+ this: base.getAncestry()
247
250
  };
248
- if (paths.target.indexOf(this) >= 0) return "after";
251
+ if (paths.target.indexOf(base) >= 0) return "after";
249
252
  if (paths.this.indexOf(target) >= 0) return "before";
250
253
  let commonPath;
251
254
  const commonIndex = {
@@ -289,9 +292,9 @@ function _guessExecutionStatusRelativeTo(target) {
289
292
  return keyPosition.target > keyPosition.this ? "before" : "after";
290
293
  }
291
294
 
292
- const executionOrderCheckedNodes = new WeakSet();
295
+ const executionOrderCheckedNodes = new Set();
293
296
 
294
- function _guessExecutionStatusRelativeToDifferentFunctions(target) {
297
+ function _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache) {
295
298
  if (!target.isFunctionDeclaration() || target.parentPath.isExportDeclaration()) {
296
299
  return "unknown";
297
300
  }
@@ -312,20 +315,37 @@ function _guessExecutionStatusRelativeToDifferentFunctions(target) {
312
315
  if (executionOrderCheckedNodes.has(path.node)) continue;
313
316
  executionOrderCheckedNodes.add(path.node);
314
317
 
315
- const status = this._guessExecutionStatusRelativeTo(path);
316
-
317
- executionOrderCheckedNodes.delete(path.node);
318
+ try {
319
+ const status = _guessExecutionStatusRelativeToCached(base, path, cache);
318
320
 
319
- if (allStatus && allStatus !== status) {
320
- return "unknown";
321
- } else {
322
- allStatus = status;
321
+ if (allStatus && allStatus !== status) {
322
+ return "unknown";
323
+ } else {
324
+ allStatus = status;
325
+ }
326
+ } finally {
327
+ executionOrderCheckedNodes.delete(path.node);
323
328
  }
324
329
  }
325
330
 
326
331
  return allStatus;
327
332
  }
328
333
 
334
+ function _guessExecutionStatusRelativeToDifferentFunctionsCached(base, target, cache) {
335
+ let nodeMap = cache.get(base.node);
336
+
337
+ if (!nodeMap) {
338
+ cache.set(base.node, nodeMap = new Map());
339
+ } else if (nodeMap.has(target.node)) {
340
+ return nodeMap.get(target.node);
341
+ }
342
+
343
+ const result = _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache);
344
+
345
+ nodeMap.set(target.node, result);
346
+ return result;
347
+ }
348
+
329
349
  function resolve(dangerous, resolved) {
330
350
  return this._resolve(dangerous, resolved) || this;
331
351
  }
@@ -4,6 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.hooks = void 0;
7
+
8
+ var _ = require("..");
9
+
7
10
  const hooks = [function (self, parent) {
8
11
  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
12
 
@@ -69,10 +69,11 @@ function replaceWithMultiple(nodes) {
69
69
 
70
70
  function replaceWithSourceString(replacement) {
71
71
  this.resync();
72
+ let ast;
72
73
 
73
74
  try {
74
75
  replacement = `(${replacement})`;
75
- replacement = (0, _parser.parse)(replacement);
76
+ ast = (0, _parser.parse)(replacement);
76
77
  } catch (err) {
77
78
  const loc = err.loc;
78
79
 
@@ -89,23 +90,21 @@ function replaceWithSourceString(replacement) {
89
90
  throw err;
90
91
  }
91
92
 
92
- replacement = replacement.program.body[0].expression;
93
+ const expressionAST = ast.program.body[0].expression;
93
94
 
94
- _index.default.removeProperties(replacement);
95
+ _index.default.removeProperties(expressionAST);
95
96
 
96
- return this.replaceWith(replacement);
97
+ return this.replaceWith(expressionAST);
97
98
  }
98
99
 
99
- function replaceWith(replacement) {
100
+ function replaceWith(replacementPath) {
100
101
  this.resync();
101
102
 
102
103
  if (this.removed) {
103
104
  throw new Error("You can't replace this node, we've already removed it");
104
105
  }
105
106
 
106
- if (replacement instanceof _index2.default) {
107
- replacement = replacement.node;
108
- }
107
+ let replacement = replacementPath instanceof _index2.default ? replacementPath.node : replacementPath;
109
108
 
110
109
  if (!replacement) {
111
110
  throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
@@ -38,9 +38,11 @@ const {
38
38
  isMethod,
39
39
  isModuleDeclaration,
40
40
  isModuleSpecifier,
41
+ isNullLiteral,
41
42
  isObjectExpression,
42
43
  isProperty,
43
44
  isPureish,
45
+ isRegExpLiteral,
44
46
  isSuper,
45
47
  isTaggedTemplateExpression,
46
48
  isTemplateLiteral,
@@ -75,7 +77,7 @@ function gatherNodeParts(node, parts) {
75
77
  }
76
78
  } else if (isModuleSpecifier(node)) {
77
79
  gatherNodeParts(node.local, parts);
78
- } else if (isLiteral(node)) {
80
+ } else if (isLiteral(node) && !isNullLiteral(node) && !isRegExpLiteral(node) && !isTemplateLiteral(node)) {
79
81
  parts.push(node.value);
80
82
  }
81
83
 
@@ -185,7 +187,7 @@ function gatherNodeParts(node, parts) {
185
187
  break;
186
188
 
187
189
  case "JSXOpeningElement":
188
- parts.push(node.name);
190
+ gatherNodeParts(node.name, parts);
189
191
  break;
190
192
 
191
193
  case "JSXFragment":
@@ -368,9 +370,9 @@ class Scope {
368
370
  path = this.path;
369
371
 
370
372
  do {
371
- const isKey = path.key === "key";
373
+ const shouldSkip = path.key === "key" || path.listKey === "decorators";
372
374
  path = path.parentPath;
373
- if (isKey && path.isMethod()) path = path.parentPath;
375
+ if (shouldSkip && path.isMethod()) path = path.parentPath;
374
376
  if (path && path.isScope()) parent = path;
375
377
  } while (path && !parent);
376
378
 
@@ -9,16 +9,10 @@ var _binding = require("../binding");
9
9
 
10
10
  var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
11
11
 
12
- var _t = require("@babel/types");
13
-
14
- const {
15
- VISITOR_KEYS,
16
- assignmentExpression,
17
- identifier,
18
- toExpression,
19
- variableDeclaration,
20
- variableDeclarator
21
- } = _t;
12
+ var t = require("@babel/types");
13
+
14
+ var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
15
+
22
16
  const renameVisitor = {
23
17
  ReferencedIdentifier({
24
18
  node
@@ -30,7 +24,11 @@ const renameVisitor = {
30
24
 
31
25
  Scope(path, state) {
32
26
  if (!path.scope.bindingIdentifierEquals(state.oldName, state.binding.identifier)) {
33
- skipAllButComputedMethodKey(path);
27
+ path.skip();
28
+
29
+ if (path.isMethod()) {
30
+ (0, _helperEnvironmentVisitor.requeueComputedKeyAndDecorators)(path);
31
+ }
34
32
  }
35
33
  },
36
34
 
@@ -59,7 +57,17 @@ class Renamer {
59
57
  return;
60
58
  }
61
59
 
62
- if (maybeExportDeclar.isExportDefaultDeclaration() && !maybeExportDeclar.get("declaration").node.id) {
60
+ if (maybeExportDeclar.isExportDefaultDeclaration()) {
61
+ const {
62
+ declaration
63
+ } = maybeExportDeclar.node;
64
+
65
+ if (t.isDeclaration(declaration) && !declaration.id) {
66
+ return;
67
+ }
68
+ }
69
+
70
+ if (maybeExportDeclar.isExportAllDeclaration()) {
63
71
  return;
64
72
  }
65
73
 
@@ -67,23 +75,11 @@ class Renamer {
67
75
  }
68
76
 
69
77
  maybeConvertFromClassFunctionDeclaration(path) {
70
- return;
71
- if (!path.isFunctionDeclaration() && !path.isClassDeclaration()) return;
72
- if (this.binding.kind !== "hoisted") return;
73
- path.node.id = identifier(this.oldName);
74
- path.node._blockHoist = 3;
75
- path.replaceWith(variableDeclaration("let", [variableDeclarator(identifier(this.newName), toExpression(path.node))]));
78
+ return path;
76
79
  }
77
80
 
78
81
  maybeConvertFromClassFunctionExpression(path) {
79
- return;
80
- if (!path.isFunctionExpression() && !path.isClassExpression()) return;
81
- if (this.binding.kind !== "local") return;
82
- path.node.id = identifier(this.oldName);
83
- this.binding.scope.parent.push({
84
- id: identifier(this.newName)
85
- });
86
- path.replaceWith(assignmentExpression("=", identifier(this.newName), path.node));
82
+ return path;
87
83
  }
88
84
 
89
85
  rename(block) {
@@ -123,24 +119,11 @@ class Renamer {
123
119
  }
124
120
 
125
121
  if (parentDeclar) {
126
- this.maybeConvertFromClassFunctionDeclaration(parentDeclar);
127
- this.maybeConvertFromClassFunctionExpression(parentDeclar);
122
+ this.maybeConvertFromClassFunctionDeclaration(path);
123
+ this.maybeConvertFromClassFunctionExpression(path);
128
124
  }
129
125
  }
130
126
 
131
127
  }
132
128
 
133
- exports.default = Renamer;
134
-
135
- function skipAllButComputedMethodKey(path) {
136
- if (!path.isMethod() || !path.node.computed) {
137
- path.skip();
138
- return;
139
- }
140
-
141
- const keys = VISITOR_KEYS[path.type];
142
-
143
- for (const key of keys) {
144
- if (key !== "key") path.skipKey(key);
145
- }
146
- }
129
+ exports.default = Renamer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babel/traverse",
3
- "version": "7.18.0",
3
+ "version": "7.18.6",
4
4
  "description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes",
5
5
  "author": "The Babel Team (https://babel.dev/team)",
6
6
  "homepage": "https://babel.dev/docs/en/next/babel-traverse",
@@ -16,21 +16,22 @@
16
16
  },
17
17
  "main": "./lib/index.js",
18
18
  "dependencies": {
19
- "@babel/code-frame": "^7.16.7",
20
- "@babel/generator": "^7.18.0",
21
- "@babel/helper-environment-visitor": "^7.16.7",
22
- "@babel/helper-function-name": "^7.17.9",
23
- "@babel/helper-hoist-variables": "^7.16.7",
24
- "@babel/helper-split-export-declaration": "^7.16.7",
25
- "@babel/parser": "^7.18.0",
26
- "@babel/types": "^7.18.0",
19
+ "@babel/code-frame": "^7.18.6",
20
+ "@babel/generator": "^7.18.6",
21
+ "@babel/helper-environment-visitor": "^7.18.6",
22
+ "@babel/helper-function-name": "^7.18.6",
23
+ "@babel/helper-hoist-variables": "^7.18.6",
24
+ "@babel/helper-split-export-declaration": "^7.18.6",
25
+ "@babel/parser": "^7.18.6",
26
+ "@babel/types": "^7.18.6",
27
27
  "debug": "^4.1.0",
28
28
  "globals": "^11.1.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@babel/helper-plugin-test-runner": "^7.16.7"
31
+ "@babel/helper-plugin-test-runner": "^7.18.6"
32
32
  },
33
33
  "engines": {
34
34
  "node": ">=6.9.0"
35
- }
35
+ },
36
+ "type": "commonjs"
36
37
  }
@@ -1,4 +1,4 @@
1
- import t from "@babel/types";
1
+ import * as t from "@babel/types";
2
2
 
3
3
  export default function generateAsserts() {
4
4
  let output = `/*
@@ -1,5 +1,5 @@
1
- import t from "@babel/types";
2
- import virtualTypes from "../../lib/path/lib/virtual-types.js";
1
+ import * as t from "@babel/types";
2
+ import * as virtualTypes from "../../lib/path/lib/virtual-types.js";
3
3
 
4
4
  export default function generateValidators() {
5
5
  let output = `/*
@@ -18,6 +18,9 @@ export interface NodePathValidators {
18
18
  }
19
19
 
20
20
  for (const type of Object.keys(virtualTypes)) {
21
+ // TODO: Remove this check once we stop compiling to CJS
22
+ if (type === "default" || type === "__esModule") continue;
23
+
21
24
  const { types } = virtualTypes[type];
22
25
  if (type[0] === "_") continue;
23
26
  if (t.NODE_FIELDS[type] || t.FLIPPED_ALIAS_KEYS[type]) {
@@ -1,4 +1,4 @@
1
- import virtualTypes from "../../lib/path/lib/virtual-types.js";
1
+ import * as virtualTypes from "../../lib/path/lib/virtual-types.js";
2
2
 
3
3
  export default function generateValidators() {
4
4
  let output = `/*
@@ -11,6 +11,9 @@ export interface VirtualTypeAliases {
11
11
  `;
12
12
 
13
13
  for (const type of Object.keys(virtualTypes)) {
14
+ // TODO: Remove this check once we stop compiling to CJS
15
+ if (type === "default" || type === "__esModule") continue;
16
+
14
17
  output += ` ${type}: ${(virtualTypes[type].types || ["Node"])
15
18
  .map(t => `t.${t}`)
16
19
  .join(" | ")};`;