@babel/traverse 7.20.0 → 7.20.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.

Files changed (57) hide show
  1. package/lib/cache.js +0 -3
  2. package/lib/cache.js.map +1 -1
  3. package/lib/context.js +7 -21
  4. package/lib/context.js.map +1 -1
  5. package/lib/hub.js +0 -6
  6. package/lib/hub.js.map +1 -1
  7. package/lib/index.js +3 -21
  8. package/lib/index.js.map +1 -1
  9. package/lib/path/ancestry.js +3 -23
  10. package/lib/path/ancestry.js.map +1 -1
  11. package/lib/path/comments.js +0 -4
  12. package/lib/path/comments.js.map +1 -1
  13. package/lib/path/context.js +14 -54
  14. package/lib/path/context.js.map +1 -1
  15. package/lib/path/conversion.js +23 -78
  16. package/lib/path/conversion.js.map +1 -1
  17. package/lib/path/evaluation.js +9 -85
  18. package/lib/path/evaluation.js.map +1 -1
  19. package/lib/path/family.js +6 -73
  20. package/lib/path/family.js.map +1 -1
  21. package/lib/path/index.js +2 -61
  22. package/lib/path/index.js.map +1 -1
  23. package/lib/path/inference/index.js +2 -27
  24. package/lib/path/inference/index.js.map +1 -1
  25. package/lib/path/inference/inferer-reference.js +10 -31
  26. package/lib/path/inference/inferer-reference.js.map +1 -1
  27. package/lib/path/inference/inferers.js +4 -44
  28. package/lib/path/inference/inferers.js.map +1 -1
  29. package/lib/path/inference/util.js +0 -4
  30. package/lib/path/inference/util.js.map +1 -1
  31. package/lib/path/introspection.js +17 -59
  32. package/lib/path/introspection.js.map +1 -1
  33. package/lib/path/lib/hoister.js +7 -23
  34. package/lib/path/lib/hoister.js.map +1 -1
  35. package/lib/path/lib/removal-hooks.js +7 -3
  36. package/lib/path/lib/removal-hooks.js.map +1 -1
  37. package/lib/path/lib/virtual-types-validator.js +0 -23
  38. package/lib/path/lib/virtual-types-validator.js.map +1 -1
  39. package/lib/path/lib/virtual-types.js +1 -0
  40. package/lib/path/lib/virtual-types.js.map +1 -1
  41. package/lib/path/modification.js +12 -47
  42. package/lib/path/modification.js.map +1 -1
  43. package/lib/path/removal.js +0 -16
  44. package/lib/path/removal.js.map +1 -1
  45. package/lib/path/replacement.js +8 -50
  46. package/lib/path/replacement.js.map +1 -1
  47. package/lib/scope/binding.js +2 -14
  48. package/lib/scope/binding.js.map +1 -1
  49. package/lib/scope/index.js +19 -184
  50. package/lib/scope/index.js.map +1 -1
  51. package/lib/scope/lib/renamer.js +2 -25
  52. package/lib/scope/lib/renamer.js.map +1 -1
  53. package/lib/traverse-node.js +0 -7
  54. package/lib/traverse-node.js.map +1 -1
  55. package/lib/visitors.js +13 -44
  56. package/lib/visitors.js.map +1 -1
  57. package/package.json +4 -4
package/lib/cache.js CHANGED
@@ -11,16 +11,13 @@ let path = new WeakMap();
11
11
  exports.path = path;
12
12
  let scope = new WeakMap();
13
13
  exports.scope = scope;
14
-
15
14
  function clear() {
16
15
  clearPath();
17
16
  clearScope();
18
17
  }
19
-
20
18
  function clearPath() {
21
19
  exports.path = path = new WeakMap();
22
20
  }
