Skip to content

Commit 323984d

Browse files
committed
Change to remove complex-types.d.ts
All types are now exposed from regular files (`index.js`).
1 parent 766107b commit 323984d

File tree

6 files changed

+104
-110
lines changed

6 files changed

+104
-110
lines changed

complex-types.d.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ export type {
33
Action,
44
ActionTuple,
55
BuildVisitor,
6+
// Used in `unist-util-visit`:
7+
InclusiveDescendant,
68
Index,
9+
// Used in `unist-util-visit`:
10+
Matches,
711
Visitor,
812
VisitorResult
913
} from './lib/index.js'

lib/complex-types.d.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

lib/index.js

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,91 @@
44
* @typedef {import('unist-util-is').Test} Test
55
*/
66

7+
/**
8+
* @typedef {(
9+
* Fn extends (value: any) => value is infer Thing
10+
* ? Thing
11+
* : Fallback
12+
* )} Predicate
13+
* Get the value of a type guard `Fn`.
14+
* @template Fn
15+
* Value; typically function that is a type guard (such as `(x): x is Y`).
16+
* @template Fallback
17+
* Value to yield if `Fn` is not a type guard.
18+
*/
19+
20+
/**
21+
* @typedef {(
22+
* Value extends Node
23+
* ? Check extends null | undefined // No test.
24+
* ? Value
25+
* : Check extends Function // Function test.
26+
* ? Extract<Value, Predicate<Check, Value>>
27+
* : Value['type'] extends Check // String (type) test.
28+
* ? Value
29+
* : Value extends Check // Partial test.
30+
* ? Value
31+
* : never
32+
* : never
33+
* )} MatchesOne
34+
* Check whether a node matches a primitive check in the type system.
35+
* @template Value
36+
* Value; typically unist `Node`.
37+
* @template Check
38+
* Value; typically `unist-util-is`-compatible test, but not arrays.
39+
*/
40+
41+
/**
42+
* @typedef {(
43+
* Check extends Array<any>
44+
* ? MatchesOne<Value, Check[keyof Check]>
45+
* : MatchesOne<Value, Check>
46+
* )} Matches
47+
* Check whether a node matches a check in the type system.
48+
* @template Value
49+
* Value; typically unist `Node`.
50+
* @template Check
51+
* Value; typically `unist-util-is`-compatible test.
52+
*/
53+
54+
/**
55+
* @typedef {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} Uint
56+
* Number; capped reasonably.
57+
*/
58+
59+
/**
60+
* @typedef {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} Increment
61+
* Increment a number in the type system.
62+
* @template {Uint} [I=0]
63+
* Index.
64+
*/
65+
66+
/**
67+
* @typedef {(
68+
* Tree extends Parent
69+
* ? Depth extends Max
70+
* ? Tree
71+
* : Tree | InclusiveDescendant<Tree['children'][number], Max, Increment<Depth>>
72+
* : Tree
73+
* )} InclusiveDescendant
74+
* Collect all (inclusive) descendants of `Tree`.
75+
*
76+
* > 👉 **Note**: for performance reasons, this seems to be the fastest way to
77+
* > recurse without actually running into an infinite loop, which the
78+
* > previous version did.
79+
* >
80+
* > Practically, a max of `2` is typically enough assuming a `Root` is
81+
* > passed, but it doesn’t improve performance.
82+
* > It gets higher with `List > ListItem > Table > TableRow > TableCell`.
83+
* > Using up to `10` doesn’t hurt or help either.
84+
* @template {Node} Tree
85+
* Tree type.
86+
* @template {Uint} [Max=10]
87+
* Max; searches up to this depth.
88+
* @template {Uint} [Depth=0]
89+
* Current depth.
90+
*/
91+
792
/**
893
* @typedef {'skip' | boolean} Action
994
* Union of the action types.
@@ -25,10 +110,6 @@
25110
*/
26111

27112
/**
28-
* @template {Node} [Visited=Node]
29-
* Visited node type.
30-
* @template {Parent} [Ancestor=Parent]
31-
* Ancestor type.
32113
* @callback Visitor
33114
* Handle a node (matching `test`, if given).
34115
*
@@ -59,17 +140,21 @@
59140
* Passing a tuple back only makes sense if the `Action` is `SKIP`.
60141
* When the `Action` is `EXIT`, that action can be returned.
61142
* When the `Action` is `CONTINUE`, `Index` can be returned.
143+
* @template {Node} [Visited=Node]
144+
* Visited node type.
145+
* @template {Parent} [Ancestor=Parent]
146+
* Ancestor type.
62147
*/
63148

64149
/**
150+
* @typedef {Visitor<Matches<InclusiveDescendant<Tree>, Check>, Extract<InclusiveDescendant<Tree>, Parent>>} BuildVisitor
151+
* Build a typed `Visitor` function from a tree and a test.
152+
*
153+
* It will infer which values are passed as `node` and which as `parents`.
65154
* @template {Node} [Tree=Node]
66155
* Tree type.
67156
* @template {Test} [Check=Test]
68157
* Test type.
69-
* @typedef {Visitor<import('./complex-types.js').Matches<import('./complex-types.js').InclusiveDescendant<Tree>, Check>, Extract<import('./complex-types.js').InclusiveDescendant<Tree>, Parent>>} BuildVisitor
70-
* Build a typed `Visitor` function from a tree and a test.
71-
*
72-
* It will infer which values are passed as `node` and which as `parents`.
73158
*/
74159

75160
import {convert} from 'unist-util-is'
@@ -111,9 +196,6 @@ export const SKIP = 'skip'
111196
* You can change the tree.
112197
* See `Visitor` for more info.
113198
*
114-
* @template {Node} Tree
115-
* @template {Test} Check
116-
*
117199
* @overload
118200
* @param {Tree} tree
119201
* @param {Check} check
@@ -137,6 +219,11 @@ export const SKIP = 'skip'
137219
* Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
138220
* @returns {undefined}
139221
* Nothing.
222+
*
223+
* @template {Node} Tree
224+
* Node type.
225+
* @template {Test} Check
226+
* `unist-util-is`-compatible test.
140227
*/
141228
export function visitParents(tree, test, visitor, reverse) {
142229
/** @type {Test} */

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"types": "index.d.ts",
4040
"files": [
4141
"lib/",
42-
"complex-types.d.ts",
4342
"index.d.ts",
4443
"index.js"
4544
],
@@ -91,7 +90,7 @@
9190
"detail": true,
9291
"#": "needed `any`s",
9392
"ignoreFiles": [
94-
"lib/complex-types.d.ts"
93+
"lib/index.d.ts"
9594
],
9695
"ignoreCatch": true,
9796
"strict": true

tsconfig.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,5 @@
1111
"target": "es2020"
1212
},
1313
"exclude": ["coverage/", "node_modules/"],
14-
"include": [
15-
"**/*.js",
16-
"lib/complex-types.d.ts",
17-
"complex-types.d.ts",
18-
"index.d.ts"
19-
]
14+
"include": ["**/*.js", "index.d.ts"]
2015
}

0 commit comments

Comments
 (0)