Skip to content

Commit

Permalink
fix: improve InclusiveDescendant performance
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jan 16, 2025
1 parent b4e685d commit 9c5f0ee
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-empty-function */

import {expectAssignable, expectError, expectType, expectNotType} from 'tsd'
import type {} from 'type-fest'

Check failure on line 4 in index.test-d.ts

View workflow job for this annotation

GitHub Actions / node

Imported module should be assigned

Check failure on line 4 in index.test-d.ts

View workflow job for this annotation

GitHub Actions / node

Unexpected empty named import block
import type {Node, Literal, Parent} from 'unist'
import {is} from 'unist-util-is'
import {visitParents, SKIP, EXIT, CONTINUE} from './index.js'
Expand Down
11 changes: 8 additions & 3 deletions lib/complex-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/ban-types */

import type {Node, Parent} from 'unist'
import type {LessThan, Subtract} from 'type-fest'
import type {Test} from 'unist-util-is'
import type {Visitor} from './index.js'

Expand All @@ -9,13 +10,17 @@ import type {Visitor} from './index.js'
*/
export type InclusiveDescendant<
Tree extends Node = never,
Found = void
> = Tree extends Parent
Found = void,
Depth extends number = 5
> = LessThan<Depth, 0> extends true
? Tree
: Tree extends Parent
?
| Tree
| InclusiveDescendant<
Exclude<Tree['children'][number], Found | Tree>,
Found | Tree
Found | Tree,
Subtract<Depth, 1>
>
: Tree

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@
"strip-ansi": "^7.0.0",
"tsd": "^0.25.0",
"type-coverage": "^2.0.0",
"type-fest": "^4.32.0",
"typescript": "^4.7.0",
"xo": "^0.53.0"
"xo": "^0.60.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
Expand Down

0 comments on commit 9c5f0ee

Please sign in to comment.