23
-
24
21
  function clearScope() {
25
22
  exports.scope = scope = new WeakMap();
26
23
  }
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":";;;;;;;;;AAAO,IAAIA,IAAI,GAAG,IAAIC,OAAJ,EAAX;;AACA,IAAIC,KAAK,GAAG,IAAID,OAAJ,EAAZ;;;AAEA,SAASE,KAAT,GAAiB;EACtBC,SAAS;EACTC,UAAU;AACX;;AAEM,SAASD,SAAT,GAAqB;EAC1B,eAAAJ,IAAI,GAAG,IAAIC,OAAJ,EAAP;AACD;;AAEM,SAASI,UAAT,GAAsB;EAC3B,gBAAAH,KAAK,GAAG,IAAID,OAAJ,EAAR;AACD"}
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":";;;;;;;;;AAAO,IAAIA,IAAI,GAAG,IAAIC,OAAO,EAAE;AAAC;AACzB,IAAIC,KAAK,GAAG,IAAID,OAAO,EAAE;AAAC;AAE1B,SAASE,KAAK,GAAG;EACtBC,SAAS,EAAE;EACXC,UAAU,EAAE;AACd;AAEO,SAASD,SAAS,GAAG;EAC1B,eAAAJ,IAAI,GAAG,IAAIC,OAAO,EAAE;AACtB;AAEO,SAASI,UAAU,GAAG;EAC3B,gBAAAH,KAAK,GAAG,IAAID,OAAO,EAAE;AACvB"}
package/lib/context.js CHANGED
@@ -4,15 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _path = require("./path");
9
-
10
8
  var _t = require("@babel/types");
11
-
12
9
  const {
13
10
  VISITOR_KEYS
14
11
  } = _t;
