walk.d.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import * as acorn from "acorn"
  2. export type FullWalkerCallback<TState> = (
  3. node: acorn.Node,
  4. state: TState,
  5. type: string
  6. ) => void
  7. export type FullAncestorWalkerCallback<TState> = (
  8. node: acorn.Node,
  9. state: TState,
  10. ancestors: acorn.Node[],
  11. type: string
  12. ) => void
  13. type AggregateType = {
  14. Expression: acorn.Expression,
  15. Statement: acorn.Statement,
  16. Function: acorn.Function,
  17. Class: acorn.Class,
  18. Pattern: acorn.Pattern,
  19. ForInit: acorn.VariableDeclaration | acorn.Expression
  20. }
  21. export type SimpleVisitors<TState> = {
  22. [type in acorn.AnyNode["type"]]?: (node: Extract<acorn.AnyNode, { type: type }>, state: TState) => void
  23. } & {
  24. [type in keyof AggregateType]?: (node: AggregateType[type], state: TState) => void
  25. }
  26. export type AncestorVisitors<TState> = {
  27. [type in acorn.AnyNode["type"]]?: ( node: Extract<acorn.AnyNode, { type: type }>, state: TState, ancestors: acorn.Node[]
  28. ) => void
  29. } & {
  30. [type in keyof AggregateType]?: (node: AggregateType[type], state: TState, ancestors: acorn.Node[]) => void
  31. }
  32. export type WalkerCallback<TState> = (node: acorn.Node, state: TState) => void
  33. export type RecursiveVisitors<TState> = {
  34. [type in acorn.AnyNode["type"]]?: ( node: Extract<acorn.AnyNode, { type: type }>, state: TState, callback: WalkerCallback<TState>) => void
  35. } & {
  36. [type in keyof AggregateType]?: (node: AggregateType[type], state: TState, callback: WalkerCallback<TState>) => void
  37. }
  38. export type FindPredicate = (type: string, node: acorn.Node) => boolean
  39. export interface Found<TState> {
  40. node: acorn.Node,
  41. state: TState
  42. }
  43. /**
  44. * does a 'simple' walk over a tree
  45. * @param node the AST node to walk
  46. * @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.
  47. * @param base a walker algorithm
  48. * @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.)
  49. */
  50. export function simple<TState>(
  51. node: acorn.Node,
  52. visitors: SimpleVisitors<TState>,
  53. base?: RecursiveVisitors<TState>,
  54. state?: TState
  55. ): void
  56. /**
  57. * 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.
  58. * @param node
  59. * @param visitors
  60. * @param base
  61. * @param state
  62. */
  63. export function ancestor<TState>(
  64. node: acorn.Node,
  65. visitors: AncestorVisitors<TState>,
  66. base?: RecursiveVisitors<TState>,
  67. state?: TState
  68. ): void
  69. /**
  70. * does a 'recursive' walk, where the walker functions are responsible for continuing the walk on the child nodes of their target node.
  71. * @param node
  72. * @param state the start state
  73. * @param functions contain an object that maps node types to walker functions
  74. * @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.
  75. */
  76. export function recursive<TState>(
  77. node: acorn.Node,
  78. state: TState,
  79. functions: RecursiveVisitors<TState>,
  80. base?: RecursiveVisitors<TState>
  81. ): void
  82. /**
  83. * does a 'full' walk over a tree, calling the {@link callback} with the arguments (node, state, type) for each node
  84. * @param node
  85. * @param callback
  86. * @param base
  87. * @param state
  88. */
  89. export function full<TState>(
  90. node: acorn.Node,
  91. callback: FullWalkerCallback<TState>,
  92. base?: RecursiveVisitors<TState>,
  93. state?: TState
  94. ): void
  95. /**
  96. * 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.
  97. * @param node
  98. * @param callback
  99. * @param base
  100. * @param state
  101. */
  102. export function fullAncestor<TState>(
  103. node: acorn.Node,
  104. callback: FullAncestorWalkerCallback<TState>,
  105. base?: RecursiveVisitors<TState>,
  106. state?: TState
  107. ): void
  108. /**
  109. * 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}.
  110. * @param functions
  111. * @param base
  112. */
  113. export function make<TState>(
  114. functions: RecursiveVisitors<TState>,
  115. base?: RecursiveVisitors<TState>
  116. ): RecursiveVisitors<TState>
  117. /**
  118. * 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.
  119. * @param node
  120. * @param start
  121. * @param end
  122. * @param type
  123. * @param base
  124. * @param state
  125. */
  126. export function findNodeAt<TState>(
  127. node: acorn.Node,
  128. start: number | undefined,
  129. end?: number | undefined,
  130. type?: FindPredicate | string,
  131. base?: RecursiveVisitors<TState>,
  132. state?: TState
  133. ): Found<TState> | undefined
  134. /**
  135. * like {@link findNodeAt}, but will match any node that exists 'around' (spanning) the given position.
  136. * @param node
  137. * @param start
  138. * @param type
  139. * @param base
  140. * @param state
  141. */
  142. export function findNodeAround<TState>(
  143. node: acorn.Node,
  144. start: number | undefined,
  145. type?: FindPredicate | string,
  146. base?: RecursiveVisitors<TState>,
  147. state?: TState
  148. ): Found<TState> | undefined
  149. /**
  150. * Find the outermost matching node after a given position.
  151. */
  152. export const findNodeAfter: typeof findNodeAround
  153. /**
  154. * Find the outermost matching node before a given position.
  155. */
  156. export const findNodeBefore: typeof findNodeAround
  157. export const base: RecursiveVisitors<any>