@babel/traverse 7.16.3 → 7.16.10

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() {
@@ -11,8 +11,12 @@ exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
11
11
 
12
12
  var _t = require("@babel/types");
13
13
 
14
+ var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
15
+
14
16
  var _helperFunctionName = require("@babel/helper-function-name");
15
17
 
18
+ var _visitors = require("../visitors");
19
+
16
20
  const {
17
21
  arrowFunctionExpression,
18
22
  assignmentExpression,
@@ -24,6 +28,8 @@ const {
24
28
  identifier,
25
29
  isIdentifier,
26
30
  jsxIdentifier,
31
+ logicalExpression,
32
+ LOGICAL_OPERATORS,
27
33
  memberExpression,
28
34
  metaProperty,
29
35
  numericLiteral,
@@ -144,6 +150,16 @@ function arrowFunctionToExpression({
144
150
  }
145
151
  }
146
152
 
153
+ const getSuperCallsVisitor = (0, _visitors.merge)([{
154
+ CallExpression(child, {
155
+ allSuperCalls
156
+ }) {
157
+ if (!child.get("callee").isSuper()) return;
158
+ allSuperCalls.push(child);
159
+ }
160
+
161
+ }, _helperEnvironmentVisitor.default]);
162
+
147
163
  function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow = true) {
148
164
  let arrowParent;
149
165
  let thisEnvFn = fnPath.findParent(p => {
@@ -190,21 +206,8 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
190
206
  }
191
207
 
192
208
  const allSuperCalls = [];
193
- thisEnvFn.traverse({
194
- Function(child) {
195
- if (child.isArrowFunctionExpression()) return;
196
- child.skip();
197
- },
198
-
199
- ClassProperty(child) {
200
- child.skip();
201
- },
202
-
203
- CallExpression(child) {
204
- if (!child.get("callee").isSuper()) return;
205
- allSuperCalls.push(child);
206
- }
207
-
209
+ thisEnvFn.traverse(getSuperCallsVisitor, {
210
+ allSuperCalls
208
211
  });
209
212
  const superBinding = getSuperBinding(thisEnvFn);
210
213
  allSuperCalls.forEach(superCall => {
@@ -301,20 +304,34 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
301
304
  };
302
305
  }
303
306
 
307
+ function isLogicalOp(op) {
308
+ return LOGICAL_OPERATORS.includes(op);
309
+ }
310
+
304
311
  function standardizeSuperProperty(superProp) {
305
312
  if (superProp.parentPath.isAssignmentExpression() && superProp.parentPath.node.operator !== "=") {
306
313
  const assignmentPath = superProp.parentPath;
307
314
  const op = assignmentPath.node.operator.slice(0, -1);
308
315
  const value = assignmentPath.node.right;
309
- assignmentPath.node.operator = "=";
316
+ const isLogicalAssignment = isLogicalOp(op);
310
317
 
311
318
  if (superProp.node.computed) {
312
319
  const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
313
- assignmentPath.get("left").replaceWith(memberExpression(superProp.node.object, assignmentExpression("=", tmp, superProp.node.property), true));
314
- 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));
315
324
  } else {
316
- assignmentPath.get("left").replaceWith(memberExpression(superProp.node.object, superProp.node.property));
317
- assignmentPath.get("right").replaceWith(binaryExpression(op, memberExpression(superProp.node.object, identifier(superProp.node.property.name)), value));
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));
333
+ } else {
334
+ assignmentPath.node.operator = "=";
318
335
  }
319
336
 
320
337
  return [assignmentPath.get("left"), assignmentPath.get("right").get("left")];
@@ -322,7 +339,7 @@ function standardizeSuperProperty(superProp) {
322
339
  const updateExpr = superProp.parentPath;
323
340
  const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
324
341
  const computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null;
325
- 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)))];
326
343
 
327
344
  if (!superProp.parentPath.node.prefix) {
328
345
  parts.push(identifier(tmp.name));
@@ -335,33 +352,39 @@ function standardizeSuperProperty(superProp) {
335
352
  }
336
353
 
337
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
+ }
338
363
  }
