lukeed / lukeed/astray

Visitor for any node for stopping walking

Open
#3 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
195
Forks
7
PR merge metrics
No merged PRs in 30d

Description

I have the following use case:

1. Collect identifiers from descendants of a particular node.
2. Stop walking, when a particular node is reached.

Implementing the first is easy:

```js
walk(ast, {
Identifier(node, state) {
// collect the node
}
})
```

But how to avoid further traversing a possibly large AST, when a particular node is reached, and that node may not be an identifier?

With a library, which does not offer visitors classified by type, but just a single callback for every node, this would be easy:

```js
traverse(ast, node => {
if (node === stopNode) return false // tell the traverse to stop

if (node.type === 'Identifier') {
// collect the node
}

return true // tell the traverse to continue
})
```

Do you think that astray could be extended to offer a visitor catch any node type?

For example, always called:

```js
walk(ast, {
Identifier(node, state) {
// collect the node
}
'*'(node, state) {
if (node === stopNode) return SKIP
}
})
```

For example, called as a fallback if a concrete visitor is not available:

```js
walk(ast, {
Identifier(node, state) {
if (node === stopNode) return SKIP

// collect the node
}
'*'(node, state) {
if (node === stopNode) return SKIP
}
})
```

The "wildcard" visitor would be a general feature, usable not only to stop the walking. Alternatively, a method specific for stopping the walking could be added, either a `stopNode` property, or a callback like `shouldStop(node): bool`.

What do you think?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading astray's walk API and its visitor-dispatch behavior, since the issue names no files or tests. Compare the wildcard, fallback, and stop-specific alternatives in the report; done should define and verify how traversal stops at a selected node while identifier collection continues elsewhere.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.