walk.d.ts 2.5 KB

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