🌜
🌞
estree-walker

estree-walker

v3.0.1

Traverse an ESTree-compliant AST

npm install estree-walker

README

estree-walker

Simple utility for walking an ESTree-compliant AST, such as one generated by acorn.

Installation

npm i estree-walker

Usage

var walk = require('estree-walker').walk;
var acorn = require('acorn');

ast = acorn.parse(sourceCode, options); // https://github.com/acornjs/acorn

walk(ast, {
  enter(node, parent, prop, index) {
    // some code happens
  },
  leave(node, parent, prop, index) {
    // some code happens
  }
});

Inside the enter function, calling this.skip() will prevent the node's children being walked, or the leave function (which is optional) being called.

Call this.replace(new_node) in either enter or leave to replace the current node with a new one.

Call this.remove() in either enter or leave to remove the current node.

Why not use estraverse?

The ESTree spec is evolving to accommodate ES6/7. I've had a couple of experiences where estraverse was unable to handle an AST generated by recent versions of acorn, because it hard-codes visitor keys.

estree-walker, by contrast, simply enumerates a node's properties to find child nodes (and child lists of nodes), and is therefore resistant to spec changes. It's also much smaller. (The performance, if you're wondering, is basically identical.)

None of which should be taken as criticism of estraverse, which has more features and has been battle-tested in many more situations, and for which I'm very grateful.

License

MIT

General

License
MIT
Typescript Types
Built-in
Tree-shakeable
Yes

Popularity

GitHub Stargazers
279
Community Interest
225
Number of Forks
30

Maintenance

Commits
10/219/2203
Last Commit
Dec 20, 2021
Open Issues
2
Closed Issues
4
Open Pull Requests
4
Closed Pull Requests
8

Versions

Versions Released
10/219/2201
Latest Version Released
Current Tags
latest3.0.1

Dependencies

Dependencies (0)
Dev Dependencies (3)

Contributors

Rich-Harris
Rich-Harris
Commits: 62
kristoferbaxter
kristoferbaxter
Commits: 10
Conduitry
Conduitry
Commits: 4
mrkishi
mrkishi
Commits: 4
tanhauhau
tanhauhau
Commits: 2
marvinhagemeister
marvinhagemeister
Commits: 1
forivall
forivall
Commits: 1
TrySound
TrySound
Commits: 1
larsrh
larsrh
Commits: 1