@babel/traverse 7.16.5 → 7.17.0

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/index.js CHANGED
@@ -23,8 +23,6 @@ Object.defineProperty(exports, "Scope", {
23
23
  });
24
24
  exports.visitors = exports.default = void 0;
25
25
 
26
- var _context = require("./context");
27
-
28
26
  var visitors = require("./visitors");
29
27
 
30
28
  exports.visitors = visitors;
@@ -33,6 +31,8 @@ var _t = require("@babel/types");
33
31
 
34
32
  var cache = require("./cache");
35
33
 
34
+ var _traverseNode = require("./traverse-node");
35
+
36
36
  var _path = require("./path");
37
37
 
38
38
  var _scope = require("./scope");
@@ -59,7 +59,7 @@ function traverse(parent, opts = {}, scope, state, parentPath) {
59
59
  }
60
60
 
61
61
  visitors.explode(opts);
62
- traverse.node(parent, opts, scope, state, parentPath);
62
+ (0, _traverseNode.traverseNode)(parent, opts, scope, state, parentPath);
63
63
  }
64
64
 
65
65
  var _default = traverse;
@@ -72,15 +72,8 @@ traverse.cheap = function (node, enter) {
72
72
  return traverseFast(node, enter);
73
73
  };
74
74
 
75
- traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
76
- const keys = VISITOR_KEYS[node.type];
77
- if (!keys) return;
78
- const context = new _context.default(scope, opts, state, parentPath);
79
-
80
- for (const key of keys) {
81
- if (skipKeys && skipKeys[key]) continue;
82
- if (context.visit(node, key)) return;
83
- }
75
+ traverse.node = function (node, opts, scope, state, path, skipKeys) {
76
+ (0, _traverseNode.traverseNode)(node, opts, scope, state, path, skipKeys);
84
77
  };
85
78
 
86
79
  traverse.clearNode = function (node, opts) {
@@ -24,9 +24,9 @@ exports.skipKey = skipKey;
24
24
  exports.stop = stop;
25
25
  exports.visit = visit;
26
26
 
27
- var _index = require("../index");
27
+ var _traverseNode = require("../traverse-node");
28
28
 
29
- var _index2 = require("./index");
29
+ var _index = require("./index");
30
30
 
31
31
  function call(key) {
32
32
  const opts = this.opts;
@@ -104,9 +104,7 @@ function visit() {
104
104
 
105
105
  restoreContext(this, currentContext);
106
106
  this.debug("Recursing into...");
107
-
108
- _index.default.node(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
109
-
107
+ this.shouldStop = (0, _traverseNode.traverseNode)(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
110
108
  restoreContext(this, currentContext);
111
109
  this.call("exit");
112
110
  return this.shouldStop;
@@ -125,7 +123,7 @@ function skipKey(key) {
125
123
  }
126
124
 
127
125
  function stop() {
128
- this._traverseFlags |= _index2.SHOULD_SKIP | _index2.SHOULD_STOP;
126
+ this._traverseFlags |= _index.SHOULD_SKIP | _index.SHOULD_STOP;
129
127
  }
130
128
 
131
129
  function setScope() {
@@ -28,6 +28,8 @@ const {
28
28
  identifier,
29
29
  isIdentifier,
30
30
  jsxIdentifier,
31
+ logicalExpression,
32
+ LOGICAL_OPERATORS,
31
33
  memberExpression,
32
34
  metaProperty,
33
35
  numericLiteral,
@@ -302,20 +304,34 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
302
304
  };
303
305
  }
304
306
 
307
+ function isLogicalOp(op) {
308
+ return LOGICAL_OPERATORS.includes(op);
309
+ }
310
+
305
311
  function standardizeSuperProperty(superProp) {
306
312
  if (superProp.parentPath.isAssignmentExpression() && superProp.parentPath.node.operator !== "=") {
307
313
  const assignmentPath = superProp.parentPath;
308
314
  const op = assignmentPath.node.operator.slice(0, -1);
309
315
  const value = assignmentPath.node.right;
310
- assignmentPath.node.operator = "=";
316
+ const isLogicalAssignment = isLogicalOp(op);
311
317
 
312
318
  if (superProp.node.computed) {
313
319
  const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
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));
320
+ const object = superProp.node.object;
321
+ const property = superProp.node.property;
322
+ assignmentPath.get("left").replaceWith(memberExpression(object, assignmentExpression("=", tmp, property), true));
323
+ assignmentPath.get("right").replaceWith(rightExpression(isLogicalAssignment ? "=" : op, memberExpression(object, identifier(tmp.name), true), value));
324
+ } else {
325
+ const object = superProp.node.object;
326
+ const property = superProp.node.property;
327
+ assignmentPath.get("left").replaceWith(memberExpression(object, property));
328
+ assignmentPath.get("right").replaceWith(rightExpression(isLogicalAssignment ? "=" : op, memberExpression(object, identifier(property.name)), value));
329
+ }
330
+
331
+ if (isLogicalAssignment) {
332
+ assignmentPath.replaceWith(logicalExpression(op, assignmentPath.node.left, assignmentPath.node.right));
316
333
  } else {
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));
334
+ assignmentPath.node.operator = "=";
319
335
  }
320
336
 
321
337
  return [assignmentPath.get("left"), assignmentPath.get("right").get("left")];
@@ -323,7 +339,7 @@ function standardizeSuperProperty(superProp) {
323
339
  const updateExpr = superProp.parentPath;
324
340
  const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
325
341
  const computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null;
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)))];
342
+ 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(superProp.parentPath.node.operator[0], identifier(tmp.name), numericLiteral(1)))];
327
343
 
