@babel/traverse 7.14.2 → 7.14.9

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
@@ -9,8 +9,6 @@ var _path = require("./path");
9
9
 
10
10
  var t = require("@babel/types");
11
11
 
12
- const testing = process.env.NODE_ENV === "test";
13
-
14
12
  class TraversalContext {
15
13
  constructor(scope, opts, state, parentPath) {
16
14
  this.queue = null;
@@ -46,10 +44,6 @@ class TraversalContext {
46
44
  }
47
45
 
48
46
  maybeQueue(path, notPriority) {
49
- if (this.trap) {
50
- throw new Error("Infinite cycle detected");
51
- }
52
-
53
47
  if (this.queue) {
54
48
  if (notPriority) {
55
49
  this.queue.push(path);
@@ -96,11 +90,6 @@ class TraversalContext {
96
90
  }
97
91
 
98
92
  if (path.key === null) continue;
99
-
100
- if (testing && queue.length >= 10000) {
101
- this.trap = true;
102
- }
103
-
104
93
  const {
105
94
  node
106
95
  } = path;
@@ -166,7 +166,15 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
166
166
  }
167
167
 
168
168
  if (argumentsPaths.length > 0) {
169
- const argumentsBinding = getBinding(thisEnvFn, "arguments", () => t.identifier("arguments"));
169
+ const argumentsBinding = getBinding(thisEnvFn, "arguments", () => {
170
+ const args = () => t.identifier("arguments");
171
+
172
+ if (thisEnvFn.scope.path.isProgram()) {
173
+ return t.conditionalExpression(t.binaryExpression("===", t.unaryExpression("typeof", args()), t.stringLiteral("undefined")), thisEnvFn.scope.buildUndefinedNode(), args());
174
+ } else {
175
+ return args();
176
+ }
177
+ });
170
178
  argumentsPaths.forEach(argumentsChild => {
171
179
  const argsRef = t.identifier(argumentsBinding);
172
180
  argsRef.loc = argumentsChild.node.loc;
@@ -398,6 +406,19 @@ function getScopeInformation(fnPath) {
398
406
 
399
407
  ReferencedIdentifier(child) {
400
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
+
401
422
  argumentsPaths.push(child);
402
423
  },
403
424
 
@@ -158,7 +158,14 @@ function getStatementListCompletion(paths, context) {
158
158
  }
159
159
  }
160
160
  } else if (paths.length) {
161
- completions = completions.concat(_getCompletionRecords(paths[paths.length - 1], context));
161
+ for (let i = paths.length - 1; i >= 0; i--) {
162
+ const pathCompletions = _getCompletionRecords(paths[i], context);
163
+
164
+ if (pathCompletions.length > 1 || pathCompletions.length === 1 && !pathCompletions[0].path.isVariableDeclaration()) {
165
+ completions = completions.concat(pathCompletions);
166
+ break;
167
+ }
168
+ }
162
169
  }
163
170
 
164
171
  return completions;
@@ -22,33 +22,7 @@ var _parser = require("@babel/parser");
22
22
 
23
23
  var t = require("@babel/types");
24
24
 
25
- const hoistVariablesVisitor = {
26
- Function(path) {
27
- path.skip();
28
- },
29
-
30
- VariableDeclaration(path) {
31
- if (path.node.kind !== "var") return;
32
- const bindings = path.getBindingIdentifiers();
33
-
34
- for (const key of Object.keys(bindings)) {
35
- path.scope.push({
36
- id: bindings[key]
37
- });
38
- }
39
-
40
- const exprs = [];
41
-
42
- for (const declar of path.node.declarations) {
43
- if (declar.init) {
44
- exprs.push(t.expressionStatement(t.assignmentExpression("=", declar.id, declar.init)));
45
- }
46
- }
47
-
48
- path.replaceWithMultiple(exprs);
49
- }
50
-
51
- };
25
+ var _helperHoistVariables = require("@babel/helper-hoist-variables");
52
26
 
53
27
  function replaceWithMultiple(nodes) {
54
28
  var _pathCache$get;
@@ -191,7 +165,12 @@ function replaceExpressionWithStatements(nodes) {
191
165
  const isParentGenerator = functionParent == null ? void 0 : functionParent.is("generator");
192
166
  const container = t.arrowFunctionExpression([], t.blockStatement(nodes));
193
167
  this.replaceWith(t.callExpression(container, []));
194
- this.traverse(hoistVariablesVisitor);
168
+ const callee = this.get("callee");
169
+ (0, _helperHoistVariables.default)(callee.get("body"), id => {
170
+ this.scope.push({
171
+ id
172
+ });
173
+ }, "var");
195
174
  const completionRecords = this.get("callee").getCompletionRecords();
196
175
 
197
176
  for (const path of completionRecords) {
@@ -202,7 +181,6 @@ function replaceExpressionWithStatements(nodes) {
202
181
  let uid = loop.getData("expressionReplacementReturnUid");
203
182
 
204
183
  if (!uid) {
205
- const callee = this.get("callee");
206
184
  uid = callee.scope.generateDeclaredUidIdentifier("ret");
207
185
  callee.get("body").pushContainer("body", t.returnStatement(t.cloneNode(uid)));
208
186
  loop.setData("expressionReplacementReturnUid", uid);
@@ -216,15 +194,15 @@ function replaceExpressionWithStatements(nodes) {
216
194
  }
217
195
  }
218
196
 
219
- const callee = this.get("callee");
220
197
  callee.arrowFunctionToExpression();
198
+ const newCallee = callee;
221
199
 
222
200
  const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", t.FUNCTION_TYPES);
223
201
 
224
202
  const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", t.FUNCTION_TYPES);
225
203
 
226
204
  if (needToAwaitFunction) {
227
- callee.set("async", true);
205
+ newCallee.set("async", true);
228
206
 
229
207
  if (!needToYieldFunction) {
230
208
  this.replaceWith(t.awaitExpression(this.node));
@@ -232,11 +210,11 @@ function replaceExpressionWithStatements(nodes) {
232
210
  }
233
211
 
234
212
  if (needToYieldFunction) {
235
- callee.set("generator", true);
213
+ newCallee.set("generator", true);
236
214
  this.replaceWith(t.yieldExpression(this.node, true));
237
215
  }
238
216
 
239
- return callee.get("body.body");
217
+ return newCallee.get("body.body");
240
218
  }
241
219
 
242
220
  function replaceInline(nodes) {
@@ -172,11 +172,17 @@ const collectorVisitor = {
172
172
 
173
173
  Declaration(path) {
174
174
  if (path.isBlockScoped()) return;
175
+ if (path.isImportDeclaration()) return;
175
176
  if (path.isExportDeclaration()) return;
176
177
  const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
177
178
  parent.registerDeclaration(path);
178
179
  },
179
180
 
181
+ ImportDeclaration(path) {
182
+ const parent = path.scope.getBlockParent();
183
+ parent.registerDeclaration(path);
184
+ },
185
+
180
186
  ReferencedIdentifier(path, state) {
181
187
  state.references.push(path);
182
188
  },
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@babel/traverse",
3
- "version": "7.14.2",
3
+ "version": "7.14.9",
4
4
  "description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes",
5
- "author": "Sebastian McKenzie <sebmck@gmail.com>",
5
+ "author": "The Babel Team (https://babel.dev/team)",
6
6
  "homepage": "https://babel.dev/docs/en/next/babel-traverse",
7
7
  "bugs": "https://github.com/babel/babel/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3A%22pkg%3A%20traverse%22+is%3Aopen",
8
8
  "license": "MIT",
@@ -14,18 +14,22 @@
14
14
  "url": "https://github.com/babel/babel.git",
15
15
  "directory": "packages/babel-traverse"
16
16
  },
17
- "main": "lib/index.js",
17
+ "main": "./lib/index.js",
18
18
  "dependencies": {
19
- "@babel/code-frame": "^7.12.13",
20
- "@babel/generator": "^7.14.2",
21
- "@babel/helper-function-name": "^7.14.2",
22
- "@babel/helper-split-export-declaration": "^7.12.13",
23
- "@babel/parser": "^7.14.2",
24
- "@babel/types": "^7.14.2",
19
+ "@babel/code-frame": "^7.14.5",
20
+ "@babel/generator": "^7.14.9",
21
+ "@babel/helper-function-name": "^7.14.5",
22
+ "@babel/helper-hoist-variables": "^7.14.5",
23
+ "@babel/helper-split-export-declaration": "^7.14.5",
24
+ "@babel/parser": "^7.14.9",
25
+ "@babel/types": "^7.14.9",
25
26
  "debug": "^4.1.0",
26
27
  "globals": "^11.1.0"
27
28
  },
28
29
  "devDependencies": {
29
- "@babel/helper-plugin-test-runner": "7.13.10"
30
+ "@babel/helper-plugin-test-runner": "7.14.5"
31
+ },
32
+ "engines": {
33
+ "node": ">=6.9.0"
30
34
  }
31
35
  }
@@ -9,6 +9,7 @@ export default function generateValidators() {
9
9
  */
10
10
  import * as t from "@babel/types";
11
11
  import NodePath from "../index";
12
+ import type { VirtualTypeAliases } from "./virtual-types";
12
13
 
13
14
  export interface NodePathValidators {
14
15
  `;
@@ -18,10 +19,18 @@ export interface NodePathValidators {
18
19
  }
19
20
 
20
21
  for (const type of Object.keys(virtualTypes)) {
22
+ const { types } = virtualTypes[type];
21
23
  if (type[0] === "_") continue;
22
24
  if (definitions.NODE_FIELDS[type] || definitions.FLIPPED_ALIAS_KEYS[type]) {
23
25
  output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
26
+ } else if (types /* in VirtualTypeAliases */) {
27
+ output += `is${type}(opts?: object): this is NodePath<VirtualTypeAliases["${type}"]>;`;
24
28
  } else {
29
+ // if it don't have types, then VirtualTypeAliases[type] is t.Node
30
+ // which TS marked as always true
31
+ // eg. if (path.isBlockScope()) return;
32
+ // path resolved to `never` here
33
+ // so we have to return boolean instead of this is NodePath<t.Node> here
25
34
  output += `is${type}(opts?: object): boolean;`;
26
35
  }
27
36
  }