Skip to content

Feature: :exit listener equivalents? #18

@JoshuaKGoldberg

Description

@JoshuaKGoldberg

Initial checklist

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    🤞 phase/openPost is being triaged manually

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions