Add generic to parent parameter in visitor callback#30
Add generic to parent parameter in visitor callback#30vvenv wants to merge 1 commit intosyntax-tree:mainfrom vvenv:main
Conversation
This comment has been minimized.
This comment has been minimized.
| * @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.
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 <[email protected]>
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