| 1 |
// This file contains that retrieve or validate anything related to the current paths ancestry. |
| 2 |
|
| 3 |
import { VISITOR_KEYS } from "@babel/types"; |
| 4 |
import type * as t from "@babel/types"; |
| 5 |
import type NodePath from "./index"; |
| 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 findParent( |
| 15 |
this: NodePath, |
| 16 |
callback: (path: NodePath) => boolean, |
| 17 |
): NodePath | null { |
| 18 |
let path = this; |
| 19 |
while ((path = path.parentPath)) { |
| 20 |
if (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 find( |
| 32 |
this: NodePath, |
| 33 |
callback: (path: NodePath) => boolean, |
| 34 |
): NodePath | null { |
| 35 |
let path = this; |
| 36 |
do { |
| 37 |
if (callback(path)) return path; |
| 38 |
} while ((path = path.parentPath)); |
| 39 |
return null; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get the parent function of the current path. |
| 44 |
*/ |
| 45 |
|
| 46 |
export function getFunctionParent(this: NodePath): NodePath<t.Function> | null { |
| 47 |
return this.findParent(p => p.isFunction()) as NodePath<t.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 getStatementParent(this: NodePath): NodePath<t.Statement> { |
| 55 |
let path = this; |
| 56 |
|
| 57 |
do { |
| 58 |
if ( |
| 59 |
!path.parentPath || |
| 60 |
(Array.isArray(path.container) && path.isStatement()) |
| 61 |
) { |
| 62 |
break; |
| 63 |
} else { |
| 64 |
path = path.parentPath; |
| 65 |
} |
| 66 |
} while (path); |
| 67 |
|
| 68 |
if (path && (path.isProgram() || path.isFile())) { |
| 69 |
throw new Error( |
| 70 |
"File/Program node, we can't possibly find a statement parent to this", |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
return path as NodePath<t.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 getEarliestCommonAncestorFrom( |
| 86 |
this: NodePath, |
| 87 |
paths: Array<NodePath>, |
| 88 |
): NodePath { |
| 89 |
return this.getDeepestCommonAncestorFrom( |
| 90 |
paths, |
| 91 |
function (deepest, i, ancestries) { |
| 92 |
let earliest; |
| 93 |
const keys = VISITOR_KEYS[deepest.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.listKey && earliest.listKey === path.listKey) { |
| 106 |
// we're in the same container so check if we're earlier |
| 107 |
if (path.key < earliest.key) { |
| 108 |
earliest = path; |
| 109 |
continue; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// handle keys |
| 114 |
const earliestKeyIndex = keys.indexOf(earliest.parentKey); |
| •115 |
const currentKeyIndex = keys.indexOf(path.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 getDeepestCommonAncestorFrom( |
| 134 |
this: NodePath, |
| 135 |
paths: Array<NodePath>, |
| 136 |
filter?: (deepest: NodePath, i: number, ancestries: NodePath[][]) => NodePath, |
| 137 |
): NodePath { |
| 138 |
if (!paths.length) { |
| 139 |
return this; |
| 140 |
} |
| 141 |
|
| 142 |
if (paths.length === 1) { |
| 143 |
return paths[0]; |
| 144 |
} |
| 145 |
|
| 146 |
// minimum depth of the tree so we know the highest node |
| 147 |
let minDepth = 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 = paths.map(path => { |
| 154 |
const ancestry: NodePath[] = []; |
| 155 |
|
| 156 |
do { |
| 157 |
ancestry.unshift(path); |
| 158 |
} while ((path = path.parentPath) && path !== this); |
| 159 |
|
| 160 |
// save min depth to avoid going too far in |
| 161 |
if (ancestry.length < minDepth) { |
| 162 |
minDepth = ancestry.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 (filter) { |
| 189 |
return filter(lastCommon, lastCommonIndex, ancestries); |
| 190 |
} else { |
| 191 |
return lastCommon; |
| 192 |
} |
| 193 |
} else { |
| 194 |
throw new 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 getAncestry(this: NodePath): Array<NodePath> { |
| 205 |
let path = this; |
| 206 |
const paths = []; |
| 207 |
do { |
| 208 |
paths.push(path); |
| 209 |
} while ((path = path.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 isAncestor(this: NodePath, maybeDescendant: NodePath): boolean { |
| 217 |
return maybeDescendant.isDescendant(this); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* A helper to find if `this` path is a descendant of @param maybeAncestor |
| 222 |
*/ |
| 223 |
export function isDescendant(this: NodePath, maybeAncestor: NodePath): boolean { |
| 224 |
return !!this.findParent(parent => parent === maybeAncestor); |
| 225 |
} |
| 226 |
|
| 227 |
export function inType(this: NodePath, ...candidateTypes: string[]): boolean { |
| 228 |
let path = this; |
| 229 |
while (path) { |
| 230 |
for (const type of candidateTypes) { |
| 231 |
if (path.node.type === type) return true; |
| 232 |
} |
| 233 |
path = path.parentPath; |
| 234 |
} |
| 235 |
|
| 236 |
return false; |
| 237 |
} |