-
-
Notifications
You must be signed in to change notification settings - Fork 7
Change types to base what visitor
gets on tree
#10
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
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
complex-types.d.ts
Outdated
|
||
export type NodeInTree<T extends Node, A = void> = T extends Parent | ||
? T | NodeInTree<Exclude<T['children'][number], A | T>, A | T> | ||
: T |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Open to different names.
Previously I used SelfAndChildren
.
We could also extract this type. Either in a repo: syntax-tree/typefest
? Or in @types/unist
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about Descendant
? Naming is hard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/syntax-tree/unist#descendant
InclusiveDescendant
to be sure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add type utilities for several things in Unist to types/unist perhaps? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe. Although really NodeInTree
isn’t that bad either actually.
Co-authored-by: Remco Haszing <[email protected]>
26e4d73
to
2997a63
Compare
type: 'text' | ||
value: string | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m thinking it may be better to just use an existing AST for testing to ensure real world compatibility. Actually using @types/mdast
here is probably even less effort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah maybe. I thought it was wise to keep this small and contained
Fixed the types. The “catch-all” overload is no longer needed. Re #10 (comment):
Take for example the docs in How to narrow if (is<List>(node, 'list')) {
// If we’re here, node is List.
//
// `'list'` is compared to `node.type` to make sure they match.
// `true` means a match, `false` means no match.
//
// `<List>` tells TypeScript to ensure `'list'` matches `List.type` and that
// if `'list'` matches both `node.type` and `List.type`, we know that node is
// `List` within this if condition.
} (although this code is for |
`assert` isn’t working for some reason in tsd, so use `unist-util-is` instead.
Nice! |
If you mean that There are some complexities with for example |
expectType<never>(node) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it may be good to have one or two type tests showing it works with plain Node
/Parent
#10 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some tests for implicit trees.
Unfortunately I was wrong. It doesn’t work :'(
visit<SomeNodeType>(anObjectThatLooksLikeANode, 'some-node-type', (node: SomeNodeType) => {})
Does not work, because anObjectThatLooksLikeANode
is probably not assignable to SomeNodeType
Co-authored-by: Christian Murphy <[email protected]>
@ChristianMurphy Still good to go? I think this should be a major? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! 👍
And yes, with some potentially breaking cases around generic Node
, semver major sounds more appropriate.
visitor
based on tree
visitor
gets on tree
Initial checklist
Description of changes
While the signature
visitParents(tree, 'emphasis', (node: Emphasis) => void)
already works (that is, assuming that the tree could potentially include an emphasis node, so not hast, and that the tree does not include antype: emphasis
which is otherwise different fromEmphasis
), the other signatures were not as smart.Given
?
is now filled with any node that isroot
or (recursively) inroot.children
.EDIT: this now also figures out the signature with a test!