Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Rich-Harris/estree-walker
Traverse an ESTree-compliant AST
https://github.com/Rich-Harris/estree-walker
Last synced: about 1 month ago
JSON representation
Traverse an ESTree-compliant AST
- Host: GitHub
- URL: https://github.com/Rich-Harris/estree-walker
- Owner: Rich-Harris
- License: mit
- Created: 2015-04-09T12:56:33.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-02-05T09:30:56.000Z (10 months ago)
- Last Synced: 2024-10-29T13:10:08.806Z (about 1 month ago)
- Language: JavaScript
- Size: 1.95 MB
- Stars: 391
- Watchers: 5
- Forks: 37
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crafted-nodejs - estree-walker - Traverse an ESTree-compliant AST (Packages / Others)
- awesome-nodejs - estree-walker - Traverse an ESTree-compliant AST. ![](https://img.shields.io/github/stars/Rich-Harris/estree-walker.svg?style=social&label=Star) (Repository / AST)
- awesome-list - estree-walker - compliant AST | Rich-Harris | 192 | (JavaScript)
README
# estree-walker
Simple utility for walking an [ESTree](https://github.com/estree/estree)-compliant AST, such as one generated by [acorn](https://github.com/marijnh/acorn).
## Installation
```bash
npm i estree-walker
```## Usage
```js
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](https://github.com/estools/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