index.d.ts 5.7 KB

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