From 9c5f0eec796ce72cbbd106e9416f91e00a62d776 Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 16 Jan 2025 16:07:25 +0800 Subject: [PATCH] fix: improve InclusiveDescendant performance --- index.test-d.ts | 1 + lib/complex-types.d.ts | 11 ++++++++--- package.json | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/index.test-d.ts b/index.test-d.ts index 4dbce97..7c03708 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,6 +1,7 @@ /* eslint-disable @typescript-eslint/no-empty-function */ import {expectAssignable, expectError, expectType, expectNotType} from 'tsd' +import type {} from 'type-fest' import type {Node, Literal, Parent} from 'unist' import {is} from 'unist-util-is' import {visitParents, SKIP, EXIT, CONTINUE} from './index.js' diff --git a/lib/complex-types.d.ts b/lib/complex-types.d.ts index ef06b3a..751cfb0 100644 --- a/lib/complex-types.d.ts +++ b/lib/complex-types.d.ts @@ -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' @@ -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 extends true + ? Tree + : Tree extends Parent ? | Tree | InclusiveDescendant< Exclude, - Found | Tree + Found | Tree, + Subtract > : Tree diff --git a/package.json b/package.json index 3aa8dc3..cd21f03 100644 --- a/package.json +++ b/package.json @@ -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",