index.d.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Visit nodes.
  3. *
  4. * This algorithm performs *depth-first* *tree traversal* in *preorder*
  5. * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
  6. *
  7. * You can choose for which nodes `visitor` is called by passing a `test`.
  8. * For complex tests, you should test yourself in `visitor`, as it will be
  9. * faster and will have improved type information.
  10. *
  11. * Walking the tree is an intensive task.
  12. * Make use of the return values of the visitor when possible.
  13. * Instead of walking a tree multiple times, walk it once, use `unist-util-is`
  14. * to check if a node matches, and then perform different operations.
  15. *
  16. * You can change the tree.
  17. * See `Visitor` for more info.
  18. *
  19. * @overload
  20. * @param {Tree} tree
  21. * @param {Check} check
  22. * @param {BuildVisitor<Tree, Check>} visitor
  23. * @param {boolean | null | undefined} [reverse]
  24. * @returns {undefined}
  25. *
  26. * @overload
  27. * @param {Tree} tree
  28. * @param {BuildVisitor<Tree>} visitor
  29. * @param {boolean | null | undefined} [reverse]
  30. * @returns {undefined}
  31. *
  32. * @param {UnistNode} tree
  33. * Tree to traverse.
  34. * @param {Visitor | Test} testOrVisitor
  35. * `unist-util-is`-compatible test (optional, omit to pass a visitor).
  36. * @param {Visitor | boolean | null | undefined} [visitorOrReverse]
  37. * Handle each node (when test is omitted, pass `reverse`).
  38. * @param {boolean | null | undefined} [maybeReverse=false]
  39. * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
  40. * @returns {undefined}
  41. * Nothing.
  42. *
  43. * @template {UnistNode} Tree
  44. * Node type.
  45. * @template {Test} Check
  46. * `unist-util-is`-compatible test.
  47. */
  48. export function visit<Tree extends UnistNode, Check extends Test>(tree: Tree, check: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined): undefined;
  49. /**
  50. * Visit nodes.
  51. *
  52. * This algorithm performs *depth-first* *tree traversal* in *preorder*
  53. * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
  54. *
  55. * You can choose for which nodes `visitor` is called by passing a `test`.
  56. * For complex tests, you should test yourself in `visitor`, as it will be
  57. * faster and will have improved type information.
  58. *
  59. * Walking the tree is an intensive task.
  60. * Make use of the return values of the visitor when possible.
  61. * Instead of walking a tree multiple times, walk it once, use `unist-util-is`
  62. * to check if a node matches, and then perform different operations.
  63. *
  64. * You can change the tree.
  65. * See `Visitor` for more info.
  66. *
  67. * @overload
  68. * @param {Tree} tree
  69. * @param {Check} check
  70. * @param {BuildVisitor<Tree, Check>} visitor
  71. * @param {boolean | null | undefined} [reverse]
  72. * @returns {undefined}
  73. *
  74. * @overload
  75. * @param {Tree} tree
  76. * @param {BuildVisitor<Tree>} visitor
  77. * @param {boolean | null | undefined} [reverse]
  78. * @returns {undefined}
  79. *
  80. * @param {UnistNode} tree
  81. * Tree to traverse.
  82. * @param {Visitor | Test} testOrVisitor
  83. * `unist-util-is`-compatible test (optional, omit to pass a visitor).
  84. * @param {Visitor | boolean | null | undefined} [visitorOrReverse]
  85. * Handle each node (when test is omitted, pass `reverse`).
  86. * @param {boolean | null | undefined} [maybeReverse=false]
  87. * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
  88. * @returns {undefined}
  89. * Nothing.
  90. *
  91. * @template {UnistNode} Tree
  92. * Node type.
  93. * @template {Test} Check
  94. * `unist-util-is`-compatible test.
  95. */
  96. export function visit<Tree extends UnistNode, Check extends Test>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined): undefined;
  97. /**
  98. * Test from `unist-util-is`.
  99. *
  100. * Note: we have remove and add `undefined`, because otherwise when generating
  101. * automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
  102. * which doesn’t work when publishing on npm.
  103. */
  104. export type Test = Exclude<import("unist-util-is").Test, undefined> | undefined;
  105. /**
  106. * Get the value of a type guard `Fn`.
  107. */
  108. export type Predicate<Fn, Fallback> = (Fn extends (value: any) => value is infer Thing ? Thing : Fallback);
  109. /**
  110. * Check whether a node matches a primitive check in the type system.
  111. */
  112. export type MatchesOne<Value, Check> = (Check extends null | undefined ? Value : Value extends {
  113. type: Check;
  114. } ? Value : Value extends Check ? Value : Check extends Function ? Predicate<Check, Value> extends Value ? Predicate<Check, Value> : never : never);
  115. /**
  116. * Check whether a node matches a check in the type system.
  117. */
  118. export type Matches<Value, Check> = (Check extends ReadonlyArray<any> ? MatchesOne<Value, Check[number]> : MatchesOne<Value, Check>);
  119. /**
  120. * Number; capped reasonably.
  121. */
  122. export type Uint = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
  123. /**
  124. * Increment a number in the type system.
  125. */
  126. export type Increment<I extends Uint = 0> = I extends 0 ? 1 : I extends 1 ? 2 : I extends 2 ? 3 : I extends 3 ? 4 : I extends 4 ? 5 : I extends 5 ? 6 : I extends 6 ? 7 : I extends 7 ? 8 : I extends 8 ? 9 : 10;
  127. /**
  128. * Collect nodes that can be parents of `Child`.
  129. */
  130. export type InternalParent<Node extends UnistNode, Child extends UnistNode> = (Node extends UnistParent ? Node extends {
  131. children: Array<infer Children>;
  132. } ? Child extends Children ? Node : never : never : never);
  133. /**
  134. * Collect nodes in `Tree` that can be parents of `Child`.
  135. */
  136. export type Parent<Tree extends UnistNode, Child extends UnistNode> = InternalParent<InclusiveDescendant<Tree>, Child>;
  137. /**
  138. * Collect nodes in `Tree` that can be ancestors of `Child`.
  139. */
  140. export type InternalAncestor<Node extends UnistNode, Child extends UnistNode, Max extends Uint = 10, Depth extends Uint = 0> = (Depth extends Max ? never : InternalParent<Node, Child> | InternalAncestor<Node, InternalParent<Node, Child>, Max, Increment<Depth>>);
  141. /**
  142. * Collect all (inclusive) descendants of `Tree`.
  143. *
  144. * > 👉 **Note**: for performance reasons, this seems to be the fastest way to
  145. * > recurse without actually running into an infinite loop, which the
  146. * > previous version did.
  147. * >
  148. * > Practically, a max of `2` is typically enough assuming a `Root` is
  149. * > passed, but it doesn’t improve performance.
  150. * > It gets higher with `List > ListItem > Table > TableRow > TableCell`.
  151. * > Using up to `10` doesn’t hurt or help either.
  152. */
  153. export type InclusiveDescendant<Tree extends UnistNode, Max extends Uint = 10, Depth extends Uint = 0> = (Tree extends UnistParent ? Depth extends Max ? Tree : Tree | InclusiveDescendant<Tree["children"][number], Max, Increment<Depth>> : Tree);
  154. /**
  155. * Handle a node (matching `test`, if given).
  156. *
  157. * Visitors are free to transform `node`.
  158. * They can also transform `parent`.
  159. *
  160. * Replacing `node` itself, if `SKIP` is not returned, still causes its
  161. * descendants to be walked (which is a bug).
  162. *
  163. * When adding or removing previous siblings of `node` (or next siblings, in
  164. * case of reverse), the `Visitor` should return a new `Index` to specify the
  165. * sibling to traverse after `node` is traversed.
  166. * Adding or removing next siblings of `node` (or previous siblings, in case
  167. * of reverse) is handled as expected without needing to return a new `Index`.
  168. *
  169. * Removing the children property of `parent` still results in them being
  170. * traversed.
  171. */
  172. export type Visitor<Visited extends UnistNode = UnistNode, Ancestor extends UnistParent = UnistParent> = (node: Visited, index: Visited extends UnistNode ? number | undefined : never, parent: Ancestor extends UnistParent ? Ancestor | undefined : never) => VisitorResult;
  173. /**
  174. * Build a typed `Visitor` function from a node and all possible parents.
  175. *
  176. * It will infer which values are passed as `node` and which as `parent`.
  177. */
  178. export type BuildVisitorFromMatch<Visited extends UnistNode, Ancestor extends UnistParent> = Visitor<Visited, Parent<Ancestor, Visited>>;
  179. /**
  180. * Build a typed `Visitor` function from a list of descendants and a test.
  181. *
  182. * It will infer which values are passed as `node` and which as `parent`.
  183. */
  184. export type BuildVisitorFromDescendants<Descendant extends UnistNode, Check extends Test> = (BuildVisitorFromMatch<Matches<Descendant, Check>, Extract<Descendant, UnistParent>>);
  185. /**
  186. * Build a typed `Visitor` function from a tree and a test.
  187. *
  188. * It will infer which values are passed as `node` and which as `parent`.
  189. */
  190. export type BuildVisitor<Tree extends UnistNode = UnistNode, Check extends Test = Test> = (BuildVisitorFromDescendants<InclusiveDescendant<Tree>, Check>);
  191. import type { Node as UnistNode } from 'unist';
  192. import type { Parent as UnistParent } from 'unist';
  193. import type { VisitorResult } from 'unist-util-visit-parents';
  194. export { CONTINUE, EXIT, SKIP } from "unist-util-visit-parents";
  195. //# sourceMappingURL=index.d.ts.map