(ast, node)
| 44 | * @returns {undefined | Node[]} result |
| 45 | */ |
| 46 | const getPathInAst = (ast, node) => { |
| 47 | if (ast === node) { |
| 48 | return []; |
| 49 | } |
| 50 | |
| 51 | const nr = /** @type {Range} */ (node.range); |
| 52 | |
| 53 | /** |
| 54 | * Returns result. |
| 55 | * @param {Node} n node |
| 56 | * @returns {Node[] | undefined} result |
| 57 | */ |
| 58 | const enterNode = (n) => { |
| 59 | if (!n) return; |
| 60 | const r = n.range; |
| 61 | if (r && r[0] <= nr[0] && r[1] >= nr[1]) { |
| 62 | const path = getPathInAst(n, node); |
| 63 | if (path) { |
| 64 | path.push(n); |
| 65 | return path; |
| 66 | } |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | if (Array.isArray(ast)) { |
| 71 | // sibling ranges are ordered and disjoint; binary search the container |
| 72 | let lo = 0; |
| 73 | let hi = ast.length - 1; |
| 74 | while (lo <= hi) { |
| 75 | const mid = (lo + hi) >> 1; |
| 76 | const item = ast[mid]; |
| 77 | const r = item && item.range; |
| 78 | if (!r) { |
| 79 | // holes or range-less nodes: scan the remaining window linearly |
| 80 | for (let i = lo; i <= hi; i++) { |
| 81 | const enterResult = enterNode(ast[i]); |
| 82 | if (enterResult !== undefined) return enterResult; |
| 83 | } |
| 84 | return; |
| 85 | } |
| 86 | if (r[0] > nr[0]) { |
| 87 | hi = mid - 1; |
| 88 | } else if (nr[0] >= r[1]) { |
| 89 | lo = mid + 1; |
| 90 | } else { |
| 91 | return r[1] >= nr[1] ? enterNode(item) : undefined; |
| 92 | } |
| 93 | } |
| 94 | } else if (ast && typeof ast === "object") { |
| 95 | const keys = |
| 96 | /** @type {(keyof Node)[]} */ |
| 97 | (Object.keys(ast)); |
| 98 | for (let i = 0; i < keys.length; i++) { |
| 99 | // We are making the faster check in `enterNode` using `n.range` |
| 100 | const value = |
| 101 | ast[ |
| 102 | /** @type {Exclude<keyof Node, "range" | "loc" | "leadingComments" | "trailingComments">} */ |
| 103 | (keys[i]) |
no test coverage detected