1 /***********************************************************************
3 A JavaScript tokenizer / parser / beautifier / compressor.
4 https://github.com/mishoo/UglifyJS2
6 -------------------------------- (C) ---------------------------------
9 <mihai.bazon@gmail.com>
10 http://mihai.bazon.net/blog
12 Distributed under the BSD license:
14 Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
20 * Redistributions of source code must retain the above
21 copyright notice, this list of conditions and the following
24 * Redistributions in binary form must reproduce the above
25 copyright notice, this list of conditions and the following
26 disclaimer in the documentation and/or other materials
27 provided with the distribution.
29 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
30 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
33 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
34 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
39 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 ***********************************************************************/
46 // Tree transformer helpers.
48 function TreeTransformer(before, after) {
49 TreeWalker.call(this);
53 TreeTransformer.prototype = new TreeWalker;
57 function _(node, descend) {
58 node.DEFMETHOD("transform", function(tw, in_list){
61 if (tw.before) x = tw.before(this, descend, in_list);
62 if (x === undefined) {
67 tw.stack[tw.stack.length - 1] = x = this;
69 y = tw.after(x, in_list);
70 if (y !== undefined) x = y;
78 function do_list(list, tw) {
79 return MAP(list, function(node){
80 return node.transform(tw, true);
86 _(AST_LabeledStatement, function(self, tw){
87 self.label = self.label.transform(tw);
88 self.body = self.body.transform(tw);
91 _(AST_SimpleStatement, function(self, tw){
92 self.body = self.body.transform(tw);
95 _(AST_Block, function(self, tw){
96 self.body = do_list(self.body, tw);
99 _(AST_DWLoop, function(self, tw){
100 self.condition = self.condition.transform(tw);
101 self.body = self.body.transform(tw);
104 _(AST_For, function(self, tw){
105 if (self.init) self.init = self.init.transform(tw);
106 if (self.condition) self.condition = self.condition.transform(tw);
107 if (self.step) self.step = self.step.transform(tw);
108 self.body = self.body.transform(tw);
111 _(AST_ForIn, function(self, tw){
112 self.init = self.init.transform(tw);
113 self.object = self.object.transform(tw);
114 self.body = self.body.transform(tw);
117 _(AST_With, function(self, tw){
118 self.expression = self.expression.transform(tw);
119 self.body = self.body.transform(tw);
122 _(AST_Exit, function(self, tw){
123 if (self.value) self.value = self.value.transform(tw);
126 _(AST_LoopControl, function(self, tw){
127 if (self.label) self.label = self.label.transform(tw);
130 _(AST_If, function(self, tw){
131 self.condition = self.condition.transform(tw);
132 self.body = self.body.transform(tw);
133 if (self.alternative) self.alternative = self.alternative.transform(tw);
136 _(AST_Switch, function(self, tw){
137 self.expression = self.expression.transform(tw);
138 self.body = do_list(self.body, tw);
141 _(AST_Case, function(self, tw){
142 self.expression = self.expression.transform(tw);
143 self.body = do_list(self.body, tw);
146 _(AST_Try, function(self, tw){
147 self.body = do_list(self.body, tw);
148 if (self.bcatch) self.bcatch = self.bcatch.transform(tw);
149 if (self.bfinally) self.bfinally = self.bfinally.transform(tw);
152 _(AST_Catch, function(self, tw){
153 self.argname = self.argname.transform(tw);
154 self.body = do_list(self.body, tw);
157 _(AST_Definitions, function(self, tw){
158 self.definitions = do_list(self.definitions, tw);
161 _(AST_VarDef, function(self, tw){
162 self.name = self.name.transform(tw);
163 if (self.value) self.value = self.value.transform(tw);
166 _(AST_Lambda, function(self, tw){
167 if (self.name) self.name = self.name.transform(tw);
168 self.argnames = do_list(self.argnames, tw);
169 self.body = do_list(self.body, tw);
172 _(AST_Call, function(self, tw){
173 self.expression = self.expression.transform(tw);
174 self.args = do_list(self.args, tw);
177 _(AST_Seq, function(self, tw){
178 self.car = self.car.transform(tw);
179 self.cdr = self.cdr.transform(tw);
182 _(AST_Dot, function(self, tw){
183 self.expression = self.expression.transform(tw);
186 _(AST_Sub, function(self, tw){
187 self.expression = self.expression.transform(tw);
188 self.property = self.property.transform(tw);
191 _(AST_Unary, function(self, tw){
192 self.expression = self.expression.transform(tw);
195 _(AST_Binary, function(self, tw){
196 self.left = self.left.transform(tw);
197 self.right = self.right.transform(tw);
200 _(AST_Conditional, function(self, tw){
201 self.condition = self.condition.transform(tw);
202 self.consequent = self.consequent.transform(tw);
203 self.alternative = self.alternative.transform(tw);
206 _(AST_Array, function(self, tw){
207 self.elements = do_list(self.elements, tw);
210 _(AST_Object, function(self, tw){
211 self.properties = do_list(self.properties, tw);
214 _(AST_ObjectProperty, function(self, tw){
215 self.value = self.value.transform(tw);