View on GitHub


Selector engine for JavaScript syntax trees

Download this project as a .zip file Download this project as a tar.gz file


Selector engine for JavaScript syntax trees. Lets you write queries against abstract syntax trees generated by Esprima ( to pull out specific expressions from the program.

There are three basic kinds of query components you can use:

The query language also supports contextual property modifiers. These modifiers can be added to the last query component to let you refine what object gets put into the results list. Property modifiers are added with a semicolon: <component>:property:modifier


You can pull all literal values in your program by using the query "Literal".

To get the initialization expression of a variable named "theValue" (ie. var theValue = 4+2;)

var query = new foresta("#theValue");
var expression = query.results[0].init; // the binary expression for '4+2'

All global variables declared in this javascript program can be selected with the selector "Program VariableDeclaration VariableDeclarator"

Contextual Property Modifiers can be useful to select a specific expression that is referred to by an identifer. For example, given the following expression:

var a = {
  update: function() {
    var s;

You can get the FunctionExpression by issuing this query: ObjectExpression Property #update:parent:value.