-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change types to base what
visitor
gets on tree
Previously, to define what `visitor` received could only be done through a TypeScript type parameter: ```js // This used to work but no longer!! visitParents<Heading>(tree, 'heading', (node) => { expectType<Heading>(node) }) ``` This did not look at `tree` at all (even if `tree` was hast, and as `Heading` is mdast, it would pass `Heading` to `visitor`). It also made it impossible to narrow types in JS. Given that more and more of unist and friends is now strongly typed, we can expect `tree` to be some kind of implementation of `Node` rather than the abstract `Node` interface itself. With that, we can also find all possible node types inside `tree`. This commit changes to perform the test (`'heading'`) in the type system and actually narrow down which nodes that are in `tree` match `test`. This gives us: ```js // This now works: const tree: Root = {/* … */} visitParents(tree, 'heading', (node) => { expectType<Heading>(node) }) ``` Closes GH-10. Reviewed-by: Remco Haszing <remcohaszing@gmail.com> Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
- Loading branch information
Showing
6 changed files
with
257 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
.DS_Store | ||
*.d.ts | ||
color.browser.d.ts | ||
color.d.ts | ||
index.d.ts | ||
test.d.ts | ||
*.log | ||
coverage/ | ||
node_modules/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
|
||
import type {Node, Parent} from 'unist' | ||
import type {Test} from 'unist-util-is' | ||
|
||
export type InclusiveDescendant< | ||
Tree extends Node = never, | ||
Found = void | ||
> = Tree extends Parent | ||
? | ||
| Tree | ||
| InclusiveDescendant< | ||
Exclude<Tree['children'][number], Found | Tree>, | ||
Found | Tree | ||
> | ||
: Tree | ||
|
||
type Predicate<Fn, Fallback = never> = Fn extends ( | ||
value: any | ||
) => value is infer Thing | ||
? Thing | ||
: Fallback | ||
|
||
type MatchesOne<Value, Check> = | ||
// Is this a node? | ||
Value extends Node | ||
? // No test. | ||
Check extends null | ||
? Value | ||
: // No test. | ||
Check extends undefined | ||
? Value | ||
: // Function test. | ||
Check extends Function | ||
? Extract<Value, Predicate<Check, Value>> | ||
: // String (type) test. | ||
Value['type'] extends Check | ||
? Value | ||
: // Partial test. | ||
Value extends Check | ||
? Value | ||
: never | ||
: never | ||
|
||
export type Matches<Value, Check> = | ||
// Is this a list? | ||
Check extends any[] | ||
? MatchesOne<Value, Check[keyof Check]> | ||
: MatchesOne<Value, Check> | ||
|
||
/* eslint-enable @typescript-eslint/ban-types */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.