@babel/traverse 7.21.4-esm.1 → 7.21.4-esm.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.

Files changed (58) hide show
  1. package/lib/cache.js +18 -7
  2. package/lib/cache.js.map +1 -1
  3. package/lib/context.js +11 -4
  4. package/lib/context.js.map +1 -1
  5. package/lib/hub.js +8 -1
  6. package/lib/hub.js.map +1 -1
  7. package/lib/index.js +36 -11
  8. package/lib/index.js.map +1 -1
  9. package/lib/path/ancestry.js +26 -11
  10. package/lib/path/ancestry.js.map +1 -1
  11. package/lib/path/comments.js +12 -4
  12. package/lib/path/comments.js.map +1 -1
  13. package/lib/path/context.js +49 -25
  14. package/lib/path/context.js.map +1 -1
  15. package/lib/path/conversion.js +26 -16
  16. package/lib/path/conversion.js.map +1 -1
  17. package/lib/path/evaluation.js +9 -2
  18. package/lib/path/evaluation.js.map +1 -1
  19. package/lib/path/family.js +33 -19
  20. package/lib/path/family.js.map +1 -1
  21. package/lib/path/index.js +40 -30
  22. package/lib/path/index.js.map +1 -1
  23. package/lib/path/inference/index.js +19 -8
  24. package/lib/path/inference/index.js.map +1 -1
  25. package/lib/path/inference/inferer-reference.js +11 -5
  26. package/lib/path/inference/inferer-reference.js.map +1 -1
  27. package/lib/path/inference/inferers.js +65 -30
  28. package/lib/path/inference/inferers.js.map +1 -1
  29. package/lib/path/inference/util.js +8 -2
  30. package/lib/path/inference/util.js.map +1 -1
  31. package/lib/path/introspection.js +45 -20
  32. package/lib/path/introspection.js.map +1 -1
  33. package/lib/path/lib/hoister.js +10 -3
  34. package/lib/path/lib/hoister.js.map +1 -1
  35. package/lib/path/lib/removal-hooks.js +8 -1
  36. package/lib/path/lib/removal-hooks.js.map +1 -1
  37. package/lib/path/lib/virtual-types-validator.js +42 -19
  38. package/lib/path/lib/virtual-types-validator.js.map +1 -1
  39. package/lib/path/lib/virtual-types.js +42 -18
  40. package/lib/path/lib/virtual-types.js.map +1 -1
  41. package/lib/path/modification.js +34 -19
  42. package/lib/path/modification.js.map +1 -1
  43. package/lib/path/removal.js +23 -12
  44. package/lib/path/removal.js.map +1 -1
  45. package/lib/path/replacement.js +33 -22
  46. package/lib/path/replacement.js.map +1 -1
  47. package/lib/scope/binding.js +8 -1
  48. package/lib/scope/binding.js.map +1 -1
  49. package/lib/scope/index.js +20 -13
  50. package/lib/scope/index.js.map +1 -1
  51. package/lib/scope/lib/renamer.js +16 -9
  52. package/lib/scope/lib/renamer.js.map +1 -1
  53. package/lib/traverse-node.js +10 -4
  54. package/lib/traverse-node.js.map +1 -1
  55. package/lib/visitors.js +13 -5
  56. package/lib/visitors.js.map +1 -1
  57. package/package.json +10 -10
  58. package/lib/package.json +0 -1
