Skip to content
Traverse an ESTree-compliant AST
JavaScript
Branch: master
Clone or download
Latest commit 8ee5d23 May 28, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Merge pull request #9 from Conduitry/patch-3 May 27, 2019
test -> v0.5.1 Nov 22, 2017
.eslintrc tidy up Apr 23, 2016
.gitignore switch from babel to buble Apr 23, 2016
CHANGELOG.md -> v0.6.1 May 27, 2019
README.md update acorn repo link May 21, 2019
index.d.ts add context to WalkerListener Nov 22, 2018
package-lock.json -> v0.6.1 May 27, 2019
package.json -> v0.6.1 May 27, 2019
rollup.config.js
yarn.lock

README.md

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: function ( node, parent, prop, index ) {
    // some code happens
  },
  leave: function ( 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.

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

You can’t perform that action at this time.