Skip to content

ttaubert/node-visitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

visitor

walk and transform ASTs with references to parent nodes

browser support

build status

This module is heavily inspired by astw, falafel and acorn. It allows to walk and update ASTs providing references to parent nodes.

examples

var visit = require("visitor");
var assert = require("assert");

visit("var xs = [1, 2, 3];", {
  ArrayExpression: function (node) {
    assert.equal(node.source(), "[1, 2, 3]");
    assert.equal(node.parent.type, "VariableDeclarator");
    assert.equal(node.parent.parent.type, "VariableDeclaration");
  }
});
var visit = require("visitor");
var assert = require("assert");

var result = visit("1 + 2", {
  BinaryExpression: function (node) {
    if (node.operator === "+" &&
        node.left.type === "Literal" &&
        node.right.type === "Literal") {
      node.update(node.left.value + node.right.value);
    }
  }
});

assert(result === "3", "combined both sides of the expression");

install

With npm do:

npm install visitor

license

MIT

About

walk and transform ASTs with references to parent nodes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published