RegJSTraverse
Traverse the RegJS AST (a AST for JavaScript's regular expressions).
Installation
npm install regjstraverse
Testing
To run the tests, run the following command:
npm test
Background
A RegJS AST can be generated by using the regjsparser
library:
// Create the AST for the regular expression `/abc/`.var ast = ;
Example Usage
regjstraverse
makes it easy to traverse the regular expression AST using the
enter
and leave
functions:
var regjstraverse = ; regjstraverse
When traversing the nodes, it's possible to skip the sub-nodes of the current
node by calling the this.skip()
method or returning regjstraverse.VisitorOption.Skip
:
var regjstraverse = ; regjstraverse
Breaking at the current point in the tree traversal is possible by invoking break
:
var regjstraverse = ; regjstraverse
Note: After invoking break
stops the entire tree traversal - no further
calls to enter
or leave
are made afterwards.
Replacing the current visited node is doable using the replace
function:
var regjstraverse = ;var parse = parse; var newAst = regjstraverse
Note: if the enter
function returns a new AST, the subnodes
of the new-replaced AST are visited. Example:
var regjstraverse = ;var regjsparser = ; var rawValues = '';var ast = regjstraverse;// Tests if `enter` was called on the replaced node.assert;