| 1 |
import { VISITOR_KEYS } from "../definitions"; |
| 2 |
import type * as t from ".."; |
| 3 |
|
| 4 |
/** |
| 5 |
* A prefix AST traversal implementation meant for simple searching |
| 6 |
* and processing. |
| 7 |
*/ |
| 8 |
export default function traverseFast<Options = {}>( |
| 9 |
node: t.Node | null | undefined, |
| 10 |
enter: (node: t.Node, opts?: Options) => void, |
| 11 |
opts?: Options, |
| 12 |
): void { |
| 13 |
if (!node) return; |
| 14 |
|
| 15 |
const keys = VISITOR_KEYS[node.type]; |
| 16 |
if (!keys) return; |
| 17 |
|
| 18 |
opts = opts || ({} as Options); |
| 19 |
enter(node, opts); |
| 20 |
|
| 21 |
for (const key of keys) { |
| 22 |
const subNode: t.Node | undefined | null = |
| 23 |
// @ts-expect-error key must present in node |
| 24 |
node[key]; |
| 25 |
|
| 26 |
if (Array.isArray(subNode)) { |
| 27 |
for (const node of subNode) { |
| 28 |
traverseFast(node, enter, opts); |
| 29 |
} |
| 30 |
} else { |
| 31 |
traverseFast(subNode, enter, opts); |
| 32 |
} |
| 33 |
} |
| 34 |
} |