index.d.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * Visit nodes, with ancestral information.
  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} test
  35. * `unist-util-is`-compatible test
  36. * @param {Visitor | boolean | null | undefined} [visitor]
  37. * Handle each node.
  38. * @param {boolean | null | undefined} [reverse]
  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 visitParents<Tree extends UnistNode, Check extends Test>(tree: Tree, check: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined): undefined;
  49. /**
  50. * Visit nodes, with ancestral information.
  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} test
  83. * `unist-util-is`-compatible test
  84. * @param {Visitor | boolean | null | undefined} [visitor]
  85. * Handle each node.
  86. * @param {boolean | null | undefined} [reverse]
  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 visitParents<Tree extends UnistNode, Check extends Test>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined): undefined;
  97. /**
  98. * Continue traversing as normal.
  99. */
  100. export const CONTINUE: true;
  101. /**
  102. * Stop traversing immediately.
  103. */
  104. export const EXIT: false;
  105. /**
  106. * Do not traverse this node’s children.
  107. */
  108. export const SKIP: "skip";
  109. /**
  110. * Test from `unist-util-is`.
  111. *
  112. * Note: we have remove and add `undefined`, because otherwise when generating
  113. * automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
  114. * which doesn’t work when publishing on npm.
  115. */
  116. export type Test = Exclude<import("unist-util-is").Test, undefined> | undefined;
  117. /**
  118. * Get the value of a type guard `Fn`.
  119. */
  120. export type Predicate<Fn, Fallback> = (Fn extends (value: any) => value is infer Thing ? Thing : Fallback);
  121. /**
  122. * Check whether a node matches a primitive check in the type system.
  123. */
  124. export type MatchesOne<Value, Check> = (Check extends null | undefined ? Value : Value extends {
  125. type: Check;
  126. } ? Value : Value extends Check ? Value : Check extends Function ? Predicate<Check, Value> extends Value ? Predicate<Check, Value> : never : never);
  127. /**
  128. * Check whether a node matches a check in the type system.
  129. */
  130. export type Matches<Value, Check> = (Check extends ReadonlyArray<infer T> ? MatchesOne<Value, T> : Check extends Array<infer T> ? MatchesOne<Value, T> : MatchesOne<Value, Check>);
  131. /**
  132. * Number; capped reasonably.
  133. */
  134. export type Uint = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
  135. /**
  136. * Increment a number in the type system.
  137. */
  138. 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;
  139. /**
  140. * Collect nodes that can be parents of `Child`.
  141. */
  142. export type InternalParent<Node extends UnistNode, Child extends UnistNode> = (Node extends UnistParent ? Node extends {
  143. children: Array<infer Children>;
  144. } ? Child extends Children ? Node : never : never : never);
  145. /**
  146. * Collect nodes in `Tree` that can be parents of `Child`.
  147. */
  148. export type Parent<Tree extends UnistNode, Child extends UnistNode> = InternalParent<InclusiveDescendant<Tree>, Child>;
  149. /**
  150. * Collect nodes in `Tree` that can be ancestors of `Child`.
  151. */
  152. 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>>);
  153. /**
  154. * Collect nodes in `Tree` that can be ancestors of `Child`.
  155. */
  156. export type Ancestor<Tree extends UnistNode, Child extends UnistNode> = InternalAncestor<InclusiveDescendant<Tree>, Child>;
  157. /**
  158. * Collect all (inclusive) descendants of `Tree`.
  159. *
  160. * > 👉 **Note**: for performance reasons, this seems to be the fastest way to
  161. * > recurse without actually running into an infinite loop, which the
  162. * > previous version did.
  163. * >
  164. * > Practically, a max of `2` is typically enough assuming a `Root` is
  165. * > passed, but it doesn’t improve performance.
  166. * > It gets higher with `List > ListItem > Table > TableRow > TableCell`.
  167. * > Using up to `10` doesn’t hurt or help either.
  168. */
  169. 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);
  170. /**
  171. * Union of the action types.
  172. */
  173. export type Action = "skip" | boolean;
  174. /**
  175. * Move to the sibling at `index` next (after node itself is completely
  176. * traversed).
  177. *
  178. * Useful if mutating the tree, such as removing the node the visitor is
  179. * currently on, or any of its previous siblings.
  180. * Results less than 0 or greater than or equal to `children.length` stop
  181. * traversing the parent.
  182. */
  183. export type Index = number;
  184. /**
  185. * List with one or two values, the first an action, the second an index.
  186. */
  187. export type ActionTuple = [(Action | null | undefined | void)?, (Index | null | undefined)?];
  188. /**
  189. * Any value that can be returned from a visitor.
  190. */
  191. export type VisitorResult = Action | ActionTuple | Index | null | undefined | void;
  192. /**
  193. * Handle a node (matching `test`, if given).
  194. *
  195. * Visitors are free to transform `node`.
  196. * They can also transform the parent of node (the last of `ancestors`).
  197. *
  198. * Replacing `node` itself, if `SKIP` is not returned, still causes its
  199. * descendants to be walked (which is a bug).
  200. *
  201. * When adding or removing previous siblings of `node` (or next siblings, in
  202. * case of reverse), the `Visitor` should return a new `Index` to specify the
  203. * sibling to traverse after `node` is traversed.
  204. * Adding or removing next siblings of `node` (or previous siblings, in case
  205. * of reverse) is handled as expected without needing to return a new `Index`.
  206. *
  207. * Removing the children property of an ancestor still results in them being
  208. * traversed.
  209. */
  210. export type Visitor<Visited extends UnistNode = UnistNode, VisitedParents extends UnistParent = UnistParent> = (node: Visited, ancestors: Array<VisitedParents>) => VisitorResult;
  211. /**
  212. * Build a typed `Visitor` function from a tree and a test.
  213. *
  214. * It will infer which values are passed as `node` and which as `parents`.
  215. */
  216. export type BuildVisitor<Tree extends UnistNode = UnistNode, Check extends Test = Test> = Visitor<Matches<InclusiveDescendant<Tree>, Check>, Ancestor<Tree, Matches<InclusiveDescendant<Tree>, Check>>>;
  217. import type { Node as UnistNode } from 'unist';
  218. import type { Parent as UnistParent } from 'unist';
  219. //# sourceMappingURL=index.d.ts.map