@babel/traverse 7.16.7 → 7.17.3

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
  }
@@ -30,7 +30,13 @@ const {
30
30
  callExpression,
31
31
  cloneNode,
32
32
  expressionStatement,
33
- isExpression
33
+ isAssignmentExpression,
34
+ isCallExpression,
35
+ isExpression,
36
+ isIdentifier,
37
+ isSequenceExpression,
38
+ isSuper,
39
+ thisExpression
34
40
  } = _t;
35
41
 
36
42
  function insertBefore(nodes_) {
@@ -96,9 +102,28 @@ function _containerInsertAfter(nodes) {
96
102
  return this._containerInsert(this.key + 1, nodes);
97
103
  }
98
104
 
105
+ const last = arr => arr[arr.length - 1];
106
+
107
+ function isHiddenInSequenceExpression(path) {
108
+ return isSequenceExpression(path.parent) && (last(path.parent.expressions) !== path.node || isHiddenInSequenceExpression(path.parentPath));
109
+ }
110
+
111
+ function isAlmostConstantAssignment(node, scope) {
112
+ if (!isAssignmentExpression(node) || !isIdentifier(node.left)) {
113
+ return false;
114
+ }
115
+
116
+ const blockScope = scope.getBlockParent();
117
+ return blockScope.hasOwnBinding(node.left.name) && blockScope.getOwnBinding(node.left.name).constantViolations.length <= 1;
118
+ }
119
+
99
120
  function insertAfter(nodes_) {
100
121
  this._assertUnremoved();
101
122
 
123
+ if (this.isSequenceExpression()) {
124
+ return last(this.get("expressions")).insertAfter(nodes_);
125
+ }
126
+
102
127
  const nodes = this._verifyNodeList(nodes_);
103
128
 
104
129
  const {
@@ -123,16 +148,28 @@ function insertAfter(nodes_) {
123
148
  return [this];
124
149
  }
125
150
 
126
- if (parentPath.isMethod({
127
- computed: true,
128
- key: node
129
- })) {
130
- scope = scope.parent;
151
+ if (isHiddenInSequenceExpression(this)) {
152
+ nodes.unshift(node);
153
+ } else if (isCallExpression(node) && isSuper(node.callee)) {
154
+ nodes.unshift(node);
155
+ nodes.push(thisExpression());
156
+ } else if (isAlmostConstantAssignment(node, scope)) {
157
+ nodes.unshift(node);
158
+ nodes.push(cloneNode(node.left));
159
+ } else if (scope.isPure(node, true)) {
160
+ nodes.push(node);
161
+ } else {
162
+ if (parentPath.isMethod({
163
+ computed: true,
164
+ key: node
165
+ })) {
166
+ scope = scope.parent;
167
+ }
168
+
169
+ const temp = scope.generateDeclaredUidIdentifier();
170
+ nodes.unshift(expressionStatement(assignmentExpression("=", cloneNode(temp), node)));
171
+ nodes.push(expressionStatement(cloneNode(temp)));
131
172
  }
132
-
133
- const temp = scope.generateDeclaredUidIdentifier();
134
- nodes.unshift(expressionStatement(assignmentExpression("=", cloneNode(temp), node)));
135
- nodes.push(expressionStatement(cloneNode(temp)));
136
173
  }
137
174
 
138
175
  return this.replaceExpressionWithStatements(nodes);
@@ -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);
@@ -848,8 +848,8 @@ class Scope {
848
848
  }
849
849
 
850
850
  const declarator = variableDeclarator(opts.id, opts.init);
851
- declarPath.node.declarations.push(declarator);
852
- this.registerBinding(kind, declarPath.get("declarations").pop());
851
+ const len = declarPath.node.declarations.push(declarator);
852
+ path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
853
853
  }
854
854
 
855
855
  getProgramParent() {
@@ -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.7",
3
+ "version": "7.17.3",
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",
@@ -17,13 +17,13 @@
17
17
  "main": "./lib/index.js",
18
18
  "dependencies": {
19
19
  "@babel/code-frame": "^7.16.7",
20
- "@babel/generator": "^7.16.7",
20
+ "@babel/generator": "^7.17.3",
21
21
  "@babel/helper-environment-visitor": "^7.16.7",
22
22
  "@babel/helper-function-name": "^7.16.7",
23
23
  "@babel/helper-hoist-variables": "^7.16.7",
24
24
  "@babel/helper-split-export-declaration": "^7.16.7",
25
- "@babel/parser": "^7.16.7",
26
- "@babel/types": "^7.16.7",
25
+ "@babel/parser": "^7.17.3",
26
+ "@babel/types": "^7.17.0",
27
27
  "debug": "^4.1.0",
28
28
  "globals": "^11.1.0"
29
29
  },