@babel/traverse 7.12.5 → 7.12.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/context.js CHANGED
@@ -90,7 +90,7 @@ class TraversalContext {
90
90
  visitQueue(queue) {
91
91
  this.queue = queue;
92
92
  this.priorityQueue = [];
93
- const visited = [];
93
+ const visited = new WeakSet();
94
94
  let stop = false;
95
95
 
96
96
  for (const path of queue) {
@@ -106,8 +106,11 @@ class TraversalContext {
106
106
  this.trap = true;
107
107
  }
108
108
 
109
- if (visited.indexOf(path.node) >= 0) continue;
110
- visited.push(path.node);
109
+ const {
110
+ node
111
+ } = path;
112
+ if (visited.has(node)) continue;
113
+ if (node) visited.add(node);
111
114
 
112
115
  if (path.visit()) {
113
116
  stop = true;
@@ -141,7 +141,7 @@ function getSibling(key) {
141
141
  container: this.container,
142
142
  listKey: this.listKey,
143
143
  key: key
144
- });
144
+ }).setContext(this.context);
145
145
  }
146
146
 
147
147
  function getPrevSibling() {
@@ -178,7 +178,7 @@ function getAllPrevSiblings() {
178
178
  return siblings;
179
179
  }
180
180
 
181
- function get(key, context) {
181
+ function get(key, context = true) {
182
182
  if (context === true) context = this.context;
183
183
  const parts = key.split(".");
184
184
 
package/lib/path/index.js CHANGED
@@ -92,26 +92,20 @@ class NodePath {
92
92
  }
93
93
 
94
94
  const targetNode = container[key];
95
- const paths = _cache.path.get(parent) || [];
96
95
 
97
- if (!_cache.path.has(parent)) {
98
- _cache.path.set(parent, paths);
99
- }
100
-
101
- let path;
96
+ let paths = _cache.path.get(parent);
102
97
 
103
- for (let i = 0; i < paths.length; i++) {
104
- const pathCheck = paths[i];
98
+ if (!paths) {
99
+ paths = new Map();
105
100
 
106
- if (pathCheck.node === targetNode) {
107
- path = pathCheck;
108
- break;
109
- }
101
+ _cache.path.set(parent, paths);
110
102
  }
111
103
 
104
+ let path = paths.get(targetNode);
105
+
112
106
  if (!path) {
113
107
  path = new NodePath(hub, parent);
114
- paths.push(path);
108
+ if (targetNode) paths.set(targetNode, path);
115
109
  }
116
110
 
117
111
  path.setup(parentPath, container, listKey, key);
@@ -25,9 +25,9 @@ function getTypeAnnotation() {
25
25
  return this.typeAnnotation = type;
26
26
  }
27
27
 
28
- function _getTypeAnnotation() {
29
- var _inferer;
28
+ const typeAnnotationInferringNodes = new WeakSet();
30
29
 
30
+ function _getTypeAnnotation() {
31
31
  const node = this.node;
32
32
 
33
33
  if (!node) {
@@ -53,16 +53,28 @@ function _getTypeAnnotation() {
53
53
  return node.typeAnnotation;
54
54
  }
55
55
 
56
- let inferer = inferers[node.type];
57
-
58
- if (inferer) {
59
- return inferer.call(this, node);
56
+ if (typeAnnotationInferringNodes.has(node)) {
57
+ return;
60
58
  }
61
59
 
62
- inferer = inferers[this.parentPath.type];
60
+ typeAnnotationInferringNodes.add(node);
61
+
62
+ try {
63
+ var _inferer;
64
+
65
+ let inferer = inferers[node.type];
63
66
 
64
- if ((_inferer = inferer) == null ? void 0 : _inferer.validParent) {
65
- return this.parentPath.getTypeAnnotation();
67
+ if (inferer) {
68
+ return inferer.call(this, node);
69
+ }
70
+
71
+ inferer = inferers[this.parentPath.type];
72
+
73
+ if ((_inferer = inferer) == null ? void 0 : _inferer.validParent) {
74
+ return this.parentPath.getTypeAnnotation();
75
+ }
76
+ } finally {
77
+ typeAnnotationInferringNodes.delete(node);
66
78
  }
67
79
  }
68
80
 
@@ -136,9 +136,7 @@ function updateSiblingKeys(fromIndex, incrementBy) {
136
136
 
137
137
  const paths = _cache.path.get(this.parent);
138
138
 
139
- for (let i = 0; i < paths.length; i++) {
140
- const path = paths[i];
141
-
139
+ for (const [, path] of paths) {
142
140
  if (path.key >= fromIndex) {
143
141
  path.key += incrementBy;
144
142
  }
@@ -188,7 +186,7 @@ function unshiftContainer(listKey, nodes) {
188
186
  container: this.node[listKey],
189
187
  listKey,
190
188
  key: 0
191
- });
189
+ }).setContext(this.context);
192
190
 
193
191
  return path._containerInsertBefore(nodes);
194
192
  }
@@ -205,7 +203,7 @@ function pushContainer(listKey, nodes) {
205
203
  container: container,
206
204
  listKey,
207
205
  key: container.length
208
- });
206
+ }).setContext(this.context);
209
207
 
210
208
  return path.replaceWithMultiple(nodes);
211
209
  }
@@ -12,6 +12,8 @@ exports._assertUnremoved = _assertUnremoved;
12
12
 
13
13
  var _removalHooks = require("./lib/removal-hooks");
14
14
 
15
+ var _cache = require("../cache");
16
+
15
17
  var _index = require("./index");
16
18
 
17
19
  function remove() {
@@ -60,6 +62,7 @@ function _remove() {
60
62
 
61
63
  function _markRemoved() {
62
64
  this._traverseFlags |= _index.SHOULD_SKIP | _index.REMOVED;
65
+ if (this.parent) _cache.path.get(this.parent).delete(this.node);
63
66
  this.node = null;
64
67
  }
65
68
 
@@ -16,6 +16,8 @@ var _index = _interopRequireDefault(require("../index"));
16
16
 
17
17
  var _index2 = _interopRequireDefault(require("./index"));
18
18
 
19
+ var _cache = require("../cache");
20
+
19
21
  var _parser = require("@babel/parser");
20
22
 
21
23
  var t = _interopRequireWildcard(require("@babel/types"));
@@ -55,10 +57,13 @@ const hoistVariablesVisitor = {
55
57
  };
56
58
 
57
59
  function replaceWithMultiple(nodes) {
60
+ var _pathCache$get;
61
+
58
62
  this.resync();
59
63
  nodes = this._verifyNodeList(nodes);
60
64
  t.inheritLeadingComments(nodes[0], this.node);
61
65
  t.inheritTrailingComments(nodes[nodes.length - 1], this.node);
66
+ (_pathCache$get = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get.delete(this.node);
62
67
  this.node = this.container[this.key] = null;
63
68
  const paths = this.insertAfter(nodes);
64
69
 
@@ -162,6 +167,8 @@ function replaceWith(replacement) {
162
167
  }
163
168
 
164
169
  function _replaceWith(node) {
170
+ var _pathCache$get2;
171
+
165
172
  if (!this.container) {
166
173
  throw new ReferenceError("Container is falsy");
167
174
  }
@@ -173,6 +180,7 @@ function _replaceWith(node) {
173
180
  }
174
181
 
175
182
  this.debug(`Replace with ${node == null ? void 0 : node.type}`);
183
+ (_pathCache$get2 = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get2.set(node, this).delete(this.node);
176
184
  this.node = this.container[this.key] = node;
177
185
  }
178
186
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babel/traverse",
3
- "version": "7.12.5",
3
+ "version": "7.12.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": "Sebastian McKenzie <sebmck@gmail.com>",
6
6
  "homepage": "https://babeljs.io/",
@@ -16,11 +16,11 @@
16
16
  "main": "lib/index.js",
17
17
  "dependencies": {
18
18
  "@babel/code-frame": "^7.10.4",
19
- "@babel/generator": "^7.12.5",
19
+ "@babel/generator": "^7.12.10",
20
20
  "@babel/helper-function-name": "^7.10.4",
21
21
  "@babel/helper-split-export-declaration": "^7.11.0",
22
- "@babel/parser": "^7.12.5",
23
- "@babel/types": "^7.12.5",
22
+ "@babel/parser": "^7.12.10",
23
+ "@babel/types": "^7.12.10",
24
24
  "debug": "^4.1.0",
25
25
  "globals": "^11.1.0",
26
26
  "lodash": "^4.17.19"