package/lib/cache.js CHANGED
@@ -1,14 +1,25 @@
1
- export let path = new WeakMap();
2
- export let scope = new WeakMap();
3
- export function clear() {
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.clear = clear;
7
+ exports.clearPath = clearPath;
8
+ exports.clearScope = clearScope;
9
+ exports.scope = exports.path = void 0;
10
+ let path = new WeakMap();
11
+ exports.path = path;
12
+ let scope = new WeakMap();
13
+ exports.scope = scope;
14
+ function clear() {
4
15
  clearPath();
5
16
  clearScope();
6
17
  }
7
- export function clearPath() {
8
- path = new WeakMap();
18
+ function clearPath() {
19
+ exports.path = path = new WeakMap();
9
20
  }
10
- export function clearScope() {
11
- scope = new WeakMap();
21
+ function clearScope() {
22
+ exports.scope = scope = new WeakMap();
12
23
  }
13
24
 
14
25
  //# 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":["export let path = new WeakMap();\nexport let 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":"AAAA,OAAO,IAAIA,IAAI,GAAG,IAAIC,OAAO,EAAE;AAC/B,OAAO,IAAIC,KAAK,GAAG,IAAID,OAAO,EAAE;AAEhC,OAAO,SAASE,KAAKA,CAAA,EAAG;EACtBC,SAAS,EAAE;EACXC,UAAU,EAAE;AACd;AAEA,OAAO,SAASD,SAASA,CAAA,EAAG;EAC1BJ,IAAI,GAAG,IAAIC,OAAO,EAAE;AACtB;AAEA,OAAO,SAASI,UAAUA,CAAA,EAAG;EAC3BH,KAAK,GAAG,IAAID,OAAO,EAAE;AACvB"}
1
+ {"version":3,"names":["path","WeakMap","exports","scope","clear","clearPath","clearScope"],"sources":["../src/cache.ts"],"sourcesContent":["export let path = new WeakMap();\nexport let 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":";;;;;;;;;AAAO,IAAIA,IAAI,GAAG,IAAIC,OAAO,EAAE;AAACC,OAAA,CAAAF,IAAA,GAAAA,IAAA;AACzB,IAAIG,KAAK,GAAG,IAAIF,OAAO,EAAE;AAACC,OAAA,CAAAC,KAAA,GAAAA,KAAA;AAE1B,SAASC,KAAKA,CAAA,EAAG;EACtBC,SAAS,EAAE;EACXC,UAAU,EAAE;AACd;AAEO,SAASD,SAASA,CAAA,EAAG;EAC1BH,OAAA,CAAAF,IAAA,GAAAA,IAAI,GAAG,IAAIC,OAAO,EAAE;AACtB;AAEO,SAASK,UAAUA,CAAA,EAAG;EAC3BJ,OAAA,CAAAC,KAAA,GAAAA,KAAK,GAAG,IAAIF,OAAO,EAAE;AACvB"}
package/lib/context.js CHANGED
@@ -1,9 +1,15 @@
1
- import NodePath from "./path/index.js";
2
- import * as _t from "@babel/types";
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _path = require("./path");
8
+ var _t = require("@babel/types");
3
9
  const {
4
10
  VISITOR_KEYS
5
11
  } = _t;
6
- export default class TraversalContext {
12
+ class TraversalContext {
7
13
  constructor(scope, opts, state, parentPath) {
8
14
  this.queue = null;
9
15
  this.priorityQueue = null;
@@ -26,7 +32,7 @@ export default class TraversalContext {
26
32
  return false;
27
33
  }
28
34
  create(node, container, key, listKey) {
29
- return NodePath.get({
35
+ return _path.default.get({
30
36
  parentPath: this.parentPath,
31
37
  parent: node,
32
38
  container,
@@ -104,5 +110,6 @@ export default class TraversalContext {
104
110
  }
105
111
  }
106
112
  }
113
+ exports.default = TraversalContext;
107
114
 
108
115
  //# sourceMappingURL=context.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["NodePath","_t","VISITOR_KEYS","TraversalContext","constructor","scope","opts","state","parentPath","queue","priorityQueue","shouldVisit","node","enter","exit","type","keys","length","key","create","container","listKey","get","parent","maybeQueue","path","notPriority","push","visitMultiple","visitQueue","visitSingle","visited","WeakSet","stop","resync","contexts","pushContext","has","add","visit","popContext","nodes","Array","isArray"],"sources":["../src/context.ts"],"sourcesContent":["import NodePath from \"./path\";\nimport { VISITOR_KEYS } from \"@babel/types\";\nimport type Scope from \"./scope\";\nimport type { TraverseOptions } from \".\";\nimport type * as t from \"@babel/types\";\nimport type { Visitor } from \"./types\";\n\nexport default class TraversalContext<S = unknown> {\n constructor(\n scope: Scope,\n opts: TraverseOptions,\n state: S,\n parentPath: NodePath,\n ) {\n this.parentPath = parentPath;\n this.scope = scope;\n this.state = state;\n this.opts = opts;\n }\n\n declare parentPath: NodePath;\n declare scope: Scope;\n declare state: S;\n declare opts: TraverseOptions;\n queue: Array<NodePath> | null = null;\n priorityQueue: Array<NodePath> | null = null;\n\n /**\n * This method does a simple check to determine whether or not we really need to attempt\n * visit a node. This will prevent us from constructing a NodePath.\n */\n\n shouldVisit(node: t.Node): boolean {\n const opts = this.opts as Visitor;\n if (opts.enter || opts.exit) return true;\n\n // check if we have a visitor for this node\n if (opts[node.type]) return true;\n\n // check if we're going to traverse into this node\n const keys: Array<string> | undefined = VISITOR_KEYS[node.type];\n if (!keys?.length) return false;\n\n // we need to traverse into this node so ensure that it has children to traverse into!\n for (const key of keys) {\n if (\n // @ts-expect-error key is from visitor keys\n node[key]\n ) {\n return true;\n }\n }\n\n return false;\n }\n\n create(\n node: t.Node,\n container: t.Node | t.Node[],\n key: string | number,\n listKey?: string,\n ): NodePath {\n // We don't need to `.setContext()` here, since `.visitQueue()` already\n // calls `.pushContext`.\n return NodePath.get({\n parentPath: this.parentPath,\n parent: node,\n container,\n key: key,\n listKey,\n });\n }\n\n maybeQueue(path: NodePath, notPriority?: boolean) {\n if (this.queue) {\n if (notPriority) {\n this.queue.push(path);\n } else {\n this.priorityQueue.push(path);\n }\n }\n }\n\n visitMultiple(container: t.Node[], parent: t.Node, listKey: string) {\n // nothing to traverse!\n if (container.length === 0) return false;\n\n const queue = [];\n\n // build up initial queue\n for (let key = 0; key < container.length; key++) {\n const node = container[key];\n if (node && this.shouldVisit(node)) {\n queue.push(this.create(parent, container, key, listKey));\n }\n }\n\n return this.visitQueue(queue);\n }\n\n visitSingle(node: t.Node, key: string): boolean {\n if (\n this.shouldVisit(\n // @ts-expect-error key may not index node\n node[key],\n )\n ) {\n return this.visitQueue([this.create(node, node, key)]);\n } else {\n return false;\n }\n }\n\n visitQueue(queue: Array<NodePath>): boolean {\n // set queue\n this.queue = queue;\n this.priorityQueue = [];\n\n const visited = new WeakSet();\n let stop = false;\n\n // visit the queue\n for (const path of queue) {\n path.resync();\n\n if (\n path.contexts.length === 0 ||\n path.contexts[path.contexts.length - 1] !== this\n ) {\n // The context might already have been pushed when this path was inserted and queued.\n // If we always re-pushed here, we could get duplicates and risk leaving contexts\n // on the stack after the traversal has completed, which could break things.\n path.pushContext(this);\n }\n\n // this path no longer belongs to the tree\n if (path.key === null) continue;\n\n // ensure we don't visit the same node twice\n const { node } = path;\n if (visited.has(node)) continue;\n if (node) visited.add(node);\n\n if (path.visit()) {\n stop = true;\n break;\n }\n\n if (this.priorityQueue.length) {\n stop = this.visitQueue(this.priorityQueue);\n this.priorityQueue = [];\n this.queue = queue;\n if (stop) break;\n }\n }\n\n // clear queue\n for (const path of queue) {\n path.popContext();\n }\n\n // clear queue\n this.queue = null;\n\n return stop;\n }\n\n visit(node: t.Node, key: string) {\n // @ts-expect-error key may not index node\n const nodes = node[key] as t.Node | t.Node[] | null;\n if (!nodes) return false;\n\n if (Array.isArray(nodes)) {\n return this.visitMultiple(nodes, node, key);\n } else {\n return this.visitSingle(node, key);\n }\n }\n}\n"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,iBAAQ;AAC7B,YAAAC,EAAA,MAA6B,cAAc;AAAC;EAAnCC;AAAY,IAAAD,EAAA;AAMrB,eAAe,MAAME,gBAAgB,CAAc;EACjDC,WAAWA,CACTC,KAAY,EACZC,IAAqB,EACrBC,KAAQ,EACRC,UAAoB,EACpB;IAAA,KAWFC,KAAK,GAA2B,IAAI;IAAA,KACpCC,aAAa,GAA2B,IAAI;IAX1C,IAAI,CAACF,UAAU,GAAGA,UAAU;IAC5B,IAAI,CAACH,KAAK,GAAGA,KAAK;IAClB,IAAI,CAACE,KAAK,GAAGA,KAAK;IAClB,IAAI,CAACD,IAAI,GAAGA,IAAI;EAClB;EAcAK,WAAWA,CAACC,IAAY,EAAW;IACjC,MAAMN,IAAI,GAAG,IAAI,CAACA,IAAe;IACjC,IAAIA,IAAI,CAACO,KAAK,IAAIP,IAAI,CAACQ,IAAI,EAAE,OAAO,IAAI;IAGxC,IAAIR,IAAI,CAACM,IAAI,CAACG,IAAI,CAAC,EAAE,OAAO,IAAI;IAGhC,MAAMC,IAA+B,GAAGd,YAAY,CAACU,IAAI,CAACG,IAAI,CAAC;IAC/D,IAAI,EAACC,IAAI,YAAJA,IAAI,CAAEC,MAAM,GAAE,OAAO,KAAK;IAG/B,KAAK,MAAMC,GAAG,IAAIF,IAAI,EAAE;MACtB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AAIhB,KAAK,EAAE;MACxBgB,IAAI,CAACe,UAAU,EAAE;IACnB;IAGA,IAAI,CAAC/B,KAAK,GAAG,IAAI;IAEjB,OAAOwB,IAAI;EACb;EAEAM,KAAKA,CAAC3B,IAAY,EAAEM,GAAW,EAAE;IAE/B,MAAMuB,KAAK,GAAG7B,IAAI,CAACM,GAAG,CAA6B;IACnD,IAAI,CAACuB,KAAK,EAAE,OAAO,KAAK;IAExB,IAAIC,KAAK,CAACC,OAAO,CAACF,KAAK,CAAC,EAAE;MACxB,OAAO,IAAI,CAACb,aAAa,CAACa,KAAK,EAAE7B,IAAI,EAAEM,GAAG,CAAC;IAC7C,CAAC,MAAM;MACL,OAAO,IAAI,CAACY,WAAW,CAAClB,IAAI,EAAEM,GAAG,CAAC;IACpC;EACF;AACF"}
1
+ {"version":3,"names":["_path","require","_t","VISITOR_KEYS","TraversalContext","constructor","scope","opts","state","parentPath","queue","priorityQueue","shouldVisit","node","enter","exit","type","keys","length","key","create","container","listKey","NodePath","get","parent","maybeQueue","path","notPriority","push","visitMultiple","visitQueue","visitSingle","visited","WeakSet","stop","resync","contexts","pushContext","has","add","visit","popContext","nodes","Array","isArray","exports","default"],"sources":["../src/context.ts"],"sourcesContent":["import NodePath from \"./path\";\nimport { VISITOR_KEYS } from \"@babel/types\";\nimport type Scope from \"./scope\";\nimport type { TraverseOptions } from \".\";\nimport type * as t from \"@babel/types\";\nimport type { Visitor } from \"./types\";\n\nexport default class TraversalContext<S = unknown> {\n constructor(\n scope: Scope,\n opts: TraverseOptions,\n state: S,\n parentPath: NodePath,\n ) {\n this.parentPath = parentPath;\n this.scope = scope;\n this.state = state;\n this.opts = opts;\n }\n\n declare parentPath: NodePath;\n declare scope: Scope;\n declare state: S;\n declare opts: TraverseOptions;\n queue: Array<NodePath> | null = null;\n priorityQueue: Array<NodePath> | null = null;\n\n /**\n * This method does a simple check to determine whether or not we really need to attempt\n * visit a node. This will prevent us from constructing a NodePath.\n */\n\n shouldVisit(node: t.Node): boolean {\n const opts = this.opts as Visitor;\n if (opts.enter || opts.exit) return true;\n\n // check if we have a visitor for this node\n if (opts[node.type]) return true;\n\n // check if we're going to traverse into this node\n const keys: Array<string> | undefined = VISITOR_KEYS[node.type];\n if (!keys?.length) return false;\n\n // we need to traverse into this node so ensure that it has children to traverse into!\n for (const key of keys) {\n if (\n // @ts-expect-error key is from visitor keys\n node[key]\n ) {\n return true;\n }\n }\n\n return false;\n }\n\n create(\n node: t.Node,\n container: t.Node | t.Node[],\n key: string | number,\n listKey?: string,\n ): NodePath {\n // We don't need to `.setContext()` here, since `.visitQueue()` already\n // calls `.pushContext`.\n return NodePath.get({\n parentPath: this.parentPath,\n parent: node,\n container,\n key: key,\n listKey,\n });\n }\n\n maybeQueue(path: NodePath, notPriority?: boolean) {\n if (this.queue) {\n if (notPriority) {\n this.queue.push(path);\n } else {\n this.priorityQueue.push(path);\n }\n }\n }\n\n visitMultiple(container: t.Node[], parent: t.Node, listKey: string) {\n // nothing to traverse!\n if (container.length === 0) return false;\n\n const queue = [];\n\n // build up initial queue\n for (let key = 0; key < container.length; key++) {\n const node = container[key];\n if (node && this.shouldVisit(node)) {\n queue.push(this.create(parent, container, key, listKey));\n }\n }\n\n return this.visitQueue(queue);\n }\n\n visitSingle(node: t.Node, key: string): boolean {\n if (\n this.shouldVisit(\n // @ts-expect-error key may not index node\n node[key],\n )\n ) {\n return this.visitQueue([this.create(node, node, key)]);\n } else {\n return false;\n }\n }\n\n visitQueue(queue: Array<NodePath>): boolean {\n // set queue\n this.queue = queue;\n this.priorityQueue = [];\n\n const visited = new WeakSet();\n let stop = false;\n\n // visit the queue\n for (const path of queue) {\n path.resync();\n\n if (\n path.contexts.length === 0 ||\n path.contexts[path.contexts.length - 1] !== this\n ) {\n // The context might already have been pushed when this path was inserted and queued.\n // If we always re-pushed here, we could get duplicates and risk leaving contexts\n // on the stack after the traversal has completed, which could break things.\n path.pushContext(this);\n }\n\n // this path no longer belongs to the tree\n if (path.key === null) continue;\n\n // ensure we don't visit the same node twice\n const { node } = path;\n if (visited.has(node)) continue;\n if (node) visited.add(node);\n\n if (path.visit()) {\n stop = true;\n break;\n }\n\n if (this.priorityQueue.length) {\n stop = this.visitQueue(this.priorityQueue);\n this.priorityQueue = [];\n this.queue = queue;\n if (stop) break;\n }\n }\n\n // clear queue\n for (const path of queue) {\n path.popContext();\n }\n\n // clear queue\n this.queue = null;\n\n return stop;\n }\n\n visit(node: t.Node, key: string) {\n // @ts-expect-error key may not index node\n const nodes = node[key] as t.Node | t.Node[] | null;\n if (!nodes) return false;\n\n if (Array.isArray(nodes)) {\n return this.visitMultiple(nodes, node, key);\n } else {\n return this.visitSingle(node, key);\n }\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,EAAA,GAAAD,OAAA;AAA4C;EAAnCE;AAAY,IAAAD,EAAA;AAMN,MAAME,gBAAgB,CAAc;EACjDC,WAAWA,CACTC,KAAY,EACZC,IAAqB,EACrBC,KAAQ,EACRC,UAAoB,EACpB;IAAA,KAWFC,KAAK,GAA2B,IAAI;IAAA,KACpCC,aAAa,GAA2B,IAAI;IAX1C,IAAI,CAACF,UAAU,GAAGA,UAAU;IAC5B,IAAI,CAACH,KAAK,GAAGA,KAAK;IAClB,IAAI,CAACE,KAAK,GAAGA,KAAK;IAClB,IAAI,CAACD,IAAI,GAAGA,IAAI;EAClB;EAcAK,WAAWA,CAACC,IAAY,EAAW;IACjC,MAAMN,IAAI,GAAG,IAAI,CAACA,IAAe;IACjC,IAAIA,IAAI,CAACO,KAAK,IAAIP,IAAI,CAACQ,IAAI,EAAE,OAAO,IAAI;IAGxC,IAAIR,IAAI,CAACM,IAAI,CAACG,IAAI,CAAC,EAAE,OAAO,IAAI;IAGhC,MAAMC,IAA+B,GAAGd,YAAY,CAACU,IAAI,CAACG,IAAI,CAAC;IAC/D,IAAI,EAACC,IAAI,YAAJA,IAAI,CAAEC,MAAM,GAAE,OAAO,KAAK;IAG/B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,EAAE;MACZ;IACF;IAGA,KAAK,MAAMR,IAAI,IAAIjB,KAAK,EAAE;MACxBiB,IAAI,CAACe,UAAU,EAAE;IACnB;IAGA,IAAI,CAAChC,KAAK,GAAG,IAAI;IAEjB,OAAOyB,IAAI;EACb;EAEAM,KAAKA,CAAC5B,IAAY,EAAEM,GAAW,EAAE;IAE/B,MAAMwB,KAAK,GAAG9B,IAAI,CAACM,GAAG,CAA6B;IACnD,IAAI,CAACwB,KAAK,EAAE,OAAO,KAAK;IAExB,IAAIC,KAAK,CAACC,OAAO,CAACF,KAAK,CAAC,EAAE;MACxB,OAAO,IAAI,CAACb,aAAa,CAACa,KAAK,EAAE9B,IAAI,EAAEM,GAAG,CAAC;IAC7C,CAAC,MAAM;MACL,OAAO,IAAI,CAACa,WAAW,CAACnB,IAAI,EAAEM,GAAG,CAAC;IACpC;EACF;AACF;AAAC2B,OAAA,CAAAC,OAAA,GAAA3C,gBAAA"}
package/lib/hub.js CHANGED
@@ -1,4 +1,10 @@
1
- export default class Hub {
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ class Hub {
2
8
  getCode() {}
3
9
  getScope() {}
4
10
  addHelper() {
@@ -8,5 +14,6 @@ export default class Hub {
8
14
  return new Error(msg);
9
15
  }
10
16
  }
17
+ exports.default = Hub;
11
18
 
12
19
  //# sourceMappingURL=hub.js.map
package/lib/hub.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["Hub","getCode","getScope","addHelper","Error","buildError","node","msg","TypeError"],"sources":["../src/hub.ts"],"sourcesContent":["import type Scope from \"./scope\";\nimport type { Node } from \"@babel/types\";\n\nexport interface HubInterface {\n getCode(): string | void;\n getScope(): Scope | void;\n addHelper(name: string): any;\n buildError(node: Node, msg: string, Error: new () => Error): Error;\n}\n\nexport default class Hub implements HubInterface {\n getCode() {}\n\n getScope() {}\n\n addHelper() {\n throw new Error(\"Helpers are not supported by the default hub.\");\n }\n\n buildError(node: Node, msg: string, Error = TypeError): Error {\n return new Error(msg);\n }\n}\n"],"mappings":"AAUA,eAAe,MAAMA,GAAG,CAAyB;EAC/CC,OAAOA,CAAA,EAAG,CAAC;EAEXC,QAAQA,CAAA,EAAG,CAAC;EAEZC,SAASA,CAAA,EAAG;IACV,MAAM,IAAIC,KAAK,CAAC,+CAA+C,CAAC;EAClE;EAEAC,UAAUA,CAACC,IAAU,EAAEC,GAAW,EAAEH,KAAK,GAAGI,SAAS,EAAS;IAC5D,OAAO,IAAIJ,KAAK,CAACG,GAAG,CAAC;EACvB;AACF"}
1
+ {"version":3,"names":["Hub","getCode","getScope","addHelper","Error","buildError","node","msg","TypeError","exports","default"],"sources":["../src/hub.ts"],"sourcesContent":["import type Scope from \"./scope\";\nimport type { Node } from \"@babel/types\";\n\nexport interface HubInterface {\n getCode(): string | void;\n getScope(): Scope | void;\n addHelper(name: string): any;\n buildError(node: Node, msg: string, Error: new () => Error): Error;\n}\n\nexport default class Hub implements HubInterface {\n getCode() {}\n\n getScope() {}\n\n addHelper() {\n throw new Error(\"Helpers are not supported by the default hub.\");\n }\n\n buildError(node: Node, msg: string, Error = TypeError): Error {\n return new Error(msg);\n }\n}\n"],"mappings":";;;;;;AAUe,MAAMA,GAAG,CAAyB;EAC/CC,OAAOA,CAAA,EAAG,CAAC;EAEXC,QAAQA,CAAA,EAAG,CAAC;EAEZC,SAASA,CAAA,EAAG;IACV,MAAM,IAAIC,KAAK,CAAC,+CAA+C,CAAC;EAClE;EAEAC,UAAUA,CAACC,IAAU,EAAEC,GAAW,EAAEH,KAAK,GAAGI,SAAS,EAAS;IAC5D,OAAO,IAAIJ,KAAK,CAACG,GAAG,CAAC;EACvB;AACF;AAACE,OAAA,CAAAC,OAAA,GAAAV,GAAA"}
package/lib/index.js CHANGED
@@ -1,16 +1,40 @@
1
- import * as visitors from "./visitors.js";
2
- import * as _t from "@babel/types";
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "Hub", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _hub.default;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "NodePath", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _path.default;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "Scope", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _scope.default;
22
+ }
23
+ });
24
+ exports.visitors = exports.default = void 0;
25
+ var visitors = require("./visitors");
26
+ exports.visitors = visitors;
27
+ var _t = require("@babel/types");
28
+ var cache = require("./cache");
29
+ var _traverseNode = require("./traverse-node");
30
+ var _path = require("./path");
31
+ var _scope = require("./scope");
32
+ var _hub = require("./hub");
3
33
  const {
4
34
  VISITOR_KEYS,
5
35
  removeProperties,
6
36
  traverseFast
7
37
  } = _t;
8
- import * as cache from "./cache.js";
9
- import { traverseNode } from "./traverse-node.js";
10
- export { default as NodePath } from "./path/index.js";
11
- export { default as Scope } from "./scope/index.js";
12
- export { default as Hub } from "./hub.js";
13
- export { visitors };
14
38
  function traverse(parent, opts = {}, scope, state, parentPath) {
15
39
  if (!parent) return;
16
40
  if (!opts.noScope && !scope) {
@@ -22,9 +46,10 @@ function traverse(parent, opts = {}, scope, state, parentPath) {
22
46
  return;
23
47
  }
24
48
  visitors.explode(opts);
25
- traverseNode(parent, opts, scope, state, parentPath);
49
+ (0, _traverseNode.traverseNode)(parent, opts, scope, state, parentPath);
26
50
  }
27
- export default traverse;
51
+ var _default = traverse;
52
+ exports.default = _default;
28
53
  traverse.visitors = visitors;
29
54
  traverse.verify = visitors.verify;
30
55
  traverse.explode = visitors.explode;
@@ -33,7 +58,7 @@ traverse.cheap = function (node, enter) {
33
58
  return;
34
59
  };
35
60
  traverse.node = function (node, opts, scope, state, path, skipKeys) {
36
- traverseNode(node, opts, scope, state, path, skipKeys);
61
+ (0, _traverseNode.traverseNode)(node, opts, scope, state, path, skipKeys);
37
62
  };
38
63
  traverse.clearNode = function (node, opts) {
39
64
  removeProperties(node, opts);
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["visitors","_t","VISITOR_KEYS","removeProperties","traverseFast","cache","traverseNode","default","NodePath","Scope","Hub","traverse","parent","opts","scope","state","parentPath","noScope","type","Error","explode","verify","cheap","node","enter","path","skipKeys","clearNode","delete","tree","hasDenylistedType","has","stop","hasType","denylistTypes","includes","denylist"],"sources":["../src/index.ts"],"sourcesContent":["import * as visitors from \"./visitors\";\nimport {\n VISITOR_KEYS,\n removeProperties,\n type RemovePropertiesOptions,\n traverseFast,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport * as cache from \"./cache\";\nimport type NodePath from \"./path\";\nimport type { default as Scope, Binding } from \"./scope\";\nimport type { Visitor } from \"./types\";\nimport { traverseNode } from \"./traverse-node\";\n\nexport type { Visitor, Binding };\nexport { default as NodePath } from \"./path\";\nexport { default as Scope } from \"./scope\";\nexport { default as Hub } from \"./hub\";\nexport type { HubInterface } from \"./hub\";\n\nexport { visitors };\n\nexport type TraverseOptions<S = t.Node> = {\n scope?: Scope;\n noScope?: boolean;\n denylist?: string[];\n} & Visitor<S>;\n\nfunction traverse<S>(\n parent: t.Node,\n opts: TraverseOptions<S>,\n scope: Scope | undefined,\n state: S,\n parentPath?: NodePath,\n): void;\n\nfunction traverse(\n parent: t.Node,\n opts: TraverseOptions,\n scope?: Scope,\n state?: any,\n parentPath?: NodePath,\n): void;\n\nfunction traverse<Options extends TraverseOptions>(\n parent: t.Node,\n // @ts-expect-error provide {} as default value for Options\n opts: Options = {},\n scope?: Scope,\n state?: any,\n parentPath?: NodePath,\n) {\n if (!parent) return;\n\n if (!opts.noScope && !scope) {\n if (parent.type !== \"Program\" && parent.type !== \"File\") {\n throw new Error(\n \"You must pass a scope and parentPath unless traversing a Program/File. \" +\n `Instead of that you tried to traverse a ${parent.type} node without ` +\n \"passing scope and parentPath.\",\n );\n }\n }\n\n if (!VISITOR_KEYS[parent.type]) {\n return;\n }\n\n visitors.explode(opts as Visitor);\n\n traverseNode(parent, opts, scope, state, parentPath);\n}\n\nexport default traverse;\n\ntraverse.visitors = visitors;\ntraverse.verify = visitors.verify;\ntraverse.explode = visitors.explode;\n\ntraverse.cheap = function (node: t.Node, enter: (node: t.Node) => void) {\n traverseFast(node, enter);\n return;\n};\n\ntraverse.node = function (\n node: t.Node,\n opts: TraverseOptions,\n scope?: Scope,\n state?: any,\n path?: NodePath,\n skipKeys?: Record<string, boolean>,\n) {\n traverseNode(node, opts, scope, state, path, skipKeys);\n // traverse.node always returns undefined\n};\n\ntraverse.clearNode = function (node: t.Node, opts?: RemovePropertiesOptions) {\n removeProperties(node, opts);\n\n cache.path.delete(node);\n};\n\ntraverse.removeProperties = function (\n tree: t.Node,\n opts?: RemovePropertiesOptions,\n) {\n traverseFast(tree, traverse.clearNode, opts);\n return tree;\n};\n\ntype HasDenylistedTypeState = {\n has: boolean;\n type: t.Node[\"type\"];\n};\nfunction hasDenylistedType(path: NodePath, state: HasDenylistedTypeState) {\n if (path.node.type === state.type) {\n state.has = true;\n path.stop();\n }\n}\n\ntraverse.hasType = function (\n tree: t.Node,\n type: t.Node[\"type\"],\n denylistTypes?: Array<string>,\n): boolean {\n // the node we're searching in is denylisted\n if (denylistTypes?.includes(tree.type)) return false;\n\n // the type we're looking for is the same as the passed node\n if (tree.type === type) return true;\n\n const state: HasDenylistedTypeState = {\n has: false,\n type: type,\n };\n\n traverse(\n tree,\n {\n noScope: true,\n denylist: denylistTypes,\n enter: hasDenylistedType,\n },\n null,\n state,\n );\n\n return state.has;\n};\n\ntraverse.cache = cache;\n"],"mappings":"AAAA,OAAO,KAAKA,QAAQ,MAAM,eAAY;AACtC,YAAAC,EAAA,MAKO,cAAc;AAAC;EAJpBC,YAAY;EACZC,gBAAgB;EAEhBC;AAAY,IAAAH,EAAA;AAGd,OAAO,KAAKI,KAAK,MAAM,YAAS;AAIhC,SAASC,YAAY,QAAQ,oBAAiB;AAG9C,SAASC,OAA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_scope","_hub","VISITOR_KEYS","removeProperties","traverseFast","traverse","parent","opts","scope","state","parentPath","noScope","type","Error","explode","traverseNode","_default","default","verify","cheap","node","enter","path","skipKeys","clearNode","delete","tree","hasDenylistedType","has","stop","hasType","denylistTypes","includes","denylist"],"sources":["../src/index.ts"],"sourcesContent":["import * as visitors from \"./visitors\";\nimport {\n VISITOR_KEYS,\n removeProperties,\n type RemovePropertiesOptions,\n traverseFast,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport * as cache from \"./cache\";\nimport type NodePath from \"./path\";\nimport type { default as Scope, Binding } from \"./scope\";\nimport type { Visitor } from \"./types\";\nimport { traverseNode } from \"./traverse-node\";\n\nexport type { Visitor, Binding };\nexport { default as NodePath } from \"./path\";\nexport { default as Scope } from \"./scope\";\nexport { default as Hub } from \"./hub\";\nexport type { HubInterface } from \"./hub\";\n\nexport { visitors };\n\nexport type TraverseOptions<S = t.Node> = {\n scope?: Scope;\n noScope?: boolean;\n denylist?: string[];\n} & Visitor<S>;\n\nfunction traverse<S>(\n parent: t.Node,\n opts: TraverseOptions<S>,\n scope: Scope | undefined,\n state: S,\n parentPath?: NodePath,\n): void;\n\nfunction traverse(\n parent: t.Node,\n opts: TraverseOptions,\n scope?: Scope,\n state?: any,\n parentPath?: NodePath,\n): void;\n\nfunction traverse<Options extends TraverseOptions>(\n parent: t.Node,\n // @ts-expect-error provide {} as default value for Options\n opts: Options = {},\n scope?: Scope,\n state?: any,\n parentPath?: NodePath,\n) {\n if (!parent) return;\n\n if (!opts.noScope && !scope) {\n if (parent.type !== \"Program\" && parent.type !== \"File\") {\n throw new Error(\n \"You must pass a scope and parentPath unless traversing a Program/File. \" +\n `Instead of that you tried to traverse a ${parent.type} node without ` +\n \"passing scope and parentPath.\",\n );\n }\n }\n\n if (!VISITOR_KEYS[parent.type]) {\n return;\n }\n\n visitors.explode(opts as Visitor);\n\n traverseNode(parent, opts, scope, state, parentPath);\n}\n\nexport default traverse;\n\ntraverse.visitors = visitors;\ntraverse.verify = visitors.verify;\ntraverse.explode = visitors.explode;\n\ntraverse.cheap = function (node: t.Node, enter: (node: t.Node) => void) {\n traverseFast(node, enter);\n return;\n};\n\ntraverse.node = function (\n node: t.Node,\n opts: TraverseOptions,\n scope?: Scope,\n state?: any,\n path?: NodePath,\n skipKeys?: Record<string, boolean>,\n) {\n traverseNode(node, opts, scope, state, path, skipKeys);\n // traverse.node always returns undefined\n};\n\ntraverse.clearNode = function (node: t.Node, opts?: RemovePropertiesOptions) {\n removeProperties(node, opts);\n\n cache.path.delete(node);\n};\n\ntraverse.removeProperties = function (\n tree: t.Node,\n opts?: RemovePropertiesOptions,\n) {\n traverseFast(tree, traverse.clearNode, opts);\n return tree;\n};\n\ntype HasDenylistedTypeState = {\n has: boolean;\n type: t.Node[\"type\"];\n};\nfunction hasDenylistedType(path: NodePath, state: HasDenylistedTypeState) {\n if (path.node.type === state.type) {\n state.has = true;\n path.stop();\n }\n}\n\ntraverse.hasType = function (\n tree: t.Node,\n type: t.Node[\"type\"],\n denylistTypes?: Array<string>,\n): boolean {\n // the node we're searching in is denylisted\n if (denylistTypes?.includes(tree.type)) return false;\n\n // the type we're looking for is the same as the passed node\n if (tree.type === type) return true;\n\n const state: HasDenylistedTypeState = {\n has: false,\n type: type,\n };\n\n traverse(\n tree,\n {\n noScope: true,\n denylist: denylistTypes,\n enter: hasDenylistedType,\n },\n null,\n state,\n );\n\n return state.has;\n};\n\ntraverse.cache = cache;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,QAAA,GAAAC,OAAA;AAAuCC,OAAA,CAAAF,QAAA,GAAAA,QAAA;AACvC,IAAAG,EAAA,GAAAF,OAAA;AAOA,IAAAG,KAAA,GAAAH,OAAA;AAIA,IAAAI,aAAA,GAAAJ,OAAA;AAGA,IAAAK,KAAA,GAAAL,OAAA;AACA,IAAAM,MAAA,GAAAN,OAAA;AACA,IAAAO,IAAA,GAAAP,OAAA;AAAuC;EAfrCQ,YAA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, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.find = find;
7
+ exports.findParent = findParent;
8
+ exports.getAncestry = getAncestry;
9
+ exports.getDeepestCommonAncestorFrom = getDeepestCommonAncestorFrom;
10
+ exports.getEarliestCommonAncestorFrom = getEarliestCommonAncestorFrom;
11
+ exports.getFunctionParent = getFunctionParent;
12
+ exports.getStatementParent = getStatementParent;
13
+ exports.inType = inType;
14
+ exports.isAncestor = isAncestor;
15
+ exports.isDescendant = isDescendant;
16
+ var _t = require("@babel/types");
2
17
  const {
3
18
  VISITOR_KEYS
4
19
  } = _t;
5
- export function findParent(callback) {
20
+ function findParent(callback) {
6
21
  let path = this;
7
22
  while (path = path.parentPath) {
8
23
  if (callback(path)) return path;
9
24
  }
10
25
  return null;
11
26
  }
12
- export function find(callback) {
27
+ function find(callback) {
13
28
  let path = this;
14
29
  do {
15
30
  if (callback(path)) return path;
16
31
  } while (path = path.parentPath);
17
32
  return null;
18
33
  }
19
- export function getFunctionParent() {
34
+ function getFunctionParent() {
20
35
  return this.findParent(p => p.isFunction());
21
36
  }
22
- export function getStatementParent() {
37
+ function getStatementParent() {
23
38
  let path = this;
24
39
  do {
25
40
  if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
@@ -33,7 +48,7 @@ export function getStatementParent() {
33
48
  }
34
49
  return path;
35
50
  }
36
- export function getEarliestCommonAncestorFrom(paths) {
51
+ function getEarliestCommonAncestorFrom(paths) {
37
52
  return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
38
53
  let earliest;
39
54
  const keys = VISITOR_KEYS[deepest.type];
@@ -58,7 +73,7 @@ export function getEarliestCommonAncestorFrom(paths) {
58
73
  return earliest;
59
74
  });
60
75
  }
61
- export function getDeepestCommonAncestorFrom(paths, filter) {
76
+ function getDeepestCommonAncestorFrom(paths, filter) {
62
77
  if (!paths.length) {
63
78
  return this;
64
79
  }
@@ -98,7 +113,7 @@ export function getDeepestCommonAncestorFrom(paths, filter) {
98
113
  throw new Error("Couldn't find intersection");
99
114
  }
100
115
  }
101
- export function getAncestry() {
116
+ function getAncestry() {
102
117
  let path = this;
103
118
  const paths = [];
104
119
  do {
@@ -106,13 +121,13 @@ export function getAncestry() {
106
121
  } while (path = path.parentPath);
107
122
  return paths;
108
123
  }
109
- export function isAncestor(maybeDescendant) {
124
+ function isAncestor(maybeDescendant) {
110
125
  return maybeDescendant.isDescendant(this);
111
126
  }
112
- export function isDescendant(maybeAncestor) {
127
+ function isDescendant(maybeAncestor) {
113
128
  return !!this.findParent(parent => parent === maybeAncestor);
114
129
  }
115
- export function inType(...candidateTypes) {
130
+ function inType(...candidateTypes) {
116
131
  let path = this;
117
132
  while (path) {
118
133
  for (const type of candidateTypes) {
@@ -1 +1 @@
1
- {"version":3,"names":["_t","VISITOR_KEYS","findParent","callback","path","parentPath","find","getFunctionParent","p","isFunction","getStatementParent","Array","isArray","container","isStatement","isProgram","isFile","Error","getEarliestCommonAncestorFrom","paths","getDeepestCommonAncestorFrom","deepest","i","ancestries","earliest","keys","type","ancestry","listKey","key","earliestKeyIndex","indexOf","parentKey","currentKeyIndex","filter","length","minDepth","Infinity","lastCommonIndex","lastCommon","map","unshift","first","depthLoop","shouldMatch","getAncestry","push","isAncestor","maybeDescendant","isDescendant","maybeAncestor","parent","inType","candidateTypes","node"],"sources":["../../src/path/ancestry.ts"],"sourcesContent":["// This file contains that retrieve or validate anything related to the current paths ancestry.\n\nimport { VISITOR_KEYS } from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type NodePath from \"./index\";\n\n/**\n * Starting at the parent path of the current `NodePath` and going up the\n * tree, return the first `NodePath` that causes the provided `callback`\n * to return a truthy value, or `null` if the `callback` never returns a\n * truthy value.\n */\n\nexport function findParent(\n this: NodePath,\n callback: (path: NodePath) => boolean,\n): NodePath | null {\n let path = this;\n while ((path = path.parentPath)) {\n if (callback(path)) return path;\n }\n return null;\n}\n\n/**\n * Starting at current `NodePath` and going up the tree, return the first\n * `NodePath` that causes the provided `callback` to return a truthy value,\n * or `null` if the `callback` never returns a truthy value.\n */\n\nexport function find(\n this: NodePath,\n callback: (path: NodePath) => boolean,\n): NodePath | null {\n let path = this;\n do {\n if (callback(path)) return path;\n } while ((path = path.parentPath));\n return null;\n}\n\n/**\n * Get the parent function of the current path.\n */\n\nexport function getFunctionParent(this: NodePath): NodePath<t.Function> | null {\n return this.findParent(p => p.isFunction()) as NodePath<t.Function> | null;\n}\n\n/**\n * Walk up the tree until we hit a parent node path in a list.\n */\n\nexport function getStatementParent(this: NodePath): NodePath<t.Statement> {\n let path = this;\n\n do {\n if (\n !path.parentPath ||\n (Array.isArray(path.container) && path.isStatement())\n ) {\n break;\n } else {\n path = path.parentPath;\n }\n } while (path);\n\n if (path && (path.isProgram() || path.isFile())) {\n throw new Error(\n \"File/Program node, we can't possibly find a statement parent to this\",\n );\n }\n\n return path as NodePath<t.Statement>;\n}\n\n/**\n * Get the deepest common ancestor and then from it, get the earliest relationship path\n * to that ancestor.\n *\n * Earliest is defined as being \"before\" all the other nodes in terms of list container\n * position and visiting key.\n */\n\nexport function getEarliestCommonAncestorFrom(\n this: NodePath,\n paths: Array<NodePath>,\n): NodePath {\n return this.getDeepestCommonAncestorFrom(\n paths,\n function (deepest, i, ancestries) {\n let earliest;\n const keys = VISITOR_KEYS[deepest.type];\n\n for (const ancestry of ancestries) {\n const path = ancestry[i + 1];\n\n // first path\n if (!earliest) {\n earliest = path;\n continue;\n }\n\n // handle containers\n if (path.listKey && earliest.listKey === path.listKey) {\n // we're in the same container so check if we're earlier\n if (path.key < earliest.key) {\n earliest = path;\n continue;\n }\n }\n\n // handle keys\n const earliestKeyIndex = keys.indexOf(earliest.parentKey);\n const currentKeyIndex = keys.indexOf(path.parentKey);\n if (earliestKeyIndex > currentKeyIndex) {\n // key appears before so it's earlier\n earliest = path;\n }\n }\n\n return earliest;\n },\n );\n}\n\n/**\n * Get the earliest path in the tree where the provided `paths` intersect.\n *\n * TODO: Possible optimisation target.\n */\n\nexport function getDeepestCommonAncestorFrom(\n this: NodePath,\n paths: Array<NodePath>,\n filter?: (deepest: NodePath, i: number, ancestries: NodePath[][]) => NodePath,\n): NodePath {\n if (!paths.length) {\n return this;\n }\n\n if (paths.length === 1) {\n return paths[0];\n }\n\n // minimum depth of the tree so we know the highest node\n let minDepth = Infinity;\n\n // last common ancestor\n let lastCommonIndex, lastCommon;\n\n // get the ancestors of the path, breaking when the parent exceeds ourselves\n const ancestries = paths.map(path => {\n const ancestry: NodePath[] = [];\n\n do {\n ancestry.unshift(path);\n } while ((path = path.parentPath) && path !== this);\n\n // save min depth to avoid going too far in\n if (ancestry.length < minDepth) {\n minDepth = ancestry.length;\n }\n\n return ancestry;\n });\n\n // get the first ancestry so we have a seed to assess all other ancestries with\n const first = ancestries[0];\n\n // check ancestor equality\n depthLoop: for (let i = 0; i < minDepth; i++) {\n const shouldMatch = first[i];\n\n for (const ancestry of ancestries) {\n if (ancestry[i] !== shouldMatch) {\n // we've hit a snag\n break depthLoop;\n }\n }\n\n // next iteration may break so store these so they can be returned\n lastCommonIndex = i;\n lastCommon = shouldMatch;\n }\n\n if (lastCommon) {\n if (filter) {\n return filter(lastCommon, lastCommonIndex, ancestries);\n } else {\n return lastCommon;\n }\n } else {\n throw new Error(\"Couldn't find intersection\");\n }\n}\n\n/**\n * Build an array of node paths containing the entire ancestry of the current node path.\n *\n * NOTE: The current node path is included in this.\n */\n\nexport function getAncestry(this: NodePath): Array<NodePath> {\n let path = this;\n const paths = [];\n do {\n paths.push(path);\n } while ((path = path.parentPath));\n return paths;\n}\n\n/**\n * A helper to find if `this` path is an ancestor of @param maybeDescendant\n */\nexport function isAncestor(this: NodePath, maybeDescendant: NodePath): boolean {\n return maybeDescendant.isDescendant(this);\n}\n\n/**\n * A helper to find if `this` path is a descendant of @param maybeAncestor\n */\nexport function isDescendant(this: NodePath, maybeAncestor: NodePath): boolean {\n return !!this.findParent(parent => parent === maybeAncestor);\n}\n\nexport function inType(this: NodePath, ...candidateTypes: string[]): boolean {\n let path = this;\n while (path) {\n for (const type of candidateTypes) {\n if (path.node.type === type) return true;\n }\n path = path.parentPath;\n }\n\n return false;\n}\n"],"mappings":"AAEA,YAAAA,EAAA,MAA6B,cAAc;AAAC;EAAnCC;AAAY,IAAAD,EAAA;AAWrB,OAAO,SAASE,UAAUA,CAExBC,QAAqC,EACpB;EACjB,IAAIC,IAAI,GAAG,IAAI;EACf,OAAQA,IAAI,GAAGA,IAAI,CAACC,UAAU,EAAG;IAC/B,IAAIF,QAAQ,CAACC,IAAI,CAAC,EAAE,OAAOA,IAAI;EACjC;EACA,OAAO,IAAI;AACb;AAQA,OAAO,SAASE,IAAIA,CAElBH,QAAqC,EACpB;EACjB,IAAIC,IAAI,GAAG,IAAI;EACf,GAAG;IACD,IAAID,QAAQ,CAACC,IAAI,CAAC,EAAE,OAAOA,IAAI;EACjC,CAAC,QAASA,IAAI,GAAGA,IAAI,CAACC,UAAU;EAChC,OAAO,IAAI;AACb;AAMA,OAAO,SAASE,iBAAiBA,CAAA,EAA8C;EAC7E,OAAO,IAAI,CAACL,UAAU,CAACM,CAAC,IAAIA,CAAC,CAACC,UAAU,EAAE,CAAC;AAC7C;AAMA,OAAO,SAASC,kBAAkBA,CAAA,EAAwC;EACxE,IAAIN,IAAI,GAAG,IAAI;EAEf,GAAG;IACD,IACE,CAACA,IAAI,CAACC,UAAU,IACfM,KAAK,CAACC,OAAO,CAACR,IAAI,CAACS,SAAS,CAAC,IAAIT,IAAI,CAACU,WAAW,EAAG,EACrD;MACA;IACF,CAAC,MAAM;MACLV,IAAI,GAAGA,IAAI,CAACC,UAAU;IACxB;EACF,CAAC,QAAQD,IAAI;EAEb,IAAIA,IAAI,KAAKA,IAAI,CAACW,SAAS,EAAE,IAAIX,IAAI,CAACY,MAAM,EAAE,CAAC,EAAE;IAC/C,MAAM,IAAIC,KAAK,CACb,sEAAsE,CACvE;EACH;EAEA,OAAOb,IAAI;AACb;AAUA,OAAO,SAASc,6BAA6BA,CAE3CC,KAAsB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,EAAW;EAC7E,OAAOA,eAAe,CAACC,YAAY,CAAC,IAAI,CAAC;AAC3C;AAKA,OAAO,SAASA,YAAYA,CAAiBC,aAAuB,EAAW;EAC7E,OAAO,CAAC,CAAC,IAAI,CAAChD,UAAU,CAACiD,MAAM,IAAIA,MAAM,KAAKD,aAAa,CAAC;AAC9D;AAEA,OAAO,SAASE,MAAMA,CAAiB,GAAGC,cAAwB,EAAW;EAC3E,IAAIjD,IAAI,GAAG,IAAI;EACf,OAAOA,IAAI,EAAE;IACX,KAAK,MAAMsB,IAAI,IAAI2B,cAAc,EAAE;MACjC,IAAIjD,IAAI,CAACkD,IAAI,CAAC5B,IAAI,KAAKA,IAAI,EAAE,OAAO,IAAI;IAC1C;IACAtB,IAAI,GAAGA,IAAI,CAACC,UAAU;EACxB;EAEA,OAAO,KAAK;AACd"}
1
+ {"version":3,"names":["_t","require","VISITOR_KEYS","findParent","callback","path","parentPath","find","getFunctionParent","p","isFunction","getStatementParent","Array","isArray","container","isStatement","isProgram","isFile","Error","getEarliestCommonAncestorFrom","paths","getDeepestCommonAncestorFrom","deepest","i","ancestries","earliest","keys","type","ancestry","listKey","key","earliestKeyIndex","indexOf","parentKey","currentKeyIndex","filter","length","minDepth","Infinity","lastCommonIndex","lastCommon","map","unshift","first","depthLoop","shouldMatch","getAncestry","push","isAncestor","maybeDescendant","isDescendant","maybeAncestor","parent","inType","candidateTypes","node"],"sources":["../../src/path/ancestry.ts"],"sourcesContent":["// This file contains that retrieve or validate anything related to the current paths ancestry.\n\nimport { VISITOR_KEYS } from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type NodePath from \"./index\";\n\n/**\n * Starting at the parent path of the current `NodePath` and going up the\n * tree, return the first `NodePath` that causes the provided `callback`\n * to return a truthy value, or `null` if the `callback` never returns a\n * truthy value.\n */\n\nexport function findParent(\n this: NodePath,\n callback: (path: NodePath) => boolean,\n): NodePath | null {\n let path = this;\n while ((path = path.parentPath)) {\n if (callback(path)) return path;\n }\n return null;\n}\n\n/**\n * Starting at current `NodePath` and going up the tree, return the first\n * `NodePath` that causes the provided `callback` to return a truthy value,\n * or `null` if the `callback` never returns a truthy value.\n */\n\nexport function find(\n this: NodePath,\n callback: (path: NodePath) => boolean,\n): NodePath | null {\n let path = this;\n do {\n if (callback(path)) return path;\n } while ((path = path.parentPath));\n return null;\n}\n\n/**\n * Get the parent function of the current path.\n */\n\nexport function getFunctionParent(this: NodePath): NodePath<t.Function> | null {\n return this.findParent(p => p.isFunction()) as NodePath<t.Function> | null;\n}\n\n/**\n * Walk up the tree until we hit a parent node path in a list.\n */\n\nexport function getStatementParent(this: NodePath): NodePath<t.Statement> {\n let path = this;\n\n do {\n if (\n !path.parentPath ||\n (Array.isArray(path.container) && path.isStatement())\n ) {\n break;\n } else {\n path = path.parentPath;\n }\n } while (path);\n\n if (path && (path.isProgram() || path.isFile())) {\n throw new Error(\n \"File/Program node, we can't possibly find a statement parent to this\",\n );\n }\n\n return path as NodePath<t.Statement>;\n}\n\n/**\n * Get the deepest common ancestor and then from it, get the earliest relationship path\n * to that ancestor.\n *\n * Earliest is defined as being \"before\" all the other nodes in terms of list container\n * position and visiting key.\n */\n\nexport function getEarliestCommonAncestorFrom(\n this: NodePath,\n paths: Array<NodePath>,\n): NodePath {\n return this.getDeepestCommonAncestorFrom(\n paths,\n function (deepest, i, ancestries) {\n let earliest;\n const keys = VISITOR_KEYS[deepest.type];\n\n for (const ancestry of ancestries) {\n const path = ancestry[i + 1];\n\n // first path\n if (!earliest) {\n earliest = path;\n continue;\n }\n\n // handle containers\n if (path.listKey && earliest.listKey === path.listKey) {\n // we're in the same container so check if we're earlier\n if (path.key < earliest.key) {\n earliest = path;\n continue;\n }\n }\n\n // handle keys\n const earliestKeyIndex = keys.indexOf(earliest.parentKey);\n const currentKeyIndex = keys.indexOf(path.parentKey);\n if (earliestKeyIndex > currentKeyIndex) {\n // key appears before so it's earlier\n earliest = path;\n }\n }\n\n return earliest;\n },\n );\n}\n\n/**\n * Get the earliest path in the tree where the provided `paths` intersect.\n *\n * TODO: Possible optimisation target.\n */\n\nexport function getDeepestCommonAncestorFrom(\n this: NodePath,\n paths: Array<NodePath>,\n filter?: (deepest: NodePath, i: number, ancestries: NodePath[][]) => NodePath,\n): NodePath {\n if (!paths.length) {\n return this;\n }\n\n if (paths.length === 1) {\n return paths[0];\n }\n\n // minimum depth of the tree so we know the highest node\n let minDepth = Infinity;\n\n // last common ancestor\n let lastCommonIndex, lastCommon;\n\n // get the ancestors of the path, breaking when the parent exceeds ourselves\n const ancestries = paths.map(path => {\n const ancestry: NodePath[] = [];\n\n do {\n ancestry.unshift(path);\n } while ((path = path.parentPath) && path !== this);\n\n // save min depth to avoid going too far in\n if (ancestry.length < minDepth) {\n minDepth = ancestry.length;\n }\n\n return ancestry;\n });\n\n // get the first ancestry so we have a seed to assess all other ancestries with\n const first = ancestries[0];\n\n // check ancestor equality\n depthLoop: for (let i = 0; i < minDepth; i++) {\n const shouldMatch = first[i];\n\n for (const ancestry of ancestries) {\n if (ancestry[i] !== shouldMatch) {\n // we've hit a snag\n break depthLoop;\n }\n }\n\n // next iteration may break so store these so they can be returned\n lastCommonIndex = i;\n lastCommon = shouldMatch;\n }\n\n if (lastCommon) {\n if (filter) {\n return filter(lastCommon, lastCommonIndex, ancestries);\n } else {\n return lastCommon;\n }\n } else {\n throw new Error(\"Couldn't find intersection\");\n }\n}\n\n/**\n * Build an array of node paths containing the entire ancestry of the current node path.\n *\n * NOTE: The current node path is included in this.\n */\n\nexport function getAncestry(this: NodePath): Array<NodePath> {\n let path = this;\n const paths = [];\n do {\n paths.push(path);\n } while ((path = path.parentPath));\n return paths;\n}\n\n/**\n * A helper to find if `this` path is an ancestor of @param maybeDescendant\n */\nexport function isAncestor(this: NodePath, maybeDescendant: NodePath): boolean {\n return maybeDescendant.isDescendant(this);\n}\n\n/**\n * A helper to find if `this` path is a descendant of @param maybeAncestor\n */\nexport function isDescendant(this: NodePath, maybeAncestor: NodePath): boolean {\n return !!this.findParent(parent => parent === maybeAncestor);\n}\n\nexport function inType(this: NodePath, ...candidateTypes: string[]): boolean {\n let path = this;\n while (path) {\n for (const type of candidateTypes) {\n if (path.node.type === type) return true;\n }\n path = path.parentPath;\n }\n\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAEA,IAAAA,EAAA,GAAAC,OAAA;AAA4C;EAAnCC;AAAY,IAAAF,EAAA;AAWd,SAASG,UAAUA,CAExBC,QAAqC,EACpB;EACjB,IAAIC,IAAI,GAAG,IAAI;EACf,OAAQA,IAAI,GAAGA,IAAI,CAACC,UAAU,EAAG;IAC/B,IAAIF,QAAQ,CAACC,IAAI,CAAC,EAAE,OAAOA,IAAI;EACjC;EACA,OAAO,IAAI;AACb;AAQO,SAASE,IAAIA,CAElBH,QAAqC,EACpB;EACjB,IAAIC,IAAI,GAAG,IAAI;EACf,GAAG;IACD,IAAID,QAAQ,CAACC,IAAI,CAAC,EAAE,OAAOA,IAAI;EACjC,CAAC,QAASA,IAAI,GAAGA,IAAI,CAACC,UAAU;EAChC,OAAO,IAAI;AACb;AAMO,SAASE,iBAAiBA,CAAA,EAA8C;EAC7E,OAAO,IAAI,CAACL,UAAU,CAACM,CAAC,IAAIA,CAAC,CAACC,UAAU,EAAE,CAAC;AAC7C;AAMO,SAASC,kBAAkBA,CAAA,EAAwC;EACxE,IAAIN,IAAI,GAAG,IAAI;EAEf,GAAG;IACD,IACE,CAACA,IAAI,CAACC,UAAU,IACfM,KAAK,CAACC,OAAO,CAACR,IAAI,CAACS,SAAS,CAAC,IAAIT,IAAI,CAACU,WAAW,EAAG,EACrD;MACA;IACF,CAAC,MAAM;MACLV,IAAI,GAAGA,IAAI,CAACC,UAAU;IACxB;EACF,CAAC,QAAQD,IAAI;EAEb,IAAIA,IAAI,KAAKA,IAAI,CAACW,SAAS,EAAE,IAAIX,IAAI,CAACY,MAAM,EAAE,CAAC,EAAE;IAC/C,MAAM,IAAIC,KAAK,CACb,sEAAsE,CACvE;EACH;EAEA,OA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KO,SAAS4B,UAAUA,CAAiBC,eAAyB,EAAW;EAC7E,OAAOA,eAAe,CAACC,YAAY,CAAC,IAAI,CAAC;AAC3C;AAKO,SAASA,YAAYA,CAAiBC,aAAuB,EAAW;EAC7E,OAAO,CAAC,CAAC,IAAI,CAAChD,UAAU,CAACiD,MAAM,IAAIA,MAAM,KAAKD,aAAa,CAAC;AAC9D;AAEO,SAASE,MAAMA,CAAiB,GAAGC,cAAwB,EAAW;EAC3E,IAAIjD,IAAI,GAAG,IAAI;EACf,OAAOA,IAAI,EAAE;IACX,KAAK,MAAMsB,IAAI,IAAI2B,cAAc,EAAE;MACjC,IAAIjD,IAAI,CAACkD,IAAI,CAAC5B,IAAI,KAAKA,IAAI,EAAE,OAAO,IAAI;IAC1C;IACAtB,IAAI,GAAGA,IAAI,CAACC,UAAU;EACxB;EAEA,OAAO,KAAK;AACd"}
@@ -1,9 +1,17 @@
1
- import * as _t from "@babel/types";
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.addComment = addComment;
7
+ exports.addComments = addComments;
8
+ exports.shareCommentsWithSiblings = shareCommentsWithSiblings;
9
+ var _t = require("@babel/types");
2
10
  const {
3
11
  addComment: _addComment,
4
12
  addComments: _addComments
5
13
  } = _t;
6
- export function shareCommentsWithSiblings() {
14
+ function shareCommentsWithSiblings() {
7
15
  if (typeof this.key === "string") return;
8
16
  const node = this.node;
9
17
  if (!node) return;
@@ -36,10 +44,10 @@ function removeIfExisting(list, toRemove) {
36
44
  lastFoundIndex = i;
37
45
  });
38
46
  }
39
- export function addComment(type, content, line) {
47
+ function addComment(type, content, line) {
40
48
  _addComment(this.node, type, content, line);
41
49
  }
42
- export function addComments(type, comments) {
50
+ function addComments(type, comments) {
43
51
  _addComments(this.node, type, comments);
44
52
  }
45
53
 
@@ -1 +1 @@
1
- {"version":3,"names":["_t","addComment","_addComment","addComments","_addComments","shareCommentsWithSiblings","key","node","trailing","trailingComments","leading","leadingComments","prev","getSibling","next","hasPrev","Boolean","hasNext","removeIfExisting","list","toRemove","lastFoundIndex","filter","el","i","indexOf","type","content","line","comments"],"sources":["../../src/path/comments.ts"],"sourcesContent":["// This file contains methods responsible for dealing with comments.\nimport type * as t from \"@babel/types\";\nimport type NodePath from \"./index\";\nimport {\n addComment as _addComment,\n addComments as _addComments,\n} from \"@babel/types\";\n\n/**\n * Share comments amongst siblings.\n */\n\nexport function shareCommentsWithSiblings(this: NodePath) {\n // NOTE: this assumes numbered keys\n if (typeof this.key === \"string\") return;\n\n const node = this.node;\n if (!node) return;\n\n const trailing = node.trailingComments;\n const leading = node.leadingComments;\n if (!trailing && !leading) return;\n\n const prev = this.getSibling(this.key - 1);\n const next = this.getSibling(this.key + 1);\n const hasPrev = Boolean(prev.node);\n const hasNext = Boolean(next.node);\n\n if (hasPrev) {\n if (leading) {\n prev.addComments(\n \"trailing\",\n removeIfExisting(leading, prev.node.trailingComments),\n );\n }\n if (trailing && !hasNext) prev.addComments(\"trailing\", trailing);\n }\n if (hasNext) {\n if (trailing) {\n next.addComments(\n \"leading\",\n removeIfExisting(trailing, next.node.leadingComments),\n );\n }\n if (leading && !hasPrev) next.addComments(\"leading\", leading);\n }\n}\n\nfunction removeIfExisting<T>(list: T[], toRemove?: T[]): T[] {\n if (!toRemove) return list;\n let lastFoundIndex = -1;\n return list.filter(el => {\n const i = toRemove.indexOf(el, lastFoundIndex);\n if (i === -1) return true;\n lastFoundIndex = i;\n });\n}\n\nexport function addComment(\n this: NodePath,\n type: t.CommentTypeShorthand,\n content: string,\n line?: boolean,\n) {\n _addComment(this.node, type, content, line);\n}\n\n/**\n * Give node `comments` of the specified `type`.\n */\n\nexport function addComments(\n this: NodePath,\n type: t.CommentTypeShorthand,\n comments: t.Comment[],\n) {\n _addComments(this.node, type, comments);\n}\n"],"mappings":"AAGA,YAAAA,EAAA,MAGO,cAAc;AAAC;EAFpBC,UAAU,EAAIC,WAAW;EACzBC,WAAW,EAAIC;AAAY,IA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AACA,QAAQ,EAAE,OAAOD,IAAI;EAC1B,IAAIE,cAAc,GAAG,CAAC,CAAC;EACvB,OAAOF,IAAI,CAACG,MAAM,CAACC,EAAE,IAAI;IACvB,MAAMC,CAAC,GAAGJ,QAAQ,CAACK,OAAO,CAACF,EAAE,EAAEF,cAAc,CAAC;IAC9C,IAAIG,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,IAAI;IACzBH,cAAc,GAAGG,CAAC;EACpB,CAAC,CAAC;AACJ;AAEA,OAAO,SAASvB,UAAUA,CAExByB,IAA4B,EAC5BC,OAAe,EACfC,IAAc,EACd;EACA1B,WAAW,CAAC,IAAI,CAACK,IAAI,EAAEmB,IAAI,EAAEC,OAAO,EAAEC,IAAI,CAAC;AAC7C;AAMA,OAAO,SAASzB,WAAWA,CAEzBuB,IAA4B,EAC5BG,QAAqB,EACrB;EACAzB,YAAY,CAAC,IAAI,CAACG,IAAI,EAAEmB,IAAI,EAAEG,QAAQ,CAAC;AACzC"}
1
+ {"version":3,"names":["_t","require","addComment","_addComment","addComments","_addComments","shareCommentsWithSiblings","key","node","trailing","trailingComments","leading","leadingComments","prev","getSibling","next","hasPrev","Boolean","hasNext","removeIfExisting","list","toRemove","lastFoundIndex","filter","el","i","indexOf","type","content","line","comments"],"sources":["../../src/path/comments.ts"],"sourcesContent":["// This file contains methods responsible for dealing with comments.\nimport type * as t from \"@babel/types\";\nimport type NodePath from \"./index\";\nimport {\n addComment as _addComment,\n addComments as _addComments,\n} from \"@babel/types\";\n\n/**\n * Share comments amongst siblings.\n */\n\nexport function shareCommentsWithSiblings(this: NodePath) {\n // NOTE: this assumes numbered keys\n if (typeof this.key === \"string\") return;\n\n const node = this.node;\n if (!node) return;\n\n const trailing = node.trailingComments;\n const leading = node.leadingComments;\n if (!trailing && !leading) return;\n\n const prev = this.getSibling(this.key - 1);\n const next = this.getSibling(this.key + 1);\n const hasPrev = Boolean(prev.node);\n const hasNext = Boolean(next.node);\n\n if (hasPrev) {\n if (leading) {\n prev.addComments(\n \"trailing\",\n removeIfExisting(leading, prev.node.trailingComments),\n );\n }\n if (trailing && !hasNext) prev.addComments(\"trailing\", trailing);\n }\n if (hasNext) {\n if (trailing) {\n next.addComments(\n \"leading\",\n removeIfExisting(trailing, next.node.leadingComments),\n );\n }\n if (leading && !hasPrev) next.addComments(\"leading\", leading);\n }\n}\n\nfunction removeIfExisting<T>(list: T[], toRemove?: T[]): T[] {\n if (!toRemove) return list;\n let lastFoundIndex = -1;\n return list.filter(el => {\n const i = toRemove.indexOf(el, lastFoundIndex);\n if (i === -1) return true;\n lastFoundIndex = i;\n });\n}\n\nexport function addComment(\n this: NodePath,\n type: t.CommentTypeShorthand,\n content: string,\n line?: boolean,\n) {\n _addComment(this.node, type, content, line);\n}\n\n/**\n * Give node `comments` of the specified `type`.\n */\n\nexport function addComments(\n this: NodePath,\n type: t.CommentTypeShorthand,\n comments: t.Comment[],\n) {\n _addComments(this.node, type, comments);\n}\n"],"mappings":";;;;;;;;AAGA,IAAAA,EAAA,GAAAC,OAAA;AAGsB;EAFpBC,UAAU,EAAIC,WAAW;EACzBC,WAAW,EAAIC;AAAY,IAAAL,EAAA;AAOtB,SAASM,yBAAyBA,CAAA,EAAiB;EAExD,IAAI,OAAO,IAAI,CAACC,GAAG,KAAK,QAAQ,EAAE;EAElC,MAAMC,IAAI,GAAG,IAAI,CAACA,IAAI;EACtB,IAAI,CAACA,IAAI,EAAE;EAEX,MAAMC,QAAQ,GAAGD,IAAI,CAACE,gBAAgB;EACtC,MAAMC,OAAO,GAAGH,IAAI,CAACI,eAAe;EACpC,IAAI,CAACH,QAAQ,IAAI,CAACE,OAAO,EAAE;EAE3B,MAAME,IAAI,GAAG,IAAI,CAACC,UAAU,CAAC,IAAI,CAACP,GAAG,GAAG,CAAC,CAAC;EAC1C,MAAMQ,IAAI,GAAG,IAAI,CAACD,UAAU,CAAC,IAAI,CAACP,GAAG,GAAG,CAAC,CAAC;EAC1C,MAAMS,OAAO,GAAGC,OAAO,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Y,CAAC,IAAI,CAACG,IAAI,EAAEmB,IAAI,EAAEG,QAAQ,CAAC;AACzC"}
@@ -1,6 +1,31 @@
1
- import { traverseNode } from "../traverse-node.js";
2
- import { SHOULD_SKIP, SHOULD_STOP } from "./index.js";
3
- export function call(key) {
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports._call = _call;
7
+ exports._getQueueContexts = _getQueueContexts;
8
+ exports._resyncKey = _resyncKey;
9
+ exports._resyncList = _resyncList;
10
+ exports._resyncParent = _resyncParent;
11
+ exports._resyncRemoved = _resyncRemoved;
12
+ exports.call = call;
13
+ exports.isBlacklisted = exports.isDenylisted = isDenylisted;
14
+ exports.popContext = popContext;
15
+ exports.pushContext = pushContext;
16
+ exports.requeue = requeue;
17
+ exports.resync = resync;
18
+ exports.setContext = setContext;
19
+ exports.setKey = setKey;
20
+ exports.setScope = setScope;
21
+ exports.setup = setup;
22
+ exports.skip = skip;
23
+ exports.skipKey = skipKey;
24
+ exports.stop = stop;
25
+ exports.visit = visit;
26
+ var _traverseNode = require("../traverse-node");
27
+ var _index = require("./index");
28
+ function call(key) {
4
29
  const opts = this.opts;
5
30
  this.debug(key);
6
31
  if (this.node) {
@@ -11,7 +36,7 @@ export function call(key) {
11
36
  }
12
37
  return false;
13
38
  }
14
- export function _call(fns) {
39
+ function _call(fns) {
15
40
  if (!fns) return false;
16
41
  for (const fn of fns) {
17
42
  if (!fn) continue;
@@ -29,12 +54,11 @@ export function _call(fns) {
29
54
  }
30
55
  return false;
31
56
  }
32
- export function isDenylisted() {
57
+ function isDenylisted() {
33
58
  var _this$opts$denylist;
34
59
  const denylist = (_this$opts$denylist = this.opts.denylist) != null ? _this$opts$denylist : this.opts.blacklist;
35
60
  return denylist && denylist.indexOf(this.node.type) > -1;
36
61
  }
37
- export { isDenylisted as isBlacklisted };
38
62
  function restoreContext(path, context) {
39
63
  if (path.context !== context) {
40
64
  path.context = context;
@@ -42,7 +66,7 @@ function restoreContext(path, context) {
42
66
  path.opts = context.opts;
43
67
  }
44
68
  }
45
- export function visit() {
69
+ function visit() {
46
70
  if (!this.node) {
47
71
  return false;
48
72
  }
@@ -59,24 +83,24 @@ export function visit() {
59
83
  }
60
84
  restoreContext(this, currentContext);
61
85
  this.debug("Recursing into...");
62
- this.shouldStop = traverseNode(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
86
+ this.shouldStop = (0, _traverseNode.traverseNode)(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
63
87
  restoreContext(this, currentContext);
64
88
  this.call("exit");
65
89
  return this.shouldStop;
66
90
  }
67
- export function skip() {
91
+ function skip() {
68
92
  this.shouldSkip = true;
69
93
  }
70
- export function skipKey(key) {
94
+ function skipKey(key) {
71
95
  if (this.skipKeys == null) {
72
96
  this.skipKeys = {};
73
97
  }
74
98
  this.skipKeys[key] = true;
75
99
  }
76
- export function stop() {
77
- this._traverseFlags |= SHOULD_SKIP | SHOULD_STOP;
100
+ function stop() {
101
+ this._traverseFlags |= _index.SHOULD_SKIP | _index.SHOULD_STOP;
78
102
  }
79
- export function setScope() {
103
+ function setScope() {
80
104
  if (this.opts && this.opts.noScope) return;
81
105
  let path = this.parentPath;
82
106
  if ((this.key === "key" || this.listKey === "decorators") && path.isMethod() || this.key === "discriminant" && path.isSwitchStatement()) {
@@ -91,7 +115,7 @@ export function setScope() {
91
115
  this.scope = this.getScope(target);
92
116
  if (this.scope) this.scope.init();
93
117
  }
94
- export function setContext(context) {
118
+ function setContext(context) {
95
119
  if (this.skipKeys != null) {
96
120
  this.skipKeys = {};
97
121
  }
@@ -104,18 +128,18 @@ export function setContext(context) {
104
128
  this.setScope();
105
129
  return this;
106
130
  }
107
- export function resync() {
131
+ function resync() {
108
132
  if (this.removed) return;
109
133
  this._resyncParent();
110
134
  this._resyncList();
111
135
  this._resyncKey();
112
136
  }
113
- export function _resyncParent() {
137
+ function _resyncParent() {
114
138
  if (this.parentPath) {
115
139
  this.parent = this.parentPath.node;
116
140
  }
117
141
  }
118
- export function _resyncKey() {
142
+ function _resyncKey() {
119
143
  if (!this.container) return;
120
144
  if (this.node === this.container[this.key]) {
121
145
  return;
@@ -137,18 +161,18 @@ export function _resyncKey() {
137
161
  }
138
162
  this.key = null;
139
163
  }
140
- export function _resyncList() {
164
+ function _resyncList() {
141
165
  if (!this.parent || !this.inList) return;
142
166
  const newContainer = this.parent[this.listKey];
143
167
  if (this.container === newContainer) return;
144
168
  this.container = newContainer || null;
145
169
  }
146
- export function _resyncRemoved() {
170
+ function _resyncRemoved() {
147
171
  if (this.key == null || !this.container || this.container[this.key] !== this.node) {
148
172
  this._markRemoved();
149
173
  }
150
174
  }
151
- export function popContext() {
175
+ function popContext() {
152
176
  this.contexts.pop();
153
177
  if (this.contexts.length > 0) {
154
178
  this.setContext(this.contexts[this.contexts.length - 1]);
@@ -156,23 +180,23 @@ export function popContext() {
156
180
  this.setContext(undefined);
157
181
  }
158
182
  }
159
- export function pushContext(context) {
183
+ function pushContext(context) {
160
184
  this.contexts.push(context);
161
185
  this.setContext(context);
162
186
  }
163
- export function setup(parentPath, container, listKey, key) {
187
+ function setup(parentPath, container, listKey, key) {
164
188
  this.listKey = listKey;
165
189
  this.container = container;
166
190
  this.parentPath = parentPath || this.parentPath;
167
191
  this.setKey(key);
168
192
  }
169
- export function setKey(key) {
193
+ function setKey(key) {
170
194
  var _this$node;
171
195
  this.key = key;
172
196
  this.node = this.container[this.key];
173
197
  this.type = (_this$node = this.node) == null ? void 0 : _this$node.type;
174
198
  }
175
- export function requeue(pathToQueue = this) {
199
+ function requeue(pathToQueue = this) {
176
200
  if (pathToQueue.removed) return;
177
201
  ;
178
202
  const contexts = this.contexts;
@@ -180,7 +204,7 @@ export function requeue(pathToQueue = this) {
180
204
  context.maybeQueue(pathToQueue);
181
205
  }
182
206
  }
183
- export function _getQueueContexts() {
207
+ function _getQueueContexts() {
184
208
  let path = this;
185
209
  let contexts = this.contexts;
186
210
  while (!contexts.length) {