|
|
import * as acorn from "acorn" |
|
|
|
|
|
export type FullWalkerCallback<TState> = ( |
|
|
node: acorn.Node, |
|
|
state: TState, |
|
|
type: string |
|
|
) => void |
|
|
|
|
|
export type FullAncestorWalkerCallback<TState> = ( |
|
|
node: acorn.Node, |
|
|
state: TState, |
|
|
ancestors: acorn.Node[], |
|
|
type: string |
|
|
) => void |
|
|
|
|
|
type AggregateType = { |
|
|
Expression: acorn.Expression, |
|
|
Statement: acorn.Statement, |
|
|
Function: acorn.Function, |
|
|
Class: acorn.Class, |
|
|
Pattern: acorn.Pattern, |
|
|
ForInit: acorn.VariableDeclaration | acorn.Expression |
|
|
} |
|
|
|
|
|
export type SimpleVisitors<TState> = { |
|
|
[type in acorn.AnyNode["type"]]?: (node: Extract<acorn.AnyNode, { type: type }>, state: TState) => void |
|
|
} & { |
|
|
[type in keyof AggregateType]?: (node: AggregateType[type], state: TState) => void |
|
|
} |
|
|
|
|
|
export type AncestorVisitors<TState> = { |
|
|
[type in acorn.AnyNode["type"]]?: ( node: Extract<acorn.AnyNode, { type: type }>, state: TState, ancestors: acorn.Node[] |
|
|
) => void |
|
|
} & { |
|
|
[type in keyof AggregateType]?: (node: AggregateType[type], state: TState, ancestors: acorn.Node[]) => void |
|
|
} |
|
|
|
|
|
export type WalkerCallback<TState> = (node: acorn.Node, state: TState) => void |
|
|
|
|
|
export type RecursiveVisitors<TState> = { |
|
|
[type in acorn.AnyNode["type"]]?: ( node: Extract<acorn.AnyNode, { type: type }>, state: TState, callback: WalkerCallback<TState>) => void |
|
|
} & { |
|
|
[type in keyof AggregateType]?: (node: AggregateType[type], state: TState, callback: WalkerCallback<TState>) => void |
|
|
} |
|
|
|
|
|
export type FindPredicate = (type: string, node: acorn.Node) => boolean |
|
|
|
|
|
export interface Found<TState> { |
|
|
node: acorn.Node, |
|
|
state: TState |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function simple<TState>( |
|
|
node: acorn.Node, |
|
|
visitors: SimpleVisitors<TState>, |
|
|
base?: RecursiveVisitors<TState>, |
|
|
state?: TState |
|
|
): void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function ancestor<TState>( |
|
|
node: acorn.Node, |
|
|
visitors: AncestorVisitors<TState>, |
|
|
base?: RecursiveVisitors<TState>, |
|
|
state?: TState |
|
|
): void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function recursive<TState>( |
|
|
node: acorn.Node, |
|
|
state: TState, |
|
|
functions: RecursiveVisitors<TState>, |
|
|
base?: RecursiveVisitors<TState> |
|
|
): void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function full<TState>( |
|
|
node: acorn.Node, |
|
|
callback: FullWalkerCallback<TState>, |
|
|
base?: RecursiveVisitors<TState>, |
|
|
state?: TState |
|
|
): void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function fullAncestor<TState>( |
|
|
node: acorn.Node, |
|
|
callback: FullAncestorWalkerCallback<TState>, |
|
|
base?: RecursiveVisitors<TState>, |
|
|
state?: TState |
|
|
): void |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function make<TState>( |
|
|
functions: RecursiveVisitors<TState>, |
|
|
base?: RecursiveVisitors<TState> |
|
|
): RecursiveVisitors<TState> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function findNodeAt<TState>( |
|
|
node: acorn.Node, |
|
|
start: number | undefined, |
|
|
end?: number | undefined, |
|
|
type?: FindPredicate | string, |
|
|
base?: RecursiveVisitors<TState>, |
|
|
state?: TState |
|
|
): Found<TState> | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function findNodeAround<TState>( |
|
|
node: acorn.Node, |
|
|
start: number | undefined, |
|
|
type?: FindPredicate | string, |
|
|
base?: RecursiveVisitors<TState>, |
|
|
state?: TState |
|
|
): Found<TState> | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const findNodeAfter: typeof findNodeAround |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const findNodeBefore: typeof findNodeAround |
|
|
|
|
|
export const base: RecursiveVisitors<any> |
|
|
|