estree-walker
Traverse an ESTree-compliant AST
Last updated 5 months ago by rich_harris .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install estree-walker 
SYNC missed versions from official npm registry.

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.

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

Current Tags

  • 2.0.1                                ...           latest (5 months ago)

22 Versions

  • 2.0.1                                ...           5 months ago
  • 2.0.0                                ...           5 months ago
  • 1.0.1                                ...           6 months ago
  • 1.0.0                                ...           7 months ago
  • 0.9.0                                ...           8 months ago
  • 0.8.1                                ...           10 months ago
  • 0.8.0                                ...           10 months ago
  • 0.7.0                                ...           10 months ago
  • 0.6.1                                ...           a year ago
  • 0.6.0                                ...           2 years ago
  • 0.5.2                                ...           2 years ago
  • 0.5.1                                ...           3 years ago
  • 0.5.0                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 0.3.1                                ...           3 years ago
  • 0.3.0                                ...           4 years ago
  • 0.2.1                                ...           4 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.3                                ...           5 years ago
  • 0.1.2                                ...           5 years ago
  • 0.1.1                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 2,369
This Week 2,369
This Month 16,160
Last Day 1,120
Last Week 21,152
Last Month 89,950
Dependencies (0)
None
Dev Dependencies (5)
Dependents (191)

Copyright 2014 - 2017 © taobao.org |