1 ### Esrecurse [![Build Status](https://travis-ci.org/estools/esrecurse.svg?branch=master)](https://travis-ci.org/estools/esrecurse)
3 Esrecurse ([esrecurse](https://github.com/estools/esrecurse)) is
4 [ECMAScript](https://www.ecma-international.org/publications/standards/Ecma-262.htm)
5 recursive traversing functionality.
9 The following code will output all variables declared at the root of a file.
12 esrecurse.visit(ast, {
13 XXXStatement: function (node) {
14 this.visit(node.left);
16 this.visit(node.right);
21 We can use `Visitor` instance.
24 var visitor = new esrecurse.Visitor({
25 XXXStatement: function (node) {
26 this.visit(node.left);
28 this.visit(node.right);
35 We can inherit `Visitor` instance easily.
38 class Derived extends esrecurse.Visitor {
50 function DerivedVisitor() {
51 esrecurse.Visitor.call(/* this for constructor */ this /* visitor object automatically becomes this. */);
53 util.inherits(DerivedVisitor, esrecurse.Visitor);
54 DerivedVisitor.prototype.XXXStatement = function (node) {
55 this.visit(node.left);
57 this.visit(node.right);
61 And you can invoke default visiting operation inside custom visit operation.
64 function DerivedVisitor() {
65 esrecurse.Visitor.call(/* this for constructor */ this /* visitor object automatically becomes this. */);
67 util.inherits(DerivedVisitor, esrecurse.Visitor);
68 DerivedVisitor.prototype.XXXStatement = function (node) {
70 this.visitChildren(node);
74 The `childVisitorKeys` option does customize the behaviour of `this.visitChildren(node)`.
75 We can use user-defined node types.
78 // This tree contains a user-defined `TestExpression` node.
80 type: 'TestExpression',
82 // This 'argument' is the property containing the other **node**.
88 // This 'extended' is the property not containing the other **node**.
94 Literal: function (node) {
99 // Extending the existing traversing rules.
101 // TargetNodeName: [ 'keys', 'containing', 'the', 'other', '**node**' ]
102 TestExpression: ['argument']
108 We can use the `fallback` option as well.
109 If the `fallback` option is `"iteration"`, `esrecurse` would visit all enumerable properties of unknown nodes.
110 Please note circular references cause the stack overflow. AST might have circular references in additional properties for some purpose (e.g. `node.parent`).
116 Literal: function (node) {
121 fallback: 'iteration'
126 If the `fallback` option is a function, `esrecurse` calls this function to determine the enumerable properties of unknown nodes.
127 Please note circular references cause the stack overflow. AST might have circular references in additional properties for some purpose (e.g. `node.parent`).
133 Literal: function (node) {
138 fallback: function (node) {
139 return Object.keys(node).filter(function(key) {
140 return key !== 'argument'
149 Copyright (C) 2014 [Yusuke Suzuki](https://github.com/Constellation)
150 (twitter: [@Constellation](https://twitter.com/Constellation)) and other contributors.
152 Redistribution and use in source and binary forms, with or without
153 modification, are permitted provided that the following conditions are met:
155 * Redistributions of source code must retain the above copyright
156 notice, this list of conditions and the following disclaimer.
158 * Redistributions in binary form must reproduce the above copyright
159 notice, this list of conditions and the following disclaimer in the
160 documentation and/or other materials provided with the distribution.
162 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
163 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
165 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
166 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
167 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
168 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
169 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
170 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
171 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.