Skip to content

Commit 653cd15

Browse files
committed
fix: improve InclusiveDescendant performance
1 parent b4e685d commit 653cd15

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lib/complex-types.d.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,48 @@ import type {Node, Parent} from 'unist'
44
import type {Test} from 'unist-util-is'
55
import type {Visitor} from './index.js'
66

7+
type LessThan<
8+
A extends number,
9+
B extends number,
10+
S extends any[] = []
11+
> = S['length'] extends B
12+
? false
13+
: S['length'] extends A
14+
? true
15+
: LessThan<A, B, [...S, any]>
16+
17+
type Subtract<
18+
A extends number,
19+
B extends number,
20+
I extends any[] = [],
21+
O extends any[] = []
22+
> = LessThan<A, B> extends true
23+
? never
24+
: LessThan<I['length'], A> extends true
25+
? Subtract<
26+
A,
27+
B,
28+
[...I, any],
29+
LessThan<I['length'], B> extends true ? O : [...O, any]
30+
>
31+
: O['length']
32+
733
/**
834
* Internal utility to collect all descendants of in `Tree`.
935
*/
1036
export type InclusiveDescendant<
1137
Tree extends Node = never,
12-
Found = void
13-
> = Tree extends Parent
38+
Found = void,
39+
Depth extends number = 5
40+
> = Depth extends 0
41+
? Tree
42+
: Tree extends Parent
1443
?
1544
| Tree
1645
| InclusiveDescendant<
1746
Exclude<Tree['children'][number], Found | Tree>,
18-
Found | Tree
47+
Found | Tree,
48+
Subtract<Depth, 1>
1949
>
2050
: Tree
2151

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"tsd": "^0.25.0",
6363
"type-coverage": "^2.0.0",
6464
"typescript": "^4.7.0",
65-
"xo": "^0.53.0"
65+
"xo": "^0.60.0"
6666
},
6767
"scripts": {
6868
"prepack": "npm run build && npm run format",

0 commit comments

Comments
 (0)