-
-
Notifications
You must be signed in to change notification settings - Fork 7
Description
Initial checklist
- I read the support docs
- I read the contributing guide
- I agree to follow the code of conduct
- I searched issues and discussions and couldn’t find anything (or linked relevant results below)
Problem
This library is a great clean way to run a visitor on either preorder (NLR) or reverse preorder (NRL) per API > visit. Thanks for making it! ❤️
I'm using this to build visitors in a linter's language support for YAML nodes and other languages. Visiting in NLR works fine, but we'd also like to have "exit" visitors akin to ESLint's :exit listeners. As in, essentially have both NLR and NRL in the same call, with intermixed enter & exit calls: not running all enter calls and then all exit calls.
Current solutions
I couldn't figure out how to do it from this package or ones linked from its README.md such as unist-util-visit. I'm not exhaustively familiar with the unist ecosystem, though. Is there a package that could be used for this? Say, a lower-level-ish one that gives more fine-grained control over walking?
Proposed solutions
As a vague first suggestion, how about allowing visit to either be a function (current behavior) or an object with enter and exit keys?
visit(root, {
enter: (node) => console.log("Entering:", node),
exit: (node) => console.log("Exiting:", node),
});As for an implementation suggestion, I think the :exit addition could run at the bottom of the visit() function after visiting children (subresult) and before returning.