@babel/traverse 7.16.3 → 7.16.5

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.

@@ -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,
@@ -144,6 +148,16 @@ function arrowFunctionToExpression({
144
148
  }
145
149
  }
146
150
 
151
+ const getSuperCallsVisitor = (0, _visitors.merge)([{
152
+ CallExpression(child, {
153
+ allSuperCalls
154
+ }) {
155
+ if (!child.get("callee").isSuper()) return;
156
+ allSuperCalls.push(child);
157
+ }
158
+
159
+ }, _helperEnvironmentVisitor.default]);
160
+
147
161
  function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow = true) {
148
162
  let arrowParent;
149
163
  let thisEnvFn = fnPath.findParent(p => {
@@ -190,21 +204,8 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
190
204
  }
191
205
 
192
206
  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
-
207
+ thisEnvFn.traverse(getSuperCallsVisitor, {
208
+ allSuperCalls
208
209
  });
209
210
  const superBinding = getSuperBinding(thisEnvFn);
210
211
  allSuperCalls.forEach(superCall => {
@@ -341,27 +342,25 @@ function hasSuperClass(thisEnvFn) {
341
342
  return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
342
343
  }
343
344
 
345
+ const assignSuperThisVisitor = (0, _visitors.merge)([{
346
+ CallExpression(child, {
347
+ supers,
348
+ thisBinding
349
+ }) {
350
+ if (!child.get("callee").isSuper()) return;
351
+ if (supers.has(child.node)) return;
352
+ supers.add(child.node);
353
+ child.replaceWithMultiple([child.node, assignmentExpression("=", identifier(thisBinding), identifier("this"))]);
354
+ }
355
+
356
+ }, _helperEnvironmentVisitor.default]);
357
+
344
358
  function getThisBinding(thisEnvFn, inConstructor) {
345
359
  return getBinding(thisEnvFn, "this", thisBinding => {
346
360
  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
-
361
+ thisEnvFn.traverse(assignSuperThisVisitor, {
362
+ supers: new WeakSet(),
363
+ thisBinding
365
364
  });
366
365
  });
367
366
  }
@@ -414,76 +413,89 @@ function getBinding(thisEnvFn, key, init) {
414
413
  return data;
415
414
  }
416
415
 
416
+ const getScopeInformationVisitor = (0, _visitors.merge)([{
417
+ ThisExpression(child, {
418
+ thisPaths
419
+ }) {
420
+ thisPaths.push(child);
421
+ },
422
+
423
+ JSXIdentifier(child, {
424
+ thisPaths
425
+ }) {
426
+ if (child.node.name !== "this") return;
427
+
428
+ if (!child.parentPath.isJSXMemberExpression({
429
+ object: child.node
430
+ }) && !child.parentPath.isJSXOpeningElement({
431
+ name: child.node
432
+ })) {
433
+ return;
434
+ }
435
+
436
+ thisPaths.push(child);
437
+ },
438
+
439
+ CallExpression(child, {
440
+ superCalls
441
+ }) {
442
+ if (child.get("callee").isSuper()) superCalls.push(child);
443
+ },
444
+
445
+ MemberExpression(child, {
446
+ superProps
447
+ }) {
448
+ if (child.get("object").isSuper()) superProps.push(child);
449
+ },
450
+
451
+ Identifier(child, {
452
+ argumentsPaths
453
+ }) {
454
+ if (!child.isReferencedIdentifier({
455
+ name: "arguments"
456
+ })) return;
457
+ let curr = child.scope;
458
+
459
+ do {
460
+ if (curr.hasOwnBinding("arguments")) {
461
+ curr.rename("arguments");
462
+ return;
463
+ }
464
+
465
+ if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
466
+ break;
467
+ }
468
+ } while (curr = curr.parent);
469
+
470
+ argumentsPaths.push(child);
471
+ },
472
+
473
+ MetaProperty(child, {
474
+ newTargetPaths
475
+ }) {
476
+ if (!child.get("meta").isIdentifier({
477
+ name: "new"
478
+ })) return;
479
+ if (!child.get("property").isIdentifier({
480
+ name: "target"
481
+ })) return;
482
+ newTargetPaths.push(child);
483
+ }
484
+
485
+ }, _helperEnvironmentVisitor.default]);
486
+
417
487
  function getScopeInformation(fnPath) {
418
488
  const thisPaths = [];
419
489
  const argumentsPaths = [];
420
490
  const newTargetPaths = [];
421
491
  const superProps = [];
422
492
  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
-
493
+ fnPath.traverse(getScopeInformationVisitor, {
494
+ thisPaths,
495
+ argumentsPaths,
496
+ newTargetPaths,
497
+ superProps,
498
+ superCalls
487
499
  });
488
500
  return {
489
501
  thisPaths,
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babel/traverse",
3
- "version": "7.16.3",
3
+ "version": "7.16.5",
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,17 +17,18 @@
17
17
  "main": "./lib/index.js",
18
18
  "dependencies": {
19
19
  "@babel/code-frame": "^7.16.0",
20
- "@babel/generator": "^7.16.0",
20
+ "@babel/generator": "^7.16.5",
21
+ "@babel/helper-environment-visitor": "^7.16.5",
21
22
  "@babel/helper-function-name": "^7.16.0",
22
23
  "@babel/helper-hoist-variables": "^7.16.0",
23
24
  "@babel/helper-split-export-declaration": "^7.16.0",
24
- "@babel/parser": "^7.16.3",
25
+ "@babel/parser": "^7.16.5",
25
26
  "@babel/types": "^7.16.0",
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.5"
31
32
  },
32
33
  "engines": {
33
34
  "node": ">=6.9.0"