Eric Foster 35b96bc934 initial commit | 6 years ago | |
---|---|---|
.. | ||
dist | 6 years ago | |
src | 6 years ago | |
CHANGELOG.md | 6 years ago | |
README.md | 6 years ago | |
index.d.ts | 6 years ago | |
package.json | 6 years ago |
Simple utility for walking an ESTree-compliant AST, such as one generated by acorn.
npm i estree-walker
var walk = require( 'estree-walker' ).walk;
var acorn = require( 'acorn' );
ast = acorn.parse( sourceCode, options ); // https://github.com/marijnh/acorn
walk( ast, {
enter: function ( node, parent ) {
// some code happens
},
leave: function ( node, parent ) {
// 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.
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.
MIT