15
-
16
12
  class TraversalContext {
17
13
  constructor(scope, opts, state, parentPath) {
18
14
  this.queue = null;
@@ -26,19 +22,20 @@ class TraversalContext {
26
22
  shouldVisit(node) {
27
23
  const opts = this.opts;
28
24
  if (opts.enter || opts.exit) return true;
25
+
29
26
  if (opts[node.type]) return true;
27
+
30
28
  const keys = VISITOR_KEYS[node.type];
31
29
  if (!(keys != null && keys.length)) return false;
32
30
 
33
31
  for (const key of keys) {
34
- if (node[key]) {
32
+ if (
33
+ node[key]) {
35
34
  return true;
36
35
  }
37
36
  }
38
-
39
37
  return false;
40
38
  }
41
-
42
39
  create(node, container, key, listKey) {
43
40
  return _path.default.get({
44
41
  parentPath: this.parentPath,
@@ -48,7 +45,6 @@ class TraversalContext {
48
45
  listKey
49
46
  });
50
47
  }
51
-
52
48
  maybeQueue(path, notPriority) {
53
49
  if (this.queue) {
54
50
  if (notPriority) {
@@ -58,30 +54,26 @@ class TraversalContext {
58
54
  }
59
55
  }
60
56
  }
61
-
62
57
  visitMultiple(container, parent, listKey) {
63
58
  if (container.length === 0) return false;
64
59
  const queue = [];
65
60
 
66
61
  for (let key = 0; key < container.length; key++) {
67
62
  const node = container[key];
68
-
69
63
  if (node && this.shouldVisit(node)) {
70
64
  queue.push(this.create(parent, container, key, listKey));
71
65
  }
72
66
  }
73
-
74
67
  return this.visitQueue(queue);
75
68
  }
76
-
77
69
  visitSingle(node, key) {
78
- if (this.shouldVisit(node[key])) {
70
+ if (this.shouldVisit(
71
+ node[key])) {
79
72
  return this.visitQueue([this.create(node, node, key)]);
80
73
  } else {
81
74
  return false;
82
75
  }
83
76
  }
84
-
85
77
  visitQueue(queue) {
86
78
  this.queue = queue;
87
79
  this.priorityQueue = [];
@@ -90,23 +82,21 @@ class TraversalContext {
90
82
 
91
83
  for (const path of queue) {
92
84
  path.resync();
93
-
94
85
  if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
95
86
  path.pushContext(this);
96
87
  }
97
88
 
98
89
  if (path.key === null) continue;
90
+
99
91
  const {
100
92
  node
101
93
  } = path;
102
94
  if (visited.has(node)) continue;
103
95
  if (node) visited.add(node);
104
-
105
96
  if (path.visit()) {
106
97
  stop = true;
107
98
  break;
108
99
  }
109
-
110
100
  if (this.priorityQueue.length) {
111
101
  stop = this.visitQueue(this.priorityQueue);
112
102
  this.priorityQueue = [];
@@ -122,20 +112,16 @@ class TraversalContext {
122
112
  this.queue = null;
123
113
  return stop;
124
114
  }
125
-
126
115
  visit(node, key) {
127
116
  const nodes = node[key];
128
117
  if (!nodes) return false;
129
-
130
118
  if (Array.isArray(nodes)) {
131
119
  return this.visitMultiple(nodes, node, key);
132
120
  } else {
133
121
  return this.visitSingle(node, key);
134
122
  }
135
123
  }
136
-
137
124
  }
138
-
139
125
  exports.default = TraversalContext;
140
126
 
141
127
  //# sourceMappingURL=context.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["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"],"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,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AA1KgD"}
1
+ {"version":3,"names":["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"],"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;AACA;AAA4C;EAAnCA;AAAY;AAMN,MAAMC,gBAAgB,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@@ -4,22 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  class Hub {
9
8
  getCode() {}
10
-
11
9
  getScope() {}
12
-
13
10
  addHelper() {
14
11
  throw new Error("Helpers are not supported by the default hub.");
15
12
  }
16
-
17
13
  buildError(node, msg, Error = TypeError) {
18
14
  return new Error(msg);
19
15
  }
20
-
21
16
  }
22
-
23
17
  exports.default = Hub;
24
18
 
25
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":";;;;;;;AAUe,MAAMA,GAAN,CAAkC;EAC/CC,OAAO,GAAG,CAAE;;EAEZC,QAAQ,GAAG,CAAE;;EAEbC,SAAS,GAAG;IACV,MAAM,IAAIC,KAAJ,CAAU,+CAAV,CAAN;EACD;;EAEDC,UAAU,CAACC,IAAD,EAAaC,GAAb,EAA0BH,KAAK,GAAGI,SAAlC,EAAoD;IAC5D,OAAO,IAAIJ,KAAJ,CAAUG,GAAV,CAAP;EACD;;AAX8C"}
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":";;;;;;AAUe,MAAMA,GAAG,CAAyB;EAC/CC,OAAO,GAAG,CAAC;EAEXC,QAAQ,GAAG,CAAC;EAEZC,SAAS,GAAG;IACV,MAAM,IAAIC,KAAK,CAAC,+CAA+C,CAAC;EAClE;EAEAC,UAAU,CAACC,IAAU,EAAEC,GAAW,EAAEH,KAAK,GAAGI,SAAS,EAAS;IAC5D,OAAO,IAAIJ,KAAK,CAACG,GAAG,CAAC;EACvB;AACF;AAAC"}
package/lib/index.js CHANGED
@@ -22,56 +22,41 @@ Object.defineProperty(exports, "Scope", {
22
22
  }
23
23
  });
24
24
  exports.visitors = exports.default = void 0;
25
-
26
25
  var visitors = require("./visitors");
27
-
28
26
  exports.visitors = visitors;
29
-
30
27
  var _t = require("@babel/types");
31
-
32
28
  var cache = require("./cache");
33
-
34
29
  var _traverseNode = require("./traverse-node");
35
-
36
30
  var _path = require("./path");
37
-
38
31
  var _scope = require("./scope");
39
-
40
32
  var _hub = require("./hub");
41
-
42
33
  const {
43
34
  VISITOR_KEYS,
44
35
  removeProperties,
45
36
  traverseFast
46
37
  } = _t;
47
-
48
- function traverse(parent, opts = {}, scope, state, parentPath) {
38
+ function traverse(parent,
39
+ opts = {}, scope, state, parentPath) {
49
40
  if (!parent) return;
50
-
51
41
  if (!opts.noScope && !scope) {
52
42
  if (parent.type !== "Program" && parent.type !== "File") {
53
43
  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.");
54
44
  }
55
45
  }
56
-
57
46
  if (!VISITOR_KEYS[parent.type]) {
58
47
  return;
59
48
  }
60
-
61
49
  visitors.explode(opts);
62
50
  (0, _traverseNode.traverseNode)(parent, opts, scope, state, parentPath);
63
51
  }
64
-
65
52
  var _default = traverse;
66
53
  exports.default = _default;
67
54
  traverse.visitors = visitors;
68
55
  traverse.verify = visitors.verify;
69
56
  traverse.explode = visitors.explode;
70
-
71
57
  traverse.cheap = function (node, enter) {
72
58
  return traverseFast(node, enter);
73
59
  };
74
-
75
60
  traverse.node = function (node, opts, scope, state, path, skipKeys) {
76
61
  (0, _traverseNode.traverseNode)(node, opts, scope, state, path, skipKeys);
77
62
  };
@@ -80,21 +65,19 @@ traverse.clearNode = function (node, opts) {
80
65
  removeProperties(node, opts);
81
66
  cache.path.delete(node);
82
67
  };
83
-
84
68
  traverse.removeProperties = function (tree, opts) {
85
69
  traverseFast(tree, traverse.clearNode, opts);
86
70
  return tree;
87
71
  };
88
-
89
72
  function hasDenylistedType(path, state) {
90
73
  if (path.node.type === state.type) {
91
74
  state.has = true;
92
75
  path.stop();
93
76
  }
94
77
  }
95
-
96
78
  traverse.hasType = function (tree, type, denylistTypes) {
97
79
  if (denylistTypes != null && denylistTypes.includes(tree.type)) return false;
80
+
98
81
  if (tree.type === type) return true;
99
82
  const state = {
100
83
  has: false,
@@ -107,7 +90,6 @@ traverse.hasType = function (tree, type, denylistTypes) {
107
90
  }, null, state);
108
91
  return state.has;
109
92
  };
110
-
111
93
  traverse.cache = cache;
112
94
 
113
95
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["VISITOR_KEYS","removeProperties","traverseFast","traverse","parent","opts","scope","state","parentPath","noScope","type","Error","visitors","explode","traverseNode","verify","cheap","node","enter","path","skipKeys","clearNode","cache","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 return traverseFast(node, enter);\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;;;;AACA;;AAOA;;AAIA;;AAGA;;AACA;;AACA;;;EAfEA,Y;EACAC,gB;EAEAC;;;AAuCF,SAASC,QAAT,CACEC,MADF,EAGEC,IAAa,GAAG,EAHlB,EAIEC,KAJF,EAKEC,KALF,EAMEC,UANF,EAOE;EACA,IAAI,CAACJ,MAAL,EAAa;;EAEb,IAAI,CAACC,IAAI,CAACI,OAAN,IAAiB,CAACH,KAAtB,EAA6B;IAC3B,IAAIF,MAAM,CAACM,IAAP,KAAgB,SAAhB,IAA6BN,MAAM,CAACM,IAAP,KAAgB,MAAjD,EAAyD;MACvD,MAAM,IAAIC,KAAJ,CACJ,4EACG,2CAA0CP,MAAM,CAACM,IAAK,gBADzD,GAEE,+BAHE,CAAN;IAKD;EACF;;EAED,IAAI,CAACV,YAAY,CAACI,MAAM,CAACM,IAAR,CAAjB,EAAgC;IAC9B;EACD;;EAEDE,QAAQ,CAACC,OAAT,CAAiBR,IAAjB;EAEA,IAAAS,0BAAA,EAAaV,M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,GAAmB,UACjBJ,IADiB,EAEjBd,IAFiB,EAGjBmB,aAHiB,EAIR;EAET,IAAIA,aAAJ,YAAIA,aAAa,CAAEC,QAAf,CAAwBN,IAAI,CAACd,IAA7B,CAAJ,EAAwC,OAAO,KAAP;EAGxC,IAAIc,IAAI,CAACd,IAAL,KAAcA,IAAlB,EAAwB,OAAO,IAAP;EAExB,MAAMH,KAA6B,GAAG;IACpCmB,GAAG,EAAE,KAD+B;IAEpChB,IAAI,EAAEA;EAF8B,CAAtC;EAKAP,QAAQ,CACNqB,IADM,EAEN;IACEf,OAAO,EAAE,IADX;IAEEsB,QAAQ,EAAEF,aAFZ;IAGEX,KAAK,EAAEO;EAHT,CAFM,EAON,IAPM,EAQNlB,KARM,CAAR;EAWA,OAAOA,KAAK,CAACmB,GAAb;AACD,CA5BD;;AA8BAvB,QAAQ,CAACmB,KAAT,GAAiBA,KAAjB"}
1
+ {"version":3,"names":["VISITOR_KEYS","removeProperties","traverseFast","traverse","parent","opts","scope","state","parentPath","noScope","type","Error","visitors","explode","traverseNode","verify","cheap","node","enter","path","skipKeys","clearNode","cache","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 return traverseFast(node, enter);\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;AAAuC;AACvC;AAOA;AAIA;AAGA;AACA;AACA;AAAuC;EAfrCA,YAAY;EACZC,gBAAgB;EAEhBC;AAAY;AAuCd,SAASC,QAAQ,CACfC,MAAc;AAEdC,IAAa,GAAG,CAAC,CAAC,EAClBC,KAAa,EACbC,KAAW,EACXC,UAAqB,EACrB;EACA,IAAI,CAACJ,MAAM,EAAE;EAEb,IAAI,CAACC,IAAI,CAACI,OAAO,IAAI,CAACH,KAAK,EAAE;IAC3B,IAAIF,MAAM,CAACM,IAAI,KAAK,SAAS,IAAIN,MAAM,CAACM,IAAI,KAAK,MAAM,EAAE;MACvD,MAAM,IAAIC,KAAK,CACb,yEAAyE,GACtE,2CAA0CP,MAAM,CAACM,IAAK,gBAAe,GACtE,+BAA+B,CAClC;IACH;EACF;EAEA,IAAI,CAACV,YAAY,CAACI,MAAM,CAACM,IAAI,CAAC,EAAE;IAC9B;EACF;EAEAE,QAAQ,CAACC,OAAO,CAACR,IAAI,CAAY;EAEjC,IAAAS,0BAAY,EAACV,MAAM,EAAEC,IAAI,EAAEC,KAAK,EAAEC,KAAK,EAAEC,UAAU,CAAC;AACtD;AAAC,eAEcL,QAAQ;AAAA;AAEvBA,QAAQ,CAACS,QAAQ,GAAGA,QAAQ;AAC5BT,QAAQ,CAACY,MAAM,GAAGH,QAAQ,CAACG,MAAM;AACjCZ,QAAQ,CAACU,OAAO,GAAGD,QAAQ,CAACC,OAAO;AAEnCV,QAAQ,CAACa,KAAK,GAAG,UAAUC,IAAY,EAAEC,KAA6B,EAAE;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I,EACJ;IACEf,OAAO,EAAE,IAAI;IACbsB,QAAQ,EAAEF,aAAa;IACvBX,KAAK,EAAEO;EACT,CAAC,EACD,IAAI,EACJlB,KAAK,CACN;EAED,OAAOA,KAAK,CAACmB,GAAG;AAClB,CAAC;AAEDvB,QAAQ,CAACmB,KAAK,GAAGA,KAAK"}
@@ -13,30 +13,24 @@ exports.getStatementParent = getStatementParent;
13
13
  exports.inType = inType;
14
14
  exports.isAncestor = isAncestor;
15
15
  exports.isDescendant = isDescendant;
16
-
17
16
  var _t = require("@babel/types");
18
-
19
17
  const {
20
18
  VISITOR_KEYS
21
19
  } = _t;
22
20
 
23
21
  function findParent(callback) {
24
22
  let path = this;
25
-
26
23
  while (path = path.parentPath) {
27
24
  if (callback(path)) return path;
28
25
  }
29
-
30
26
  return null;
31
27
  }
32
28
 
33
29
  function find(callback) {
34
30
  let path = this;
35
-
36
31
  do {
37
32
  if (callback(path)) return path;
38
33
  } while (path = path.parentPath);
39
-
40
34
  return null;
41
35
  }
42
36
 
@@ -46,7 +40,6 @@ function getFunctionParent() {
46
40
 
47
41
  function getStatementParent() {
48
42
  let path = this;
49
-
50
43
  do {
51
44
  if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
52
45
  break;
@@ -54,11 +47,9 @@ function getStatementParent() {
54
47
  path = path.parentPath;
55
48
  }
56
49
  } while (path);
57
-
58
50
  if (path && (path.isProgram() || path.isFile())) {
59
51
  throw new Error("File/Program node, we can't possibly find a statement parent to this");
60
52
  }
61
-
62
53
  return path;
63
54
  }
64
55
 
@@ -66,7 +57,6 @@ function getEarliestCommonAncestorFrom(paths) {
66
57
  return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
67
58
  let earliest;
68
59
  const keys = VISITOR_KEYS[deepest.type];
69
-
70
60
  for (const ancestry of ancestries) {
71
61
  const path = ancestry[i + 1];
72
62
 
@@ -84,12 +74,10 @@ function getEarliestCommonAncestorFrom(paths) {
84
74
 
85
75
  const earliestKeyIndex = keys.indexOf(earliest.parentKey);
86
76
  const currentKeyIndex = keys.indexOf(path.parentKey);
87
-
88
77
  if (earliestKeyIndex > currentKeyIndex) {
89
78
  earliest = path;
90
79
  }
91
80
  }
92
-
93
81
  return earliest;
94
82
  });
95
83
  }
@@ -98,16 +86,16 @@ function getDeepestCommonAncestorFrom(paths, filter) {
98
86
  if (!paths.length) {
99
87
  return this;
100
88
  }
101
-
102
89
  if (paths.length === 1) {
103
90
  return paths[0];
104
91
  }
105
92
 
106
93
  let minDepth = Infinity;
94
+
107
95
  let lastCommonIndex, lastCommon;
96
+
108
97
  const ancestries = paths.map(path => {
109
98
  const ancestry = [];
110
-
111
99
  do {
112
100
  ancestry.unshift(path);
113
101
  } while ((path = path.parentPath) && path !== this);
@@ -115,14 +103,13 @@ function getDeepestCommonAncestorFrom(paths, filter) {
115
103
  if (ancestry.length < minDepth) {
116
104
  minDepth = ancestry.length;
117
105
  }
118
-
119
106
  return ancestry;
120
107
  });
108
+
121
109
  const first = ancestries[0];
122
110
 
123
111
  depthLoop: for (let i = 0; i < minDepth; i++) {
124
112
  const shouldMatch = first[i];
125
-
126
113
  for (const ancestry of ancestries) {
127
114
  if (ancestry[i] !== shouldMatch) {
128
115
  break depthLoop;
@@ -132,7 +119,6 @@ function getDeepestCommonAncestorFrom(paths, filter) {
132
119
  lastCommonIndex = i;
133
120
  lastCommon = shouldMatch;
134
121
  }
135
-
136
122
  if (lastCommon) {
137
123
  if (filter) {
138
124
  return filter(lastCommon, lastCommonIndex, ancestries);
@@ -147,11 +133,9 @@ function getDeepestCommonAncestorFrom(paths, filter) {
147
133
  function getAncestry() {
148
134
  let path = this;
149
135
  const paths = [];
150
-
151
136
  do {
152
137
  paths.push(path);
153
138
  } while (path = path.parentPath);
154
-
155
139
  return paths;
156
140
  }
157
141
 
@@ -162,18 +146,14 @@ function isAncestor(maybeDescendant) {
162
146
  function isDescendant(maybeAncestor) {
163
147
  return !!this.findParent(parent => parent === maybeAncestor);
164
148
  }
165
-
166
149
  function inType(...candidateTypes) {
167
150
  let path = this;
168
-
169
151
  while (path) {
170
152
  for (const type of candidateTypes) {
171
153
  if (path.node.type === type) return true;
172
154
  }
173
-
174
155
  path = path.parentPath;
175
156
  }
176
-
177
157
  return false;
178
158
  }
179
159
 
@@ -1 +1 @@
1
- {"version":3,"names":["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 as string);\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;;;EAASA;;;AAWF,SAASC,UAAT,CAELC,QAFK,EAGY;EACjB,IAAIC,IAAI,GAAG,IAAX;;EACA,OAAQA,IAAI,GAAGA,IAAI,CAACC,UAApB,EAAiC;IAC/B,IAAIF,QAAQ,C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jC,IAAIjD,IAAI,CAACkD,IAAL,CAAU5B,IAAV,KAAmBA,IAAvB,EAA6B,OAAO,IAAP;IAC9B;;IACDtB,IAAI,GAAGA,IAAI,CAACC,UAAZ;EACD;;EAED,OAAO,KAAP;AACD"}
1
+ {"version":3,"names":["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 as string);\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;AAA4C;EAAnCA;AAAY;;AAWd,SAASC,UAAU,CAExBC,QAAqC,EACpB;EACjB,IAAIC,IAAI,GAAG,IAAI;EACf,OAAQA,IAAI,GAAGA,IAAI,CAACC,UAAU,EAAG;IAC/B,IAAIF,QAAQ,CAACC,IAAI,CAAC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D,IAAI,CAAC5B,IAAI,KAAKA,IAAI,EAAE,OAAO,IAAI;IAC1C;IACAtB,IAAI,GAAGA,IAAI,CAACC,UAAU;EACxB;EAEA,OAAO,KAAK;AACd"}
@@ -6,9 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.addComment = addComment;
7
7
  exports.addComments = addComments;
8
8
  exports.shareCommentsWithSiblings = shareCommentsWithSiblings;
9
-
10
9
  var _t = require("@babel/types");
11
-
12
10
  const {
13
11
  addComment: _addComment,
14
12
  addComments: _addComments
@@ -25,14 +23,12 @@ function shareCommentsWithSiblings() {
25
23
  const next = this.getSibling(this.key + 1);
26
24
  const hasPrev = Boolean(prev.node);
27
25
  const hasNext = Boolean(next.node);
28
-
29
26
  if (hasPrev && !hasNext) {
30
27
  prev.addComments("trailing", trailing);
31
28
  } else if (hasNext && !hasPrev) {
32
29
  next.addComments("leading", leading);
33
30
  }
34
31
  }
35
-
36
32
  function addComment(type, content, line) {
37
33
  _addComment(this.node, type, content, line);
38
34
  }
@@ -1 +1 @@
1
- {"version":3,"names":["addComment","_addComment","addComments","_addComments","shareCommentsWithSiblings","key","node","trailing","trailingComments","leading","leadingComments","prev","getSibling","next","hasPrev","Boolean","hasNext","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 if (hasPrev && !hasNext) {\n prev.addComments(\"trailing\", trailing);\n } else if (hasNext && !hasPrev) {\n next.addComments(\"leading\", leading);\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;;;EACEA,U,EAAcC,W;EACdC,W,EAAeC;;;AAOV,SAASC,yBAAT,GAAmD;EAExD,IAAI,OAAO,KAAKC,GAAZ,KAAoB,QAAxB,EAAkC;EAElC,MAAMC,IAAI,GAAG,KAAKA,IAAlB;EACA,IAAI,CAACA,IAAL,EAAW;EAEX,MAAMC,QAAQ,GAAGD,IAAI,CAACE,gBAAtB;EACA,MAAMC,OAAO,GAAGH,IAAI,CAACI,eAArB;EACA,IAAI,CAACH,QAAD,IAAa,CAACE,OAAlB,EAA2B;EAE3B,MAAME,IAAI,GAAG,KAAKC,UAAL,CAAgB,KAAKP,GAAL,GAAW,CAA3B,CAAb;EACA,MAAMQ,IAAI,GAAG,KAAKD,UAAL,CAAgB,KAAKP,GAAL,GAAW,CAA3B,CAAb;EACA,MAAMS,OAAO,GAAGC,OAAO,CAACJ,IAAI,CAACL,IAAN,CAAvB;EACA,MAAMU,OAAO,GAAGD,OAAO,CAACF,IAAI,CAACP,IAAN,CAAvB;;EACA,IAAIQ,OAAO,IAAI,CAACE,OAAhB,EAAyB;IACvBL,IAAI,CAACT,WAAL,CAAiB,UAAjB,EAA6BK,QAA7B;EACD,CAFD,MAEO,IAAIS,OAAO,IAAI,CAACF,OAAhB,EAAyB;IAC9BD,IAAI,CAACX,WAAL,CAAiB,SAAjB,EAA4BO,OAA5B;EACD;AACF;;AAEM,SAAST,UAAT,CAELiB,IAFK,EAGLC,OAHK,EAILC,IAJK,EAKL;EACAlB,WAAW,CAAC,KAAKK,IAAN,EAAYW,IAAZ,EAAkBC,OAAlB,EAA2BC,IAA3B,CAAX;AACD;;AAMM,SAASjB,WAAT,CAELe,IAFK,EAGLG,QAHK,EAIL;EACAjB,YAAY,CAAC,KAAKG,IAAN,EAAYW,IAAZ,EAAkBG,QAAlB,CAAZ;AACD"}
1
+ {"version":3,"names":["addComment","_addComment","addComments","_addComments","shareCommentsWithSiblings","key","node","trailing","trailingComments","leading","leadingComments","prev","getSibling","next","hasPrev","Boolean","hasNext","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 if (hasPrev && !hasNext) {\n prev.addComments(\"trailing\", trailing);\n } else if (hasNext && !hasPrev) {\n next.addComments(\"leading\", leading);\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;AAGsB;EAFpBA,UAAU,EAAIC,WAAW;EACzBC,WAAW,EAAIC;AAAY;;AAOtB,SAASC,yBAAyB,GAAiB;EAExD,IAAI,OAAO,IAAI,CAACC,GAAG,KAAK,QAAQ,EAAE;EAElC,MAAMC,IAAI,GAAG,IAAI,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