1 ### Estraverse [![Build Status](https://secure.travis-ci.org/Constellation/estraverse.png)](http://travis-ci.org/Constellation/estraverse)
3 Estraverse ([estraverse](http://github.com/Constellation/estraverse)) is
4 [ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm)
5 traversal functions from [esmangle project](http://github.com/Constellation/esmangle).
9 You can find usage docs at [wiki page](https://github.com/Constellation/estraverse/wiki/Usage).
13 The following code will output all variables declared at the root of a file.
16 estraverse.traverse(ast, {
17 enter: function (node, parent) {
18 if (node.type == 'FunctionExpression' || node.type == 'FunctionDeclaration')
19 return estraverse.VisitorOption.Skip;
21 leave: function (node, parent) {
22 if (node.type == 'VariableDeclarator')
23 console.log(node.id.name);
28 We can use `this.skip`, `this.remove` and `this.break` functions instead of using Skip, Remove and Break.
31 estraverse.traverse(ast, {
32 enter: function (node) {
38 And estraverse provides `estraverse.replace` function. When returning node from `enter`/`leave`, current node is replaced with it.
41 result = estraverse.replace(tree, {
42 enter: function (node) {
43 // Replace it with replaced.
44 if (node.type === 'Literal')
50 By passing `visitor.keys` mapping, we can extend estraverse traversing functionality.
53 // This tree contains a user-defined `TestExpression` node.
55 type: 'TestExpression',
57 // This 'argument' is the property containing the other **node**.
63 // This 'extended' is the property not containing the other **node**.
66 estraverse.traverse(tree, {
67 enter: function (node) { },
69 // Extending the exising traversing rules.
71 // TargetNodeName: [ 'keys', 'containing', 'the', 'other', '**node**' ]
72 TestExpression: ['argument']
77 By passing `visitor.fallback` option, we can control the behavior when encountering unknown nodes.
79 // This tree contains a user-defined `TestExpression` node.
81 type: 'TestExpression',
83 // This 'argument' is the property containing the other **node**.
89 // This 'extended' is the property not containing the other **node**.
92 estraverse.traverse(tree, {
93 enter: function (node) { },
95 // Iterating the child **nodes** of unknown nodes.
102 Copyright (C) 2012-2013 [Yusuke Suzuki](http://github.com/Constellation)
103 (twitter: [@Constellation](http://twitter.com/Constellation)) and other contributors.
105 Redistribution and use in source and binary forms, with or without
106 modification, are permitted provided that the following conditions are met:
108 * Redistributions of source code must retain the above copyright
109 notice, this list of conditions and the following disclaimer.
111 * Redistributions in binary form must reproduce the above copyright
112 notice, this list of conditions and the following disclaimer in the
113 documentation and/or other materials provided with the distribution.
115 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
116 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
117 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
118 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
119 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
120 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
121 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
122 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
123 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
124 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.