-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
Add generic to parent parameter in visitor callback #30
Conversation
This comment has been minimized.
This comment has been minimized.
@@ -20,7 +20,7 @@ | |||
* @callback Visitor | |||
* @param {V} node Found node | |||
* @param {number|null} index Position of `node` in `parent` | |||
* @param {Parent|null} parent Parent of `node` | |||
* @param {import('unist').Parent<V>|null} parent Parent of `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.
This isn't the case though.
For example if you have an AST like https://astexplorer.net/#/gist/07590330c6b74c87dc6ca4c170f71bf0/c6107a73d37212d1ea8b1b6cd18cd1cef230527d
And have a visitor going to emphasis
nodes
visit(ast, 'emphasis', (node, position, parent) => {/* do something */})
It would be incorrect to say Parent<Emphasis>
, the Parent
also contains Text
, Strong
, and InlineCode
First of all I think this library should stay in sync with its parent (ha!), Furthermore, this should be smarter! As we’re already inferring which children exist in |
I think this is fairly doable, but it requires some type-fu. I think the check should be the other way around: The parent type can be similar to
Punny! 😄 Jokes aside, I doubt if this is possible for As for the current implementation I agree with @ChristianMurphy. |
Previously, a basic `Parent` from `@types/unist` was used, as an array, for the second parameter of a visitor (`parents`). This changes that to instead use an array of descendants in `tree` which implement the abstract `Parent` interface. This is not perfect, because several parents can’t be found in certain nodes practically, but it will at least help folks narrow. Related to syntax-tree/unist-util-visit#30.
Previously, a basic `Parent` from `@types/unist` was used, as an array, for the second parameter of a visitor (`parents`). This changes that to instead use an array of descendants in `tree` which implement the abstract `Parent` interface. This is not perfect, because several parents can’t be found in certain nodes practically, but it will at least help folks narrow. Related to syntax-tree/unist-util-visit#30. Closes GH-11. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
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. Related-to: syntax-tree/unist-util-visit-parents#11.
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.
Initial checklist
Description of changes
Parent should be superset of Child