@babel/traverse 8.0.0-alpha.0 → 8.0.0-alpha.2

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.
package/lib/cache.js CHANGED
@@ -1,14 +1,28 @@
1
- export let path = new WeakMap();
1
+ let pathsCache = new WeakMap();
2
+ export { pathsCache as path };
2
3
  export let scope = new WeakMap();
3
4
  export function clear() {
4
5
  clearPath();
5
6
  clearScope();
6
7
  }
7
8
  export function clearPath() {
8
- path = new WeakMap();
9
+ pathsCache = new WeakMap();
9
10
  }
10
11
  export function clearScope() {
11
12
  scope = new WeakMap();
12
13
  }
14
+ const nullHub = Object.freeze({});
15
+ export function getCachedPaths(hub, parent) {
16
+ ;
17
+ return pathsCache.get(hub ?? nullHub)?.get(parent);
18
+ }
19
+ export function getOrCreateCachedPaths(hub, parent) {
20
+ ;
21
+ let parents = pathsCache.get(hub ?? nullHub);
22
+ if (!parents) pathsCache.set(hub ?? nullHub, parents = new WeakMap());
23
+ let paths = parents.get(parent);
24
+ if (!paths) parents.set(parent, paths = new Map());
25
+ return paths;
26
+ }
13
27
 
14
28
  //# sourceMappingURL=cache.js.map
