node package manager
Love JavaScript? Your insights can make it even better. Take the 2017 JavaScript Ecosystem Survey ยป


A more tree-like AST builder for reflect.js.

Tree Builder

The tree builder creates default nodes extended with properties and methods for traversing and manipulating the tree.


var Reflect = require('reflect');
var treeBuilder = require('reflect-builder');

var source = "a + b + c(5)";
var ast = Reflect.parse(source, {builder: treeBuilder});

function visit (node, pre, post) {
    if (pre) pre(node);
    node.children().forEach(function (child) {
        visit(child, pre, post);
    if (post) post(node);

visit(ast, null, function (n) { console.log(n.type); });

Node Properties

See the Mozilla documentation for the default node APIs. The following are additional properties the tree builder provides.


The node's parent.


Returns the children of the node. If an array of nodes are passed as an argument, they replace node's current set of children.


Removes the specified child from node's set of children.

node.replace(delinquent, adopted)

Removes the delinquent and adds the adopted child in its place.


For block type nodes, such as FunctionDeclarations, Programs, and Blocks, this appends a child to its list of statements.


MIT X License