仲裁视频会议H5

walk.d.mts 5.7KB

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