package/lib/cache.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["path","WeakMap","scope","clear","clearPath","clearScope"],"sources":["../src/cache.ts"],"sourcesContent":["import type { Node } from \"@babel/types\";\nimport type NodePath from \"./path\";\nimport type Scope from \"./scope\";\n\nexport let path: WeakMap<Node, Map<Node, NodePath>> = new WeakMap();\nexport let scope: WeakMap<Node, Scope> = new WeakMap();\n\nexport function clear() {\n clearPath();\n clearScope();\n}\n\nexport function clearPath() {\n path = new WeakMap();\n}\n\nexport function clearScope() {\n scope = new WeakMap();\n}\n"],"mappings":"AAIA,OAAO,IAAIA,IAAwC,GAAG,IAAIC,OAAO,CAAC,CAAC;AACnE,OAAO,IAAIC,KAA2B,GAAG,IAAID,OAAO,CAAC,CAAC;AAEtD,OAAO,SAASE,KAAKA,CAAA,EAAG;EACtBC,SAAS,CAAC,CAAC;EACXC,UAAU,CAAC,CAAC;AACd;AAEA,OAAO,SAASD,SAASA,CAAA,EAAG;EAC1BJ,IAAI,GAAG,IAAIC,OAAO,CAAC,CAAC;AACtB;AAEA,OAAO,SAASI,UAAUA,CAAA,EAAG;EAC3BH,KAAK,GAAG,IAAID,OAAO,CAAC,CAAC;AACvB"}
1
+ {"version":3,"names":["pathsCache","WeakMap","path","scope","clear","clearPath","clearScope","nullHub","Object","freeze","getCachedPaths","hub","parent","get","getOrCreateCachedPaths","parents","set","paths","Map"],"sources":["../src/cache.ts"],"sourcesContent":["import type { Node } from \"@babel/types\";\nimport type NodePath from \"./path\";\nimport type Scope from \"./scope\";\nimport type { HubInterface } from \"./hub\";\n\nlet pathsCache: WeakMap<\n HubInterface | typeof nullHub,\n WeakMap<Node, Map<Node, NodePath>>\n> = new WeakMap();\nexport { pathsCache as path };\nexport let scope: WeakMap<Node, Scope> = new WeakMap();\n\nexport function clear() {\n clearPath();\n clearScope();\n}\n\nexport function clearPath() {\n pathsCache = new WeakMap();\n}\n\nexport function clearScope() {\n scope = new WeakMap();\n}\n\n// NodePath#hub can be null, but it's not a valid weakmap key because it\n// cannot be collected by GC. Use an object, knowing tht it will not be\n// collected anyway. It's not a memory leak because pathsCache.get(nullHub)\n// is itself a weakmap, so its entries can still be collected.\nconst nullHub = Object.freeze({} as const);\n\nexport function getCachedPaths(hub: HubInterface | null, parent: Node) {\n if (!process.env.BABEL_8_BREAKING) {\n // Only use Hub as part of the cache key in Babel 8, because it is a\n // breaking change (it causes incompatibilities with older `@babel/core`\n // versions: see https://github.com/babel/babel/pull/15759)\n hub = null;\n }\n return pathsCache.get(hub ?? nullHub)?.get(parent);\n}\n\nexport function getOrCreateCachedPaths(hub: HubInterface | null, parent: Node) {\n if (!process.env.BABEL_8_BREAKING) {\n hub = null;\n }\n\n let parents = pathsCache.get(hub ?? nullHub);\n if (!parents) pathsCache.set(hub ?? nullHub, (parents = new WeakMap()));\n\n let paths = parents.get(parent);\n if (!paths) parents.set(parent, (paths = new Map()));\n\n return paths;\n}\n"],"mappings":"AAKA,IAAIA,UAGH,GAAG,IAAIC,OAAO,CAAC,CAAC;AACjB,SAASD,UAAU,IAAIE,IAAI;AAC3B,OAAO,IAAIC,KAA2B,GAAG,IAAIF,OAAO,CAAC,CAAC;AAEtD,OAAO,SAASG,KAAKA,CAAA,EAAG;EACtBC,SAAS,CAAC,CAAC;EACXC,UAAU,CAAC,CAAC;AACd;AAEA,OAAO,SAASD,SAASA,CAAA,EAAG;EAC1BL,UAAU,GAAG,IAAIC,OAAO,CAAC,CAAC;AAC5B;AAEA,OAAO,SAASK,UAAUA,CAAA,EAAG;EAC3BH,KAAK,GAAG,IAAIF,OAAO,CAAC,CAAC;AACvB;AAMA,MAAMM,OAAO,GAAGC,MAAM,CAACC,MAAM,CAAC,CAAC,CAAU,CAAC;AAE1C,OAAO,SAASC,cAAcA,CAACC,GAAwB,EAAEC,MAAY,EAAE;EAAA;EAOrE,OAAOZ,UAAU,CAACa,GAAG,CAACF,GAAG,IAAIJ,OAAO,CAAC,EAAEM,GAAG,CAACD,MAAM,CAAC;AACpD;AAEA,OAAO,SAASE,sBAAsBA,CAACH,GAAwB,EAAEC,MAAY,EAAE;EAAA;EAK7E,IAAIG,OAAO,GAAGf,UAAU,CAACa,GAAG,CAACF,GAAG,IAAIJ,OAAO,CAAC;EAC5C,IAAI,CAACQ,OAAO,EAAEf,UAAU,CAACgB,GAAG,CAACL,GAAG,IAAIJ,OAAO,EAAGQ,OAAO,GAAG,IAAId,OAAO,CAAC,CAAE,CAAC;EAEvE,IAAIgB,KAAK,GAAGF,OAAO,CAACF,GAAG,CAACD,MAAM,CAAC;EAC/B,IAAI,CAACK,KAAK,EAAEF,OAAO,CAACC,GAAG,CAACJ,MAAM,EAAGK,KAAK,GAAG,IAAIC,GAAG,CAAC,CAAE,CAAC;EAEpD,OAAOD,KAAK;AACd"}
package/lib/index.js CHANGED
@@ -263,26 +263,39 @@ var visitors = /*#__PURE__*/Object.freeze({
263
263
  merge: merge
264
264
  });
265
265
 
266
- let path = new WeakMap();
266
+ let pathsCache = new WeakMap();
267
267
  let scope = new WeakMap();
268
268
  function clear() {
269
269
  clearPath();
270
270
  clearScope();
271
271
  }
272
272
  function clearPath() {
273
- path = new WeakMap();
273
+ pathsCache = new WeakMap();
274
274
  }
275
275
  function clearScope() {
276
276
  scope = new WeakMap();
277
277
  }
