Visit nodes in the tree
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
LICENSE
README.md
package.json

README.md

Tree Visitor

Visit nodes in the tree.

Example

Visit a single node:

var Visitor = require('tree-visitor');
var node = { type: 'number', value: 1 };

function MyVisitor() {}
MyVisitor.prototype = new Visitor();

MyVisitor.prototype.visit_number = function (number) {
	console.log(number.value);
};

var myVisitor = new MyVisitor();
myVisitor.visit(node); // 1

Visit an array of nodes:

var Visitor = require('tree-visitor');
var nodes = [
	{ type: 'number', value: 1 },
	{ type: 'string', value: 'abc', quote: '"' }
];

function MyVisitor() {}
MyVisitor.prototype = new Visitor();

MyVisitor.prototype.visit_number = function (number) {
	console.log(number.value);
};

MyVisitor.prototype.visit_string = function (string) {
	console.log(string.quote + string.value + string.quote);
};

var myVisitor = new MyVisitor();
myVisitor.visit(nodes); // 1 "abc"

Visit nested nodes:

var Visitor = require('tree-visitor');
var number = { type: 'number', value: 1 };
var string = { type: 'string', value: 'abc' };
var expression = {
		type: 'binaryExpression',
		operator: '+',
		left: number,
		right: string
};

function MyVisitor() {}
MyVisitor.prototype = new Visitor();

MyVisitor.prototype.visit_binaryExpression = function (binaryExpression) {
	this.visit(binaryExpression.left);
	console.log(binaryExpression.operator);
	this.visit(binaryExpression.right);
};

MyVisitor.prototype.visit_number = function (number) {
	console.log(number.value);
};

MyVisitor.prototype.visit_string = function (string) {
	console.log(string.quote + string.value + string.quote);
};

var myVisitor = new MyVisitor();
myVisitor.visit(expression); // 1 + "abc"

One method to rule them all:

var Visitor = require('tree-visitor');

var nodes = [
	{ type: 'number', value: 1 },
	{ type: 'string', value: 'abc', quote: '"' }
];

function MyVisitor() {}
MyVisitor.prototype = new Visitor();

MyVisitor.prototype.visit_node = function (node) {
	console.log(node.value);
};

var myVisitor = new MyVisitor();
myVisitor.visit(nodes); // 1 abc

API

var Visitor = require('tree-visitor');

function MyVisitor() {}
MyVisitor.prototype = new Visitor();

MyVisitor.prototype.visit_nodeType = function (node) {};

var myVisitor = new MyVisitor();
myVisitor.visit(node);

Visitor should be "subclassed" by a constructor, the new constructor should have methods like .visit_nodeType(), and the node being visited should have a type property. If the value of type and the string after .visit_ matches, the node will be passed to that method when calling .visit(node).

Either a single node (any js object), or an array of nodes can be visited. In the latter case, the nodes are visited sequentially.

.visit_node() is a special method, if the node being visited doesn't have a corresponding method, it's passed to .visit_node(). If it does, however, it will not be passed to this method.

Nodes that are not a plain object (e.g., string, null, etc) or don't have a type property are ignored (i.e., it's not passed to any method, not even .visit_node()).

When visiting a single node, the returning value of .visit(node) is the returning value of the corresponding method, or the node itself if it doesn't have a corresponding method.

When visiting an array of nodes, the returning value of .visit(nodes) is the original array (i.e., nodes).