walk.d.mts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import * as acorn from "acorn"
  2. export type FullWalkerCallback<TState> = (
  3. node: acorn.AnyNode,
  4. state: TState,
  5. type: string
  6. ) => void
  7. export type FullAncestorWalkerCallback<TState> = (
  8. node: acorn.AnyNode,
  9. state: TState,
  10. ancestors: acorn.AnyNode[],
  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.AnyNode[]
  28. ) => void
  29. } & {
  30. [type in keyof AggregateType]?: (node: AggregateType[type], state: TState, ancestors: acorn.AnyNode[]) => void
  31. }
  32. export type WalkerCallback<TState> = (node: acorn.AnyNode, 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.AnyNode) => boolean
  39. export interface Found<TState> {
  40. node: acorn.AnyNode,
  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. */
  59. export function ancestor<TState>(
  60. node: acorn.Node,
  61. visitors: AncestorVisitors<TState>,
  62. base?: RecursiveVisitors<TState>,
  63. state?: TState
  64. ): void
  65. /**
  66. * does a 'recursive' walk, where the walker functions are responsible for continuing the walk on the child nodes of their target node.
  67. * @param node
  68. * @param state the start state
  69. * @param functions contain an object that maps node types to walker functions
  70. * @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.
  71. */
  72. export function recursive<TState>(
  73. node: acorn.Node,
  74. state: TState,
  75. functions: RecursiveVisitors<TState>,
  76. base?: RecursiveVisitors<TState>
  77. ): void
  78. /**
  79. * does a 'full' walk over a tree, calling the {@link callback} with the arguments (node, state, type) for each node
  80. */
  81. export function full<TState>(
  82. node: acorn.Node,
  83. callback: FullWalkerCallback<TState>,
  84. base?: RecursiveVisitors<TState>,
  85. state?: TState
  86. ): void
  87. /**
  88. * 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.
  89. */
  90. export function fullAncestor<TState>(
  91. node: acorn.Node,
  92. callback: FullAncestorWalkerCallback<TState>,
  93. base?: RecursiveVisitors<TState>,
  94. state?: TState
  95. ): void
  96. /**
  97. * 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}.
  98. */
  99. export function make<TState>(
  100. functions: RecursiveVisitors<TState>,
  101. base?: RecursiveVisitors<TState>
  102. ): RecursiveVisitors<TState>
  103. /**
  104. * 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.
  105. */
  106. export function findNodeAt<TState>(
  107. node: acorn.Node,
  108. start: number | undefined | null,
  109. end?: number | undefined | null,
  110. type?: FindPredicate | string,
  111. base?: RecursiveVisitors<TState>,
  112. state?: TState
  113. ): Found<TState> | undefined
  114. /**
  115. * like {@link findNodeAt}, but will match any node that exists 'around' (spanning) the given position.
  116. */
  117. export function findNodeAround<TState>(
  118. node: acorn.Node,
  119. start: number | undefined | null,
  120. type?: FindPredicate | string,
  121. base?: RecursiveVisitors<TState>,
  122. state?: TState
  123. ): Found<TState> | undefined
  124. /**
  125. * Find the outermost matching node after a given position.
  126. */
  127. export const findNodeAfter: typeof findNodeAround
  128. /**
  129. * Find the outermost matching node before a given position.
  130. */
  131. export const findNodeBefore: typeof findNodeAround
  132. export const base: RecursiveVisitors<any>