# tree-visitor

> Visit nodes in the tree

Latest version **1.0.0** (published 2013-08-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install tree-visitor
pnpm add tree-visitor
yarn add tree-visitor
bun add tree-visitor
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2013-08-19 |
| First published | 2013-07-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Glen Huang |
| Maintainers | curvedmark |
| Keywords | ast, tree, visitor, compiler |

## Links

- npm: https://www.npmjs.com/package/tree-visitor
- Repository: https://github.com/curvedmark/tree-visitor
- Issues: https://github.com/curvedmark/tree-visitor/issues
- npm.io page: https://npm.io/package/tree-visitor

## 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

- 1.0.0 (latest) — 2013-08-19
- 0.6.0 — 2013-08-04
- 0.5.0 — 2013-08-02
- 0.4.0 — 2013-07-28
- 0.3.0 — 2013-07-26

## README

# Tree Visitor

Visit nodes in the tree.

## Example

Visit a single node:

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

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

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

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

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

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