Debian Codegraph @

packages/babel-traverse/src/path/ancestry.ts

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // This file contains that retrieve or validate anything related to the current paths ancestry.
2
3 import { VISITOR_KEYSscip-typescript npm @babel/types 7.20.7 src/definitions/`utils.ts`/VISITOR_KEYS. } from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/;
4 import type * as tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/ from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/;
5 import type NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# from "./index"scip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/;
6
7 /**
8 * Starting at the parent path of the current `NodePath` and going up the
9 * tree, return the first `NodePath` that causes the provided `callback`
10 * to return a truthy value, or `null` if the `callback` never returns a
11 * truthy value.
12 */
13
14 export function findParentscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/findParent().(
15 thisscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/findParent().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#,
16 callbackscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/findParent().(callback): (path: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#) => boolean,
17 ): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# | null {
18 let path = this;
19 while ((path = path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath.)) {
20 if (callbackscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/findParent().(callback)(path)) return path;
21 }
22 return null;
23 }
24
25 /**
26 * Starting at current `NodePath` and going up the tree, return the first
27 * `NodePath` that causes the provided `callback` to return a truthy value,
28 * or `null` if the `callback` never returns a truthy value.
29 */
30
31 export function findscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/find().(
32 thisscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/find().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#,
33 callbackscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/find().(callback): (path: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#) => boolean,
34 ): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# | null {
35 let path = this;
36 do {
37 if (callbackscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/find().(callback)(path)) return path;
38 } while ((path = path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath.));
39 return null;
40 }
41
42 /**
43 * Get the parent function of the current path.
44 */
45
46 export function getFunctionParentscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getFunctionParent().(thisscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getFunctionParent().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Functionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Function#> | null {
47 return this.findParentscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/findParent().(p => p.isFunctionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isFunction().()) as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Functionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Function#> | null;
48 }
49
50 /**
51 * Walk up the tree until we hit a parent node path in a list.
52 */
53
54 export function getStatementParentscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getStatementParent().(thisscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getStatementParent().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Statementscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Statement#> {
55 let path = this;
56
57 do {
58 if (
59 !path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath. ||
60 (Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#.isArrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ArrayConstructor#isArray().(path.containerscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#container.) && path.isStatementscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isStatement().())
61 ) {
62 break;
63 } else {
64 path = path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath.;
65 }
66 } while (path);
67
68 if (path && (path.isProgramscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isProgram().() || path.isFilescip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isFile().())) {
69 throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#(
70 "File/Program node, we can't possibly find a statement parent to this",
71 );
72 }
73
74 return path as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Statementscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Statement#>;
75 }
76
77 /**
78 * Get the deepest common ancestor and then from it, get the earliest relationship path
79 * to that ancestor.
80 *
81 * Earliest is defined as being "before" all the other nodes in terms of list container
82 * position and visiting key.
83 */
84
85 export function getEarliestCommonAncestorFromscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getEarliestCommonAncestorFrom().(
86 thisscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getEarliestCommonAncestorFrom().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#,
87 pathsscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getEarliestCommonAncestorFrom().(paths): Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#<NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#>,
88 ): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# {
89 return this.getDeepestCommonAncestorFromscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(
90 pathsscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getEarliestCommonAncestorFrom().(paths),
91 function (deepest, i, ancestries) {
92 let earliest;
93 const keys = VISITOR_KEYSscip-typescript npm @babel/types 7.20.7 src/definitions/`utils.ts`/VISITOR_KEYS.[deepest.typescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#type.];
94
95 for (const ancestry of ancestries) {
96 const path = ancestry[i + 1];
97
98 // first path
99 if (!earliest) {
100 earliest = path;
101 continue;
102 }
103
104 // handle containers
105 if (path.listKeyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#listKey. && earliest.listKeyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#listKey. === path.listKeyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#listKey.) {
106 // we're in the same container so check if we're earlier
107 if (path.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key. < earliest.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key.) {
108 earliest = path;
109 continue;
110 }
111 }
112
113 // handle keys
114 const earliestKeyIndex = keys.indexOfscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#indexOf().(earliest.parentKeyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#`<get>parentKey`().);
•115 const currentKeyIndex = keys.indexOfscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#indexOf().(path.parentKeyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#`<get>parentKey`().);
116 if (earliestKeyIndex > currentKeyIndex) {
117 // key appears before so it's earlier
118 earliest = path;
119 }
120 }
121
122 return earliest;
123 },
124 );
125 }
126
127 /**
128 * Get the earliest path in the tree where the provided `paths` intersect.
129 *
130 * TODO: Possible optimisation target.
131 */
132
133 export function getDeepestCommonAncestorFromscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(
134 thisscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#,
135 pathsscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(paths): Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#<NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#>,
136 filterscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(filter)?: (deepest: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, i: number, ancestries: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#[][]) => NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#,
137 ): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# {
138 if (!pathsscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(paths).lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.) {
139 return this;
140 }
141
142 if (pathsscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(paths).lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. === 1) {
143 return pathsscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(paths)[0];
144 }
145
146 // minimum depth of the tree so we know the highest node
147 let minDepth = Infinityscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Infinity.;
148
149 // last common ancestor
150 let lastCommonIndex, lastCommon;
151
152 // get the ancestors of the path, breaking when the parent exceeds ourselves
153 const ancestries = pathsscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(paths).mapscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#map().(path => {
154 const ancestry: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#[] = [];
155
156 do {
157 ancestry.unshiftscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#unshift().(path);
158 } while ((path = path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath.) && path !== this);
159
160 // save min depth to avoid going too far in
161 if (ancestry.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. < minDepth) {
162 minDepth = ancestry.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.;
163 }
164
165 return ancestry;
166 });
167
168 // get the first ancestry so we have a seed to assess all other ancestries with
169 const first = ancestries[0];
170
171 // check ancestor equality
172 depthLoop: for (let i = 0; i < minDepth; i++) {
173 const shouldMatch = first[i];
174
175 for (const ancestry of ancestries) {
176 if (ancestry[i] !== shouldMatch) {
177 // we've hit a snag
178 break depthLoop;
179 }
180 }
181
182 // next iteration may break so store these so they can be returned
183 lastCommonIndex = i;
184 lastCommon = shouldMatch;
185 }
186
187 if (lastCommon) {
188 if (filterscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(filter)) {
189 return filterscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getDeepestCommonAncestorFrom().(filter)(lastCommon, lastCommonIndex, ancestries);
190 } else {
191 return lastCommon;
192 }
193 } else {
194 throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#("Couldn't find intersection");
195 }
196 }
197
198 /**
199 * Build an array of node paths containing the entire ancestry of the current node path.
200 *
201 * NOTE: The current node path is included in this.
202 */
203
204 export function getAncestryscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getAncestry().(thisscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getAncestry().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#): Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#<NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#> {
205 let path = this;
206 const paths = [];
207 do {
208 paths.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(path);
209 } while ((path = path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath.));
210 return paths;
211 }
212
213 /**
214 * A helper to find if `this` path is an ancestor of @param maybeDescendant
215 */
216 export function isAncestorscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/isAncestor().(thisscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/isAncestor().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, maybeDescendantscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/isAncestor().(maybeDescendant): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#): boolean {
217 return maybeDescendantscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/isAncestor().(maybeDescendant).isDescendantscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/isDescendant().(this);
218 }
219
220 /**
221 * A helper to find if `this` path is a descendant of @param maybeAncestor
222 */
223 export function isDescendantscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/isDescendant().(thisscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/isDescendant().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, maybeAncestorscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/isDescendant().(maybeAncestor): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#): boolean {
224 return !!this.findParentscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/findParent().(parent => parent === maybeAncestorscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/isDescendant().(maybeAncestor));
225 }
226
227 export function inTypescip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/inType().(thisscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/inType().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, ...candidateTypesscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/inType().(candidateTypes): string[]): boolean {
228 let path = this;
229 while (path) {
230 for (const type of candidateTypesscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/inType().(candidateTypes)) {
231 if (path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..typescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ArrayExpression#type. === type) return true;
232 }
233 path = path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath.;
234 }
235
236 return false;
237 }