328
344
  if (!superProp.parentPath.node.prefix) {
329
345
  parts.push(identifier(tmp.name));
@@ -336,6 +352,14 @@ function standardizeSuperProperty(superProp) {
336
352
  }
337
353
 
338
354
  return [superProp];
355
+
356
+ function rightExpression(op, left, right) {
357
+ if (op === "=") {
358
+ return assignmentExpression("=", left, right);
359
+ } else {
360
+ return binaryExpression(op, left, right);
361
+ }
362
+ }
339
363
  }
340
364
 
341
365
  function hasSuperClass(thisEnvFn) {
package/lib/path/index.js CHANGED
@@ -135,6 +135,10 @@ class NodePath {
135
135
  return val;
136
136
  }
137
137
 
138
+ hasNode() {
139
+ return this.node != null;
140
+ }
141
+
138
142
  buildCodeFrameError(msg, Error = SyntaxError) {
139
143
  return this.hub.buildError(this.node, msg, Error);
140
144
  }
@@ -467,7 +467,7 @@ class Scope {
467
467
  checkBlockScopedCollisions(local, kind, name, id) {
468
468
  if (kind === "param") return;
469
469
  if (local.kind === "local") return;
470
- const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && (kind === "let" || kind === "const");
470
+ const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && kind === "const";
471
471
 
472
472
  if (duplicate) {
473
473
  throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.traverseNode = traverseNode;
7
+
8
+ var _context = require("./context");
9
+
10
+ var _t = require("@babel/types");
11
+
12
+ const {
13
+ VISITOR_KEYS
14
+ } = _t;
15
+
16
+ function traverseNode(node, opts, scope, state, path, skipKeys) {
17
+ const keys = VISITOR_KEYS[node.type];
18
+ if (!keys) return false;
19
+ const context = new _context.default(scope, opts, state, path);
20
+
21
+ for (const key of keys) {
22
+ if (skipKeys && skipKeys[key]) continue;
23
+
24
+ if (context.visit(node, key)) {
25
+ return true;
26
+ }
27
+ }
28
+
29
+ return false;
30
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babel/traverse",
3
- "version": "7.16.5",
3
+ "version": "7.17.0",
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,19 +16,19 @@
16
16
  },
17
17
  "main": "./lib/index.js",
18
18
  "dependencies": {
19
- "@babel/code-frame": "^7.16.0",
20
- "@babel/generator": "^7.16.5",
21
- "@babel/helper-environment-visitor": "^7.16.5",
22
- "@babel/helper-function-name": "^7.16.0",
23
- "@babel/helper-hoist-variables": "^7.16.0",
24
- "@babel/helper-split-export-declaration": "^7.16.0",
25
- "@babel/parser": "^7.16.5",
26
- "@babel/types": "^7.16.0",
19
+ "@babel/code-frame": "^7.16.7",
20
+ "@babel/generator": "^7.17.0",
21
+ "@babel/helper-environment-visitor": "^7.16.7",
22
+ "@babel/helper-function-name": "^7.16.7",
23
+ "@babel/helper-hoist-variables": "^7.16.7",
24
+ "@babel/helper-split-export-declaration": "^7.16.7",
25
+ "@babel/parser": "^7.17.0",
26
+ "@babel/types": "^7.17.0",
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.5"
31
+ "@babel/helper-plugin-test-runner": "^7.16.7"
32
32
  },
33
33
  "engines": {
34
34
  "node": ">=6.9.0"
@@ -1,6 +1,5 @@
1
1
  import t from "@babel/types";
2
2
  import virtualTypes from "../../lib/path/lib/virtual-types.js";
3
- import definitions from "@babel/types/lib/definitions/index.js";
4
3
 
5
4
  export default function generateValidators() {
6
5
  let output = `/*
@@ -21,7 +20,7 @@ export interface NodePathValidators {
21
20
  for (const type of Object.keys(virtualTypes)) {
22
21
  const { types } = virtualTypes[type];
23
22
  if (type[0] === "_") continue;
24
- if (definitions.NODE_FIELDS[type] || definitions.FLIPPED_ALIAS_KEYS[type]) {
23
+ if (t.NODE_FIELDS[type] || t.FLIPPED_ALIAS_KEYS[type]) {
25
24
  output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
26
25
  } else if (types /* in VirtualTypeAliases */) {
27
26
  output += `is${type}(opts?: object): this is NodePath<VirtualTypeAliases["${type}"]>;`;