278
+ const nullHub = Object.freeze({});
279
+ function getCachedPaths(hub, parent) {
280
+ return pathsCache.get(hub ?? nullHub)?.get(parent);
281
+ }
282
+ function getOrCreateCachedPaths(hub, parent) {
283
+ let parents = pathsCache.get(hub ?? nullHub);
284
+ if (!parents) pathsCache.set(hub ?? nullHub, parents = new WeakMap());
285
+ let paths = parents.get(parent);
286
+ if (!paths) parents.set(parent, paths = new Map());
287
+ return paths;
288
+ }
278
289
 
279
290
  var cache = /*#__PURE__*/Object.freeze({
280
291
  __proto__: null,
281
- get path () { return path; },
292
+ get path () { return pathsCache; },
282
293
  get scope () { return scope; },
283
294
  clear: clear,
284
295
  clearPath: clearPath,
285
- clearScope: clearScope
296
+ clearScope: clearScope,
297
+ getCachedPaths: getCachedPaths,
298
+ getOrCreateCachedPaths: getOrCreateCachedPaths
286
299
  });
287
300
 
288
301
  const renameVisitor = {
@@ -1992,7 +2005,7 @@ function replaceWithMultiple(nodes) {
1992
2005
  nodes = this._verifyNodeList(nodes);
1993
2006
  inheritLeadingComments(nodes[0], this.node);
1994
2007
  inheritTrailingComments(nodes[nodes.length - 1], this.node);
1995
- path.get(this.parent)?.delete(this.node);
2008
+ getCachedPaths(this.hub, this.parent)?.delete(this.node);
1996
2009
  this.node = this.container[this.key] = null;
1997
2010
  const paths = this.insertAfter(nodes);
1998
2011
  if (this.node) {
@@ -2079,7 +2092,7 @@ function _replaceWith(node) {
2079
2092
  validate$1(this.parent, this.key, node);
2080
2093
  }
2081
2094
  this.debug(`Replace with ${node?.type}`);
2082
- path.get(this.parent)?.set(node, this).delete(this.node);
2095
+ getCachedPaths(this.hub, this.parent)?.set(node, this).delete(this.node);
2083
2096
  this.node = this.container[this.key] = node;
2084
2097
  }
2085
2098
  function replaceExpressionWithStatements(nodes) {
@@ -3611,7 +3624,9 @@ function _remove() {
3611
3624
  }
3612
3625
  function _markRemoved() {
3613
3626
  this._traverseFlags |= SHOULD_SKIP | REMOVED;
3614
- if (this.parent) path.get(this.parent).delete(this.node);
3627
+ if (this.parent) {
3628
+ getCachedPaths(this.hub, this.parent).delete(this.node);
3629
+ }
3615
3630
  this.node = null;
3616
3631
  }
3617
3632
  function _assertUnremoved() {
@@ -3932,7 +3947,7 @@ function insertAfter(nodes_) {
3932
3947
  }
3933
3948
  function updateSiblingKeys(fromIndex, incrementBy) {
3934
3949
  if (!this.parent) return;
3935
- const paths = path.get(this.parent);
3950
+ const paths = getCachedPaths(this.hub, this.parent) || [];
3936
3951
  for (const [, path] of paths) {
3937
3952
  if (typeof path.key === "number" && path.key >= fromIndex) {
3938
3953
  path.key += incrementBy;
@@ -4521,12 +4536,6 @@ function isForAwaitStatement() {
4521
4536
  await: true
4522
4537
  });
4523
4538
  }
4524
- function isExistentialTypeParam() {
4525
- throw new Error("`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.");
4526
- }
4527
- function isNumericLiteralTypeAnnotation() {
4528
- throw new Error("`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.");
4529
- }
4530
4539
 
4531
4540
  var NodePath_virtual_types_validator = /*#__PURE__*/Object.freeze({
4532
4541
  __proto__: null,
@@ -4545,9 +4554,7 @@ var NodePath_virtual_types_validator = /*#__PURE__*/Object.freeze({
4545
4554
  isFlow: isFlow,
4546
4555
  isRestProperty: isRestProperty,
4547
4556
  isSpreadProperty: isSpreadProperty,
4548
- isForAwaitStatement: isForAwaitStatement,
4549
- isExistentialTypeParam: isExistentialTypeParam,
4550
- isNumericLiteralTypeAnnotation: isNumericLiteralTypeAnnotation
4557
+ isForAwaitStatement: isForAwaitStatement
4551
4558
  });
4552
4559
 
4553
4560
  const {
@@ -4591,18 +4598,14 @@ class NodePath {
4591
4598
  throw new Error("To get a node path the parent needs to exist");
4592
4599
  }
4593
4600
  const targetNode = container[key];
4594
- let paths = path.get(parent);
4595
- if (!paths) {
4596
- paths = new Map();
4597
- path.set(parent, paths);
4598
- }
4599
- let path$1 = paths.get(targetNode);
4600
- if (!path$1) {
4601
- path$1 = new NodePath(hub, parent);
4602
- if (targetNode) paths.set(targetNode, path$1);
4601
+ const paths = getOrCreateCachedPaths(hub, parent);
4602
+ let path = paths.get(targetNode);
4603
+ if (!path) {
4604
+ path = new NodePath(hub, parent);
4605
+ if (targetNode) paths.set(targetNode, path);
4603
4606
  }
4604
- path$1.setup(parentPath, container, listKey, key);
4605
- return path$1;
4607
+ path.setup(parentPath, container, listKey, key);
4608
+ return path;
4606
4609
  }
4607
4610
  getScope(scope) {
4608
4611
  return this.isScope() ? new Scope(this) : scope;
@@ -4820,12 +4823,16 @@ class TraversalContext {
4820
4823
  const {
4821
4824
  VISITOR_KEYS: VISITOR_KEYS$1
4822
4825
  } = _t;
4823
- function traverseNode(node, opts, scope, state, path, skipKeys) {
4826
+ function traverseNode(node, opts, scope, state, path, skipKeys, visitSelf) {
4824
4827
  const keys = VISITOR_KEYS$1[node.type];
4825
4828
  if (!keys) return false;
4826
4829
  const context = new TraversalContext(scope, opts, state, path);
4830
+ if (visitSelf) {
4831
+ if (skipKeys?.[path.parentKey]) return false;
4832
+ return context.visitQueue([path]);
4833
+ }
4827
4834
  for (const key of keys) {
4828
- if (skipKeys && skipKeys[key]) continue;
4835
+ if (skipKeys?.[key]) continue;
4829
4836
  if (context.visit(node, key)) {
4830
4837
  return true;
4831
4838
  }
@@ -4849,18 +4856,21 @@ const {
4849
4856
  removeProperties,
4850
4857
  traverseFast
4851
4858
  } = _t;
4852
- function traverse(parent, opts = {}, scope, state, parentPath) {
4859
+ function traverse(parent, opts = {}, scope, state, parentPath, visitSelf) {
4853
4860
  if (!parent) return;
4854
4861
  if (!opts.noScope && !scope) {
4855
4862
  if (parent.type !== "Program" && parent.type !== "File") {
4856
4863
  throw new Error("You must pass a scope and parentPath unless traversing a Program/File. " + `Instead of that you tried to traverse a ${parent.type} node without ` + "passing scope and parentPath.");
4857
4864
  }
4858
4865
  }
4866
+ if (!parentPath && visitSelf) {
4867
+ throw new Error("visitSelf can only be used when providing a NodePath.");
4868
+ }
4859
4869
  if (!VISITOR_KEYS[parent.type]) {
4860
4870
  return;
4861
4871
  }
4862
4872
  explode(opts);
4863
- traverseNode(parent, opts, scope, state, parentPath);
4873
+ traverseNode(parent, opts, scope, state, parentPath, null, visitSelf);
4864
4874
  }
4865
4875
  traverse.visitors = visitors;
4866
4876
  traverse.verify = verify;
@@ -4874,7 +4884,6 @@ traverse.node = function (node, opts, scope, state, path, skipKeys) {
4874
4884
  };
4875
4885
  traverse.clearNode = function (node, opts) {
4876
4886
  removeProperties(node, opts);
4877
- path.delete(node);
4878
4887
  };
4879
4888
  traverse.removeProperties = function (tree, opts) {
4880
4889
  traverseFast(tree, traverse.clearNode, opts);