UNPKG

2.99 kBMarkdownView Raw
1### Esrecurse [![Build Status](https://secure.travis-ci.org/estools/esrecurse.png)](http://travis-ci.org/estools/esrecurse)
2
3Esrecurse ([esrecurse](http://github.com/estools/esrecurse)) is
4[ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm)
5recursive traversing functionality.
6
7### Example Usage
8
9The following code will output all variables declared at the root of a file.
10
11```javascript
12esrecurse.visit(ast, {
13 XXXStatement: function (node) {
14 this.visit(node.left);
15 // do something...
16 this.visit(node.right);
17 }
18});
19```
20
21We can use `Visitor` instance.
22
23```javascript
24var visitor = new esrecurse.Visitor({
25 XXXStatement: function (node) {
26 this.visit(node.left);
27 // do something...
28 this.visit(node.right);
29 }
30});
31
32visitor.visit(ast);
33```
34
35We can inherit `Visitor` instance easily.
36
37```javascript
38function DerivedVisitor() {
39 esrecurse.Visitor.call(/* this for constructor */ this, /* visitor object. */ this);
40}
41util.inherits(DerivedVisitor, esrecurse.Visitor);
42DerivedVisitor.prototype.XXXStatement = function (node) {
43 this.visit(node.left);
44 // do something...
45 this.visit(node.right);
46};
47```
48
49And you can invoke default visiting operation inside custom visit operation.
50
51```javascript
52function DerivedVisitor() {
53 esrecurse.Visitor.call(/* this for constructor */ this, /* visitor object. */ this);
54}
55util.inherits(DerivedVisitor, esrecurse.Visitor);
56DerivedVisitor.prototype.XXXStatement = function (node) {
57 // do something...
58 this.visitChildren(node);
59};
60```
61
62### License
63
64Copyright (C) 2014 [Yusuke Suzuki](http://github.com/Constellation)
65 (twitter: [@Constellation](http://twitter.com/Constellation)) and other contributors.
66
67Redistribution and use in source and binary forms, with or without
68modification, are permitted provided that the following conditions are met:
69
70 * Redistributions of source code must retain the above copyright
71 notice, this list of conditions and the following disclaimer.
72
73 * Redistributions in binary form must reproduce the above copyright
74 notice, this list of conditions and the following disclaimer in the
75 documentation and/or other materials provided with the distribution.
76
77THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
78AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
81DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
82(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
83LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
84ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
85(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
86THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.