339
364
 
340
365
  function hasSuperClass(thisEnvFn) {
341
366
  return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
342
367
  }
343
368
 
369
+ const assignSuperThisVisitor = (0, _visitors.merge)([{
370
+ CallExpression(child, {
371
+ supers,
372
+ thisBinding
373
+ }) {
374
+ if (!child.get("callee").isSuper()) return;
375
+ if (supers.has(child.node)) return;
376
+ supers.add(child.node);
377
+ child.replaceWithMultiple([child.node, assignmentExpression("=", identifier(thisBinding), identifier("this"))]);
378
+ }
379
+
380
+ }, _helperEnvironmentVisitor.default]);
381
+
344
382
  function getThisBinding(thisEnvFn, inConstructor) {
345
383
  return getBinding(thisEnvFn, "this", thisBinding => {
346
384
  if (!inConstructor || !hasSuperClass(thisEnvFn)) return thisExpression();
347
- const supers = new WeakSet();
348
- thisEnvFn.traverse({
349
- Function(child) {
350
- if (child.isArrowFunctionExpression()) return;
351
- child.skip();
352
- },
353
-
354
- ClassProperty(child) {
355
- child.skip();
356
- },
357
-
358
- CallExpression(child) {
359
- if (!child.get("callee").isSuper()) return;
360
- if (supers.has(child.node)) return;
361
- supers.add(child.node);
362
- child.replaceWithMultiple([child.node, assignmentExpression("=", identifier(thisBinding), identifier("this"))]);
363
- }
364
-
385
+ thisEnvFn.traverse(assignSuperThisVisitor, {
386
+ supers: new WeakSet(),
387
+ thisBinding
365
388
  });
366
389
  });
367
390
  }
@@ -414,76 +437,89 @@ function getBinding(thisEnvFn, key, init) {
414
437
  return data;
415
438
  }
416
439
 
440
+ const getScopeInformationVisitor = (0, _visitors.merge)([{
441
+ ThisExpression(child, {
442
+ thisPaths
443
+ }) {
444
+ thisPaths.push(child);
445
+ },
446
+
447
+ JSXIdentifier(child, {
448
+ thisPaths
449
+ }) {
450
+ if (child.node.name !== "this") return;
451
+
452
+ if (!child.parentPath.isJSXMemberExpression({
453
+ object: child.node
454
+ }) && !child.parentPath.isJSXOpeningElement({
455
+ name: child.node
456
+ })) {
457
+ return;
458
+ }
459
+
460
+ thisPaths.push(child);
461
+ },
462
+
463
+ CallExpression(child, {
464
+ superCalls
465
+ }) {
466
+ if (child.get("callee").isSuper()) superCalls.push(child);
467
+ },
468
+
469
+ MemberExpression(child, {
470
+ superProps
471
+ }) {
472
+ if (child.get("object").isSuper()) superProps.push(child);
473
+ },
474
+
475
+ Identifier(child, {
476
+ argumentsPaths
477
+ }) {
478
+ if (!child.isReferencedIdentifier({
479
+ name: "arguments"
480
+ })) return;
481
+ let curr = child.scope;
482
+
483
+ do {
484
+ if (curr.hasOwnBinding("arguments")) {
485
+ curr.rename("arguments");
486
+ return;
487
+ }
488
+
489
+ if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
490
+ break;
491
+ }
492
+ } while (curr = curr.parent);
493
+
494
+ argumentsPaths.push(child);
495
+ },
496
+
497
+ MetaProperty(child, {
498
+ newTargetPaths
499
+ }) {
500
+ if (!child.get("meta").isIdentifier({
501
+ name: "new"
502
+ })) return;
503
+ if (!child.get("property").isIdentifier({
504
+ name: "target"
505
+ })) return;
506
+ newTargetPaths.push(child);
507
+ }
508
+
509
+ }, _helperEnvironmentVisitor.default]);
510
+
417
511
  function getScopeInformation(fnPath) {
418
512
  const thisPaths = [];
419
513
  const argumentsPaths = [];
420
514
  const newTargetPaths = [];
421
515
  const superProps = [];
422
516
  const superCalls = [];
423
- fnPath.traverse({
424
- ClassProperty(child) {
425
- child.skip();
426
- },
427
-
428
- Function(child) {
429
- if (child.isArrowFunctionExpression()) return;
430
- child.skip();
431
- },
432
-
433
- ThisExpression(child) {
434
- thisPaths.push(child);
435
- },
436
-
437
- JSXIdentifier(child) {
438
- if (child.node.name !== "this") return;
439
-
440
- if (!child.parentPath.isJSXMemberExpression({
441
- object: child.node
442
- }) && !child.parentPath.isJSXOpeningElement({
443
- name: child.node
444
- })) {
445
- return;
446
- }
447
-
448
- thisPaths.push(child);
449
- },
450
-
451
- CallExpression(child) {
452
- if (child.get("callee").isSuper()) superCalls.push(child);
453
- },
454
-
455
- MemberExpression(child) {
456
- if (child.get("object").isSuper()) superProps.push(child);
457
- },
458
-
459
- ReferencedIdentifier(child) {
460
- if (child.node.name !== "arguments") return;
461
- let curr = child.scope;
462
-
463
- do {
464
- if (curr.hasOwnBinding("arguments")) {
465
- curr.rename("arguments");
466
- return;
467
- }
468
-
469
- if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
470
- break;
471
- }
472
- } while (curr = curr.parent);
473
-
474
- argumentsPaths.push(child);
475
- },
476
-
477
- MetaProperty(child) {
478
- if (!child.get("meta").isIdentifier({
479
- name: "new"
480
- })) return;
481
- if (!child.get("property").isIdentifier({
482
- name: "target"
483
- })) return;
484
- newTargetPaths.push(child);
485
- }
486
-
517
+ fnPath.traverse(getScopeInformationVisitor, {
518
+ thisPaths,
519
+ argumentsPaths,
520
+ newTargetPaths,
521
+ superProps,
522
+ superCalls
487
523
  });
488
524
  return {
489
525
  thisPaths,
@@ -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);
@@ -575,6 +575,7 @@ class Scope {
575
575
  this.registerBinding(path.node.kind, declar);
576
576
  }
577
577
  } else if (path.isClassDeclaration()) {
578
+ if (path.node.declare) return;
578
579
  this.registerBinding("let", path);
579
580
  } else if (path.isImportDeclaration()) {
580
581
  const specifiers = path.get("specifiers");
@@ -940,6 +941,8 @@ class Scope {
940
941
  if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {} else {
941
942
  return binding;
942
943
  }
944
+ } else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
945
+ break;
943
946
  }
944
947
 
945
948
  previousPath = scope.path;
@@ -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.3",
3
+ "version": "7.16.10",
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,18 +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.0",
21
- "@babel/helper-function-name": "^7.16.0",
22
- "@babel/helper-hoist-variables": "^7.16.0",
23
- "@babel/helper-split-export-declaration": "^7.16.0",
24
- "@babel/parser": "^7.16.3",
25
- "@babel/types": "^7.16.0",
19
+ "@babel/code-frame": "^7.16.7",
20
+ "@babel/generator": "^7.16.8",
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.16.10",
26
+ "@babel/types": "^7.16.8",
26
27
  "debug": "^4.1.0",
27
28
  "globals": "^11.1.0"
28
29
  },
29
30
  "devDependencies": {
30
- "@babel/helper-plugin-test-runner": "^7.16.0"
31
+ "@babel/helper-plugin-test-runner": "^7.16.7"
31
32
  },
32
33
  "engines": {
33
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}"]>;`;