UNPKG

6.89 kBJavaScriptView Raw
1/***********************************************************************
2
3 A JavaScript tokenizer / parser / beautifier / compressor.
4 https://github.com/mishoo/UglifyJS2
5
6 -------------------------------- (C) ---------------------------------
7
8 Author: Mihai Bazon
9 <mihai.bazon@gmail.com>
10 http://mihai.bazon.net/blog
11
12 Distributed under the BSD license:
13
14 Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
19
20 * Redistributions of source code must retain the above
21 copyright notice, this list of conditions and the following
22 disclaimer.
23
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.
28
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
40 SUCH DAMAGE.
41
42 ***********************************************************************/
43
44"use strict";
45
46function TreeTransformer(before, after) {
47 TreeWalker.call(this);
48 this.before = before;
49 this.after = after;
50}
51TreeTransformer.prototype = new TreeWalker;
52
53(function(DEF) {
54 function do_list(list, tw) {
55 return MAP(list, function(node) {
56 return node.transform(tw, true);
57 });
58 }
59
60 DEF(AST_Node, noop);
61 DEF(AST_LabeledStatement, function(self, tw) {
62 self.label = self.label.transform(tw);
63 self.body = self.body.transform(tw);
64 });
65 DEF(AST_SimpleStatement, function(self, tw) {
66 self.body = self.body.transform(tw);
67 });
68 DEF(AST_Block, function(self, tw) {
69 self.body = do_list(self.body, tw);
70 });
71 DEF(AST_Do, function(self, tw) {
72 self.body = self.body.transform(tw);
73 self.condition = self.condition.transform(tw);
74 });
75 DEF(AST_While, function(self, tw) {
76 self.condition = self.condition.transform(tw);
77 self.body = self.body.transform(tw);
78 });
79 DEF(AST_For, function(self, tw) {
80 if (self.init) self.init = self.init.transform(tw);
81 if (self.condition) self.condition = self.condition.transform(tw);
82 if (self.step) self.step = self.step.transform(tw);
83 self.body = self.body.transform(tw);
84 });
85 DEF(AST_ForIn, function(self, tw) {
86 self.init = self.init.transform(tw);
87 self.object = self.object.transform(tw);
88 self.body = self.body.transform(tw);
89 });
90 DEF(AST_With, function(self, tw) {
91 self.expression = self.expression.transform(tw);
92 self.body = self.body.transform(tw);
93 });
94 DEF(AST_Exit, function(self, tw) {
95 if (self.value) self.value = self.value.transform(tw);
96 });
97 DEF(AST_LoopControl, function(self, tw) {
98 if (self.label) self.label = self.label.transform(tw);
99 });
100 DEF(AST_If, function(self, tw) {
101 self.condition = self.condition.transform(tw);
102 self.body = self.body.transform(tw);
103 if (self.alternative) self.alternative = self.alternative.transform(tw);
104 });
105 DEF(AST_Switch, function(self, tw) {
106 self.expression = self.expression.transform(tw);
107 self.body = do_list(self.body, tw);
108 });
109 DEF(AST_Case, function(self, tw) {
110 self.expression = self.expression.transform(tw);
111 self.body = do_list(self.body, tw);
112 });
113 DEF(AST_Try, function(self, tw) {
114 self.body = do_list(self.body, tw);
115 if (self.bcatch) self.bcatch = self.bcatch.transform(tw);
116 if (self.bfinally) self.bfinally = self.bfinally.transform(tw);
117 });
118 DEF(AST_Catch, function(self, tw) {
119 self.argname = self.argname.transform(tw);
120 self.body = do_list(self.body, tw);
121 });
122 DEF(AST_Definitions, function(self, tw) {
123 self.definitions = do_list(self.definitions, tw);
124 });
125 DEF(AST_VarDef, function(self, tw) {
126 self.name = self.name.transform(tw);
127 if (self.value) self.value = self.value.transform(tw);
128 });
129 DEF(AST_Lambda, function(self, tw) {
130 if (self.name) self.name = self.name.transform(tw);
131 self.argnames = do_list(self.argnames, tw);
132 self.body = do_list(self.body, tw);
133 });
134 DEF(AST_Call, function(self, tw) {
135 self.expression = self.expression.transform(tw);
136 self.args = do_list(self.args, tw);
137 });
138 DEF(AST_Sequence, function(self, tw) {
139 self.expressions = do_list(self.expressions, tw);
140 });
141 DEF(AST_Dot, function(self, tw) {
142 self.expression = self.expression.transform(tw);
143 });
144 DEF(AST_Sub, function(self, tw) {
145 self.expression = self.expression.transform(tw);
146 self.property = self.property.transform(tw);
147 });
148 DEF(AST_Unary, function(self, tw) {
149 self.expression = self.expression.transform(tw);
150 });
151 DEF(AST_Binary, function(self, tw) {
152 self.left = self.left.transform(tw);
153 self.right = self.right.transform(tw);
154 });
155 DEF(AST_Conditional, function(self, tw) {
156 self.condition = self.condition.transform(tw);
157 self.consequent = self.consequent.transform(tw);
158 self.alternative = self.alternative.transform(tw);
159 });
160 DEF(AST_Array, function(self, tw) {
161 self.elements = do_list(self.elements, tw);
162 });
163 DEF(AST_Object, function(self, tw) {
164 self.properties = do_list(self.properties, tw);
165 });
166 DEF(AST_ObjectProperty, function(self, tw) {
167 self.value = self.value.transform(tw);
168 });
169})(function(node, descend) {
170 node.DEFMETHOD("transform", function(tw, in_list) {
171 var x, y;
172 tw.push(this);
173 if (tw.before) x = tw.before(this, descend, in_list);
174 if (typeof x === "undefined") {
175 x = this;
176 descend(x, tw);
177 if (tw.after) {
178 y = tw.after(x, in_list);
179 if (typeof y !== "undefined") x = y;
180 }
181 }
182 tw.pop();
183 return x;
184 });
185});