Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve InclusiveDescendant performance #16

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions lib/complex-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,48 @@
import type {Test} from 'unist-util-is'
import type {Visitor} from './index.js'

type LessThan<
A extends number,
B extends number,
S extends any[] = []
> = S['length'] extends B
? false
: S['length'] extends A
? true
: LessThan<A, B, [...S, any]>

type Subtract<
A extends number,
B extends number,
I extends any[] = [],
O extends any[] = []
> = LessThan<A, B> extends true
? never
: LessThan<I['length'], A> extends true
? Subtract<
A,
B,
[...I, any],
LessThan<I['length'], B> extends true ? O : [...O, any]
>
: O['length']

/**
* Internal utility to collect all descendants of in `Tree`.
*/
export type InclusiveDescendant<
Tree extends Node = never,
Found = void
> = Tree extends Parent
Found = void,
Depth extends number = 5
> = Depth extends 0
? Tree
: Tree extends Parent
?
| Tree
| InclusiveDescendant<
Exclude<Tree['children'][number], Found | Tree>,
Found | Tree
Found | Tree,
Subtract<Depth, 1>
>
: Tree

Expand All @@ -23,7 +53,7 @@
* Infer the thing that is asserted from a type guard.
*/
type Predicate<Fn, Fallback = never> = Fn extends (
value: any

Check failure on line 56 in lib/complex-types.d.ts

View workflow job for this annotation

GitHub Actions / node

Type Parameter name `Function_` must match one of the following formats: StrictPascalCase

Check failure on line 56 in lib/complex-types.d.ts

View workflow job for this annotation

GitHub Actions / node

Type Parameter name `Function_` must match one of the following formats: StrictPascalCase
) => value is infer Thing
? Thing
: Fallback
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"tsd": "^0.25.0",
"type-coverage": "^2.0.0",
"typescript": "^4.7.0",
"xo": "^0.53.0"
"xo": "^0.60.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
Expand Down
Loading