Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including visitor with all npm packages installed. Try it out:

var visitor = require("visitor")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

visitor v0.0.0

walk and transform ASTs with references to parent nodes

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

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free