# ast-traverse

> simple but flexible AST traversal with pre and post visitors

Latest version **0.1.1** (published 2013-09-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install ast-traverse
pnpm add ast-traverse
yarn add ast-traverse
bun add ast-traverse
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2013-09-20 |
| First published | 2013-09-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 36 |
| Author | Olov Lassus |
| Maintainers | olov |
| Keywords | ast, traverse, traversal, walk, visit, visitor, esprima |

## Links

- npm: https://www.npmjs.com/package/ast-traverse
- Repository: https://github.com/olov/ast-traverse
- npm.io page: https://npm.io/package/ast-traverse

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2013-09-20
- 0.1.0 — 2013-09-01

## README

# ast-traverse.js
Simple but flexible AST traversal with pre and post visitors.
Works in node and browsers.



## Usage
```javascript
// 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

```javascript
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

```html
<script src="ast-traverse/ast-traverse.js"></script>
```

---
_Source: https://npm.io/package/ast-traverse · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
