|
1 |
| -import {Node} from 'acorn'; |
2 |
| - |
3 |
| -declare module "acorn-walk" { |
4 |
| - type FullWalkerCallback<TState> = ( |
5 |
| - node: Node, |
6 |
| - state: TState, |
7 |
| - type: string |
8 |
| - ) => void; |
9 |
| - |
10 |
| - type FullAncestorWalkerCallback<TState> = ( |
11 |
| - node: Node, |
12 |
| - state: TState | Node[], |
13 |
| - ancestors: Node[], |
14 |
| - type: string |
15 |
| - ) => void; |
16 |
| - type WalkerCallback<TState> = (node: Node, state: TState) => void; |
17 |
| - |
18 |
| - type SimpleWalkerFn<TState> = ( |
19 |
| - node: Node, |
20 |
| - state: TState |
21 |
| - ) => void; |
22 |
| - |
23 |
| - type AncestorWalkerFn<TState> = ( |
24 |
| - node: Node, |
25 |
| - state: TState| Node[], |
26 |
| - ancestors: Node[] |
27 |
| - ) => void; |
28 |
| - |
29 |
| - type RecursiveWalkerFn<TState> = ( |
30 |
| - node: Node, |
31 |
| - state: TState, |
32 |
| - callback: WalkerCallback<TState> |
33 |
| - ) => void; |
34 |
| - |
35 |
| - type SimpleVisitors<TState> = { |
36 |
| - [type: string]: SimpleWalkerFn<TState> |
37 |
| - }; |
38 |
| - |
39 |
| - type AncestorVisitors<TState> = { |
40 |
| - [type: string]: AncestorWalkerFn<TState> |
41 |
| - }; |
42 |
| - |
43 |
| - type RecursiveVisitors<TState> = { |
44 |
| - [type: string]: RecursiveWalkerFn<TState> |
45 |
| - }; |
46 |
| - |
47 |
| - type FindPredicate = (type: string, node: Node) => boolean; |
48 |
| - |
49 |
| - interface Found<TState> { |
50 |
| - node: Node, |
51 |
| - state: TState |
52 |
| - } |
53 |
| - |
54 |
| - export function simple<TState>( |
55 |
| - node: Node, |
56 |
| - visitors: SimpleVisitors<TState>, |
57 |
| - base?: RecursiveVisitors<TState>, |
58 |
| - state?: TState |
59 |
| - ): void; |
| 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 | +} |
60 | 22 |
|
61 |
| - export function ancestor<TState>( |
62 |
| - node: Node, |
63 |
| - visitors: AncestorVisitors<TState>, |
64 |
| - base?: RecursiveVisitors<TState>, |
65 |
| - state?: TState |
66 |
| - ): void; |
67 |
| - |
68 |
| - export function recursive<TState>( |
69 |
| - node: Node, |
70 |
| - state: TState, |
71 |
| - functions: RecursiveVisitors<TState>, |
72 |
| - base?: RecursiveVisitors<TState> |
73 |
| - ): void; |
74 |
| - |
75 |
| - export function full<TState>( |
76 |
| - node: Node, |
77 |
| - callback: FullWalkerCallback<TState>, |
78 |
| - base?: RecursiveVisitors<TState>, |
79 |
| - state?: TState |
80 |
| - ): void; |
| 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 | +} |
81 | 28 |
|
82 |
| - export function fullAncestor<TState>( |
83 |
| - node: Node, |
84 |
| - callback: FullAncestorWalkerCallback<TState>, |
85 |
| - base?: RecursiveVisitors<TState>, |
86 |
| - state?: TState |
87 |
| - ): void; |
88 |
| - |
89 |
| - export function make<TState>( |
90 |
| - functions: RecursiveVisitors<TState>, |
91 |
| - base?: RecursiveVisitors<TState> |
92 |
| - ): RecursiveVisitors<TState>; |
93 |
| - |
94 |
| - export function findNodeAt<TState>( |
95 |
| - node: Node, |
96 |
| - start: number | undefined, |
97 |
| - end?: number | undefined, |
98 |
| - type?: FindPredicate | string, |
99 |
| - base?: RecursiveVisitors<TState>, |
100 |
| - state?: TState |
101 |
| - ): Found<TState> | undefined; |
| 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 | +} |
102 | 35 |
|
103 |
| - export function findNodeAround<TState>( |
104 |
| - node: Node, |
105 |
| - start: number | undefined, |
106 |
| - type?: FindPredicate | string, |
107 |
| - base?: RecursiveVisitors<TState>, |
108 |
| - state?: TState |
109 |
| - ): Found<TState> | undefined; |
| 36 | +export type WalkerCallback<TState> = (node: acorn.Node, state: TState) => void |
110 | 37 |
|
111 |
| - export const findNodeAfter: typeof findNodeAround; |
| 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 | +} |
112 | 43 |
|
113 |
| - export const base: RecursiveVisitors<any>; |
| 44 | +export type FindPredicate = (type: string, node: acorn.Node) => boolean |
| 45 | + |
| 46 | +export interface Found<TState> { |
| 47 | + node: acorn.Node, |
| 48 | + state: TState |
114 | 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