Skip to content
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 .skip() and .up() method to forEachDescendant #347

Closed
dsherret opened this issue Jun 14, 2018 · 0 comments
Closed

Add .skip() and .up() method to forEachDescendant #347

dsherret opened this issue Jun 14, 2018 · 0 comments

Comments

@dsherret
Copy link
Owner

dsherret commented Jun 14, 2018

Right now, forEachDescendant looks like this:

node.forEachDescendant((node, stop) => {
    console.log(node.getText());
    if (someCondition)
        stop();
});

It would be nice to add a .skip() function that skips looking at the descendants of only the current node and an up function that does the same, but also skips looking at the next siblings:

node.forEachDescendant((node, traversal) => {
    console.log(node.getText());
    if (someCondition)
        traversal.stop();
    else if (otherCondition)
        traversal.skip(); // skip looking at the descendants
    else if (otherCondition2)
        traversal.up(); // skip and go back up the tree to the parent's next sibling
});

Inspired by: https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md#stopping-traversal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant