|
| 1 | +import * as acorn from "acorn" |
| 2 | + |
| 3 | +export type FullWalkerCallback<TState> = ( |
| 4 | + node: acorn.Node, |
| 5 | + state: TState, |
| 6 | + type: string |
| 7 | +) => void |
| 8 | + |
| 9 | +export type FullAncestorWalkerCallback<TState> = ( |
| 10 | + node: acorn.Node, |
| 11 | + state: TState, |
| 12 | + ancestors: acorn.Node[], |
| 13 | + type: string |
| 14 | +) => void |
| 15 | + |
| 16 | +type AggregateType = { |
| 17 | + Expression: acorn.Expression, |
| 18 | + Statement: acorn.Statement, |
| 19 | + Pattern: acorn.Pattern, |
| 20 | + ForInit: acorn.VariableDeclaration | acorn.Expression |
| 21 | +} |
| 22 | + |
| 23 | +export type SimpleVisitors<TState> = { |
| 24 | + [type in acorn.AnyNode["type"]]?: (node: Extract<acorn.AnyNode, { type: type }>, state: TState) => void |
| 25 | +} & { |
| 26 | + [type in keyof AggregateType]?: (node: AggregateType[type], state: TState) => void |
| 27 | +} |
| 28 | + |
| 29 | +export type AncestorVisitors<TState> = { |
| 30 | + [type in acorn.AnyNode["type"]]?: ( node: Extract<acorn.AnyNode, { type: type }>, state: TState, ancestors: acorn.Node[] |
| 31 | +) => void |
| 32 | +} & { |
| 33 | + [type in keyof AggregateType]?: (node: AggregateType[type], state: TState, ancestors: acorn.Node[]) => void |
| 34 | +} |
| 35 | + |
| 36 | +export type WalkerCallback<TState> = (node: acorn.Node, state: TState) => void |
| 37 | + |
| 38 | +export type RecursiveVisitors<TState> = { |
| 39 | + [type in acorn.AnyNode["type"]]?: ( node: Extract<acorn.AnyNode, { type: type }>, state: TState, callback: WalkerCallback<TState>) => void |
| 40 | +} & { |
| 41 | + [type in keyof AggregateType]?: (node: AggregateType[type], state: TState, callback: WalkerCallback<TState>) => void |
| 42 | +} |
| 43 | + |
| 44 | +export type FindPredicate = (type: string, node: acorn.Node) => boolean |
| 45 | + |
| 46 | +export interface Found<TState> { |
| 47 | + node: acorn.Node, |
| 48 | + state: TState |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * does a 'simple' walk over a tree |
| 53 | + * @param node the AST node to walk |
| 54 | + * @param visitors an object with properties whose names correspond to node types in the {@link https://github.com/estree/estree | ESTree spec}. The properties should contain functions that will be called with the node object and, if applicable the state at that point. |
| 55 | + * @param base a walker algorithm |
| 56 | + * @param state a start state. The default walker will simply visit all statements and expressions and not produce a meaningful state. (An example of a use of state is to track scope at each point in the tree.) |
| 57 | + */ |
| 58 | +export function simple<TState>( |
| 59 | + node: acorn.Node, |
| 60 | + visitors: SimpleVisitors<TState>, |
| 61 | + base?: RecursiveVisitors<TState>, |
| 62 | + state?: TState |
| 63 | +): void |
| 64 | + |
| 65 | +/** |
| 66 | + * does a 'simple' walk over a tree, building up an array of ancestor nodes (including the current node) and passing the array to the callbacks as a third parameter. |
| 67 | + * @param node |
| 68 | + * @param visitors |
| 69 | + * @param base |
| 70 | + * @param state |
| 71 | + */ |
| 72 | +export function ancestor<TState>( |
| 73 | + node: acorn.Node, |
| 74 | + visitors: AncestorVisitors<TState>, |
| 75 | + base?: RecursiveVisitors<TState>, |
| 76 | + state?: TState |
| 77 | + ): void |
| 78 | + |
| 79 | +/** |
| 80 | + * does a 'recursive' walk, where the walker functions are responsible for continuing the walk on the child nodes of their target node. |
| 81 | + * @param node |
| 82 | + * @param state the start state |
| 83 | + * @param functions contain an object that maps node types to walker functions |
| 84 | + * @param base provides the fallback walker functions for node types that aren't handled in the {@link functions} object. If not given, the default walkers will be used. |
| 85 | + */ |
| 86 | +export function recursive<TState>( |
| 87 | + node: acorn.Node, |
| 88 | + state: TState, |
| 89 | + functions: RecursiveVisitors<TState>, |
| 90 | + base?: RecursiveVisitors<TState> |
| 91 | +): void |
| 92 | + |
| 93 | +/** |
| 94 | + * does a 'full' walk over a tree, calling the {@link callback} with the arguments (node, state, type) for each node |
| 95 | + * @param node |
| 96 | + * @param callback |
| 97 | + * @param base |
| 98 | + * @param state |
| 99 | + */ |
| 100 | +export function full<TState>( |
| 101 | + node: acorn.Node, |
| 102 | + callback: FullWalkerCallback<TState>, |
| 103 | + base?: RecursiveVisitors<TState>, |
| 104 | + state?: TState |
| 105 | +): void |
| 106 | + |
| 107 | +/** |
| 108 | + * does a 'full' walk over a tree, building up an array of ancestor nodes (including the current node) and passing the array to the callbacks as a third parameter. |
| 109 | + * @param node |
| 110 | + * @param callback |
| 111 | + * @param base |
| 112 | + * @param state |
| 113 | + */ |
| 114 | +export function fullAncestor<TState>( |
| 115 | + node: acorn.Node, |
| 116 | + callback: FullAncestorWalkerCallback<TState>, |
| 117 | + base?: RecursiveVisitors<TState>, |
| 118 | + state?: TState |
| 119 | +): void |
| 120 | + |
| 121 | +/** |
| 122 | + * builds a new walker object by using the walker functions in {@link functions} and filling in the missing ones by taking defaults from {@link base}. |
| 123 | + * @param functions |
| 124 | + * @param base |
| 125 | + */ |
| 126 | +export function make<TState>( |
| 127 | + functions: RecursiveVisitors<TState>, |
| 128 | + base?: RecursiveVisitors<TState> |
| 129 | +): RecursiveVisitors<TState> |
| 130 | + |
| 131 | +/** |
| 132 | + * tries to locate a node in a tree at the given start and/or end offsets, which satisfies the predicate test. {@link start} and {@link end} can be either `null` (as wildcard) or a `number`. {@link test} may be a string (indicating a node type) or a function that takes (nodeType, node) arguments and returns a boolean indicating whether this node is interesting. {@link base} and {@link state} are optional, and can be used to specify a custom walker. Nodes are tested from inner to outer, so if two nodes match the boundaries, the inner one will be preferred. |
| 133 | + * @param node |
| 134 | + * @param start |
| 135 | + * @param end |
| 136 | + * @param type |
| 137 | + * @param base |
| 138 | + * @param state |
| 139 | + */ |
| 140 | +export function findNodeAt<TState>( |
| 141 | + node: acorn.Node, |
| 142 | + start: number | undefined, |
| 143 | + end?: number | undefined, |
| 144 | + type?: FindPredicate | string, |
| 145 | + base?: RecursiveVisitors<TState>, |
| 146 | + state?: TState |
| 147 | +): Found<TState> | undefined |
| 148 | + |
| 149 | +/** |
| 150 | + * like {@link findNodeAt}, but will match any node that exists 'around' (spanning) the given position. |
| 151 | + * @param node |
| 152 | + * @param start |
| 153 | + * @param type |
| 154 | + * @param base |
| 155 | + * @param state |
| 156 | + */ |
| 157 | +export function findNodeAround<TState>( |
| 158 | + node: acorn.Node, |
| 159 | + start: number | undefined, |
| 160 | + type?: FindPredicate | string, |
| 161 | + base?: RecursiveVisitors<TState>, |
| 162 | + state?: TState |
| 163 | +): Found<TState> | undefined |
| 164 | + |
| 165 | +/** |
| 166 | + * similar to {@link findNodeAround}, but will match all nodes after the given position (testing outer nodes before inner nodes). |
| 167 | + */ |
| 168 | +export const findNodeAfter: typeof findNodeAround |
| 169 | + |
| 170 | +export const base: RecursiveVisitors<any> |
0 commit comments