-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, a basic `Parent` from `@types/unist` was used for the third parameter of a visitor (`parent`). This changes that to instead use an array of descendants in `tree` which implement the abstract `Parent` interface and can have `node` as a child. Closes GH-30. Closes GH-31. Related-to: syntax-tree/unist-util-visit-parents#11.
- Loading branch information
Showing
6 changed files
with
79 additions
and
32 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,6 @@ | ||
.DS_Store | ||
*.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,54 @@ | ||
import type {Node, Parent} from 'unist' | ||
import type {Test} from 'unist-util-is' | ||
import type { | ||
VisitorResult, | ||
Matches, | ||
InclusiveDescendant | ||
} from 'unist-util-visit-parents/complex-types' | ||
|
||
/** | ||
* Called when a node (matching test, if given) is found. | ||
* Visitors are free to transform node. | ||
* They can also transform the parent of node (the last of ancestors). | ||
* Replacing node itself, if `SKIP` is not returned, still causes its descendants to be visited. | ||
* If adding or removing previous siblings (or next siblings, in case of reverse) of node, | ||
* visitor should return a new index (number) to specify the sibling to traverse after node is traversed. | ||
* Adding or removing next siblings of node (or previous siblings, in case of reverse) | ||
* is handled as expected without needing to return a new index. | ||
* Removing the children property of an ancestor still results in them being traversed. | ||
*/ | ||
export type Visitor< | ||
Visited extends Node = Node, | ||
Ancestor extends Parent = Parent | ||
> = ( | ||
node: Visited, | ||
index: Visited extends Node ? number | null : never, | ||
parent: Ancestor extends Node ? Ancestor | null : Ancestor | ||
) => VisitorResult | ||
|
||
type ParentsOf< | ||
Ancestor extends Node, | ||
Child extends Node | ||
> = Ancestor extends Parent | ||
? Child extends Ancestor['children'][number] | ||
? Ancestor | ||
: never | ||
: never | ||
|
||
type BuildVisitorFromMatch< | ||
Visited extends Node, | ||
Ancestor extends Parent | ||
> = Visitor<Visited, ParentsOf<Ancestor, Visited>> | ||
|
||
type BuildVisitorFromDescendants< | ||
Descendant extends Node, | ||
Check extends Test | ||
> = BuildVisitorFromMatch< | ||
Matches<Descendant, Check>, | ||
Extract<Descendant, Parent> | ||
> | ||
|
||
export type BuildVisitor< | ||
Tree extends Node = Node, | ||
Check extends Test = string | ||
> = BuildVisitorFromDescendants<InclusiveDescendant<Tree>, Check> |
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
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