ast-traverse

WebJar for ast-traverse

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

ast-traverse
Last Version

Last Version

0.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

ast-traverse
WebJar for ast-traverse
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/olov/ast-traverse

Download ast-traverse

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/ast-traverse/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>ast-traverse</artifactId>
    <version>0.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/ast-traverse/
implementation 'org.webjars.npm:ast-traverse:0.1.1'
// https://jarcasting.com/artifacts/org.webjars.npm/ast-traverse/
implementation ("org.webjars.npm:ast-traverse:0.1.1")
'org.webjars.npm:ast-traverse:jar:0.1.1'
<dependency org="org.webjars.npm" name="ast-traverse" rev="0.1.1">
  <artifact name="ast-traverse" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='ast-traverse', version='0.1.1')
)
libraryDependencies += "org.webjars.npm" % "ast-traverse" % "0.1.1"
[org.webjars.npm/ast-traverse "0.1.1"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

ast-traverse.js

Simple but flexible AST traversal with pre and post visitors. Works in node and browsers.

Usage

// ast is a Mozilla Parser API compatible structure
// generated by Esprima or another parser
var ast = require("esprima").parse("f(1, x) + 2");

var traverse = require("ast-traverse");

// print AST node types, pre-order (node first, then its children)
traverse(ast, {pre: function(node, parent, prop, idx) {
    console.log(node.type + (parent ? " from parent " + parent.type +
        " via " + prop + (idx !== undefined ? "[" + idx + "]" : "") : ""));
}});
console.log();
/*
 =>
 Program
 ExpressionStatement from parent Program via body[0]
 BinaryExpression from parent ExpressionStatement via expression
 CallExpression from parent BinaryExpression via left
 Identifier from parent CallExpression via callee
 Literal from parent CallExpression via arguments[0]
 Identifier from parent CallExpression via arguments[1]
 Literal from parent BinaryExpression via right
 */


// you can also visit post-order, or both
// all four arguments are provided to both visitors (left out unused below)
var indent = 0;
traverse(ast, {
    pre: function(node) {
        console.log(Array(indent + 1).join(" ") + node.type);
        indent += 4;
    },
    post: function() {
        indent -= 4;
    }
});
console.log();
/*
=>
 Program
     ExpressionStatement
         BinaryExpression
             CallExpression
                 Identifier
                 Literal
                 Identifier
             Literal
*/


// return false from the pre-visitor to skip traversing its children
// throw an exception to abort traversal


// by default node property names beginning with $ are skipped
// but you can supply your own skipProperty function instead
traverse(ast, {
    pre: function(node) {
        console.log(node.type);
    },
    skipProperty: function(prop, node) {
        return prop === "parent" || prop === "expression";
    }
});
/*
=>
 Program
 ExpressionStatement
*/

Installation

Node

Install using npm

npm install ast-traverse
var traverse = require("ast-traverse");

Browser

Clone the repo and include it in a script tag

git clone https://github.com/olov/ast-traverse.git
<script src="ast-traverse/ast-traverse.js"></script>

Versions

Version
0.1.1