Recursively walk and transform EcmaScript AST
Recursively walk and add extra information/helpers to Esprima / Mozilla SpiderMonkey Parser API compatible AST.
The main difference between other tools is that it also keeps information about tokens and white spaces and it is meant to be used to transform the tokens and not the string values itself.
This library is specially useful for non-destructive AST manipulation.
This module was heavily inspired by node-falafel and node-burrito but I needed more information than what is currently available on falafel (specially about tokens, empty lines and white spaces) and also needed to do the node traversing on the opposite order (start from leaf nodes). The amount of changes required to introduce the new features and the differences on the concept behind the tool justified a new project.
It was created mainly to be used on esformatter.
Besides all the regular tokens returned by
esprima we also add a few more
that are important for non-destructive transformations:
- Can store multiple white spaces (tabs are considered white space, line breaks not). Important if you want to do non-destructive replacements that are white-space sensitive.
- Multiple subsequent white spaces are treated as a single token.
It's way easier to rebuild the JS string if the tokens already have line breaks and comments. It's also easier to identify if previous/next/current token is a LineBreak or Comment (sometimes needed for non-destructive transformations).
Rocambole structure might change in the future to keep the extraneous tokens
tokens array and also add an option to toggle the behavior.
Each Node have the following extra properties/methods:
Each token also have:
BlockComment also have:
To get a better idea of the generated AST structure try out rocambole-visualize.
You should treat the tokens as a linked list instead of reading the
ast.tokens array (inserting/removing items from a linked list is very cheap
and won't break the loop). You should grab a reference to the
token.next until you find the desired token or reach the end of the
program. To loop between all tokens inside a node you can do like this:
var token = nodestartToken;while token !== nodeendTokennextdoStuffWithTokentoken;token = tokennext;
toString loops through all tokens between
node.endToken grabbing the
token.raw (used by comments) or
properties. To implement a method similar to falafel
update() you can do
var newToken =type : 'Custom' // can be anything (not used internally)value : str;// update linked list referencesif nodestartTokenprevnodestartTokenprevnext = newToken;newTokenprev = nodestartTokenprev;if nodeendTokennextnodeendTokennextprev = newToken;newTokennext = nodeendTokennext;nodestartToken = nodeendToken = newToken;
I plan to create helpers as separate projects when possible.
- rocambole-token: helpers for token manipulation/traversal
- rocambole-node: helpers for node manipulation/traversal
There are a few nice helpers on esformatter to deal with indentation, white spaces and line breaks that might be useful.
Parses a string and instrument the AST with extra properties/methods.
var rocambole = require'rocambole';var ast = rocamboleparsestring;console.log aststartToken ;// to get a string representation of all tokens call toString()console.log asttoString ;
moonwalk() starts at the leaf nodes and go down the tree until it reaches
the root node (
Program). Each node will be traversed only once.
rocambolemoonwalkastif nodetype == 'ArrayExpression'console.log nodedepth +': '+ nodetoString ;;
Program [#18]`-FunctionDeclaration [#16]|-BlockStatement [#14]| |-IfStatement [#12]| | |-BynaryExpression [#9]| | | |-Identifier [#4]| | | `-Literal [#5]| | `-BlockStatement [#10]| | `-ExpressionStatement [#6]| | `-AssignmentExpression [#3]| | |-Identifier [#1 walk starts here]| | `-Literal [#2]| `-VariableDeclaration [#13]| `-VariableDeclarator [#11]| |-Identifier [#7]| `-Literal [#8]`-ReturnStatement [#17]`-Identifier [#15]
This behavior is very different from node-falafel and node-burrito.
It loops through all nodes on the AST starting from the root node (
Besides the regular unit tests we also use istanbul to generate code coverage reports, tests should have at least 95% code coverage for statements, branches and lines and 100% code coverage for functions or travis build will fail.
We do not run the coverage test at each call since it slows down the
performnace of the tests and it also makes it harder to see the test results.
To execute tests and generate coverage report call
npm test --coverage, for
regular tests just do
Coverage reports are not committed to the repository since they will change at
npm test --coverage call.
esprimato v2.0 because of ES6 features and to avoid
esprima-fbbug related to RegExp.
rocambole.walkto avoid confusions.
esprima-fbbecause of ES6 features.
- really handle sparse arrays (eg.
[,]), fixes moonwalk. (#15)
- handle sparse arrays (eg.
- only add
WhiteSpaceis on the start of a line.
toStringto empty programs AST (#16)
- fix error if input is empty (#12)
- support anything that implements
toString()as input (#13)
BlockCommentwhen prev token is not
rocambole.parse()performance by 4500%. (#4)
rocambole.moonwalk()performance by 11000%.
- fix consecutive comments before start of program. (#3)
- avoid recursion over comments.
- fix weird bug on esformatter introduced on v0.1.1 related to
- Improve token manipulation methods behavior (
- Initial release