Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/saintedlama/estools
Provides a set of utility function to traverse, filter and map esprima ASTs
https://github.com/saintedlama/estools
Last synced: 2 months ago
JSON representation
Provides a set of utility function to traverse, filter and map esprima ASTs
- Host: GitHub
- URL: https://github.com/saintedlama/estools
- Owner: saintedlama
- License: mit
- Created: 2014-08-15T08:12:57.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-05-31T21:36:43.000Z (over 7 years ago)
- Last Synced: 2024-10-12T12:25:40.763Z (3 months ago)
- Language: JavaScript
- Size: 18.6 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# estools [![Build Status](https://travis-ci.org/saintedlama/estools.svg?branch=master)](https://travis-ci.org/saintedlama/estools) [![Coverage Status](https://coveralls.io/repos/saintedlama/estools/badge.png?branch=master)](https://coveralls.io/r/saintedlama/estools?branch=master)
Estools provides a set of utility functions to traverse, filter and map esprima ASTs.
npm install estools --save
var estools = require('estools')
# API
## traverse(ast, visitor)
Traverses all tree nodes of an AST and calls visitor functions passing the AST node.ast is the result of esprima.parse, visitor may be a function or a visitor object.
The visitor object may define the following functions
### enter(node, parent)
Called when entering a node. The current node and parent node are passed as parameter
Example
traverse(ast, {
enter : function(node, parent) {
console.log(node.type); // Print the node type to std out
}
});### leave(node, parent)
Called when leaving a node. The current node and parent node are passed as parameter
Example
traverse(ast, {
leave : function(node, parent) {
console.log(node.type); // Print the node type to std out
}
});### visit(node, parent, next)
Called after `enter` and before `leave`. The current node and a parent node are passed as parameter. Additionally
a function `next` is passed. The `visit` function is responsible to call `next` to visit child nodes. To skip
child nodes just omit the call to `visit`.Example
traverse(ast, {
visit : function(node, parent, next) {
next();
}
});Example skipping nodes
traverse(ast, {
visit : function(node, parent, next) {
if (node.type != 'FunctionExpression') { // Skip child nodes of function expressions
next();
}
}
});## filter(ast, filterObj)
Filters AST nodes and returns matching AST nodes as a flat list.
## map(ast, mappingFunction)
Maps ASTs and nodes to a normalized tree with node having child nodes stored in a nodes field array.