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 ***********************************************************************/
75 AST_PrefixedTemplateString,
92 } from "./utils/index.js";
94 function def_transform(node, descend) {
95 node.DEFMETHOD("transform", function(tw, in_list) {
96 let transformed = undefined;
98 if (tw.before) transformed = tw.before(this, descend, in_list);
99 if (transformed === undefined) {
101 descend(transformed, tw);
103 const after_ret = tw.after(transformed, in_list);
104 if (after_ret !== undefined) transformed = after_ret;
112 function do_list(list, tw) {
113 return MAP(list, function(node) {
114 return node.transform(tw, true);
118 def_transform(AST_Node, noop);
120 def_transform(AST_LabeledStatement, function(self, tw) {
121 self.label = self.label.transform(tw);
122 self.body = self.body.transform(tw);
125 def_transform(AST_SimpleStatement, function(self, tw) {
126 self.body = self.body.transform(tw);
129 def_transform(AST_Block, function(self, tw) {
130 self.body = do_list(self.body, tw);
133 def_transform(AST_Do, function(self, tw) {
134 self.body = self.body.transform(tw);
135 self.condition = self.condition.transform(tw);
138 def_transform(AST_While, function(self, tw) {
139 self.condition = self.condition.transform(tw);
140 self.body = self.body.transform(tw);
143 def_transform(AST_For, function(self, tw) {
144 if (self.init) self.init = self.init.transform(tw);
145 if (self.condition) self.condition = self.condition.transform(tw);
146 if (self.step) self.step = self.step.transform(tw);
147 self.body = self.body.transform(tw);
150 def_transform(AST_ForIn, function(self, tw) {
151 self.init = self.init.transform(tw);
152 self.object = self.object.transform(tw);
153 self.body = self.body.transform(tw);
156 def_transform(AST_With, function(self, tw) {
157 self.expression = self.expression.transform(tw);
158 self.body = self.body.transform(tw);
161 def_transform(AST_Exit, function(self, tw) {
162 if (self.value) self.value = self.value.transform(tw);
165 def_transform(AST_LoopControl, function(self, tw) {
166 if (self.label) self.label = self.label.transform(tw);
169 def_transform(AST_If, function(self, tw) {
170 self.condition = self.condition.transform(tw);
171 self.body = self.body.transform(tw);
172 if (self.alternative) self.alternative = self.alternative.transform(tw);
175 def_transform(AST_Switch, function(self, tw) {
176 self.expression = self.expression.transform(tw);
177 self.body = do_list(self.body, tw);
180 def_transform(AST_Case, function(self, tw) {
181 self.expression = self.expression.transform(tw);
182 self.body = do_list(self.body, tw);
185 def_transform(AST_Try, function(self, tw) {
186 self.body = do_list(self.body, tw);
187 if (self.bcatch) self.bcatch = self.bcatch.transform(tw);
188 if (self.bfinally) self.bfinally = self.bfinally.transform(tw);
191 def_transform(AST_Catch, function(self, tw) {
192 if (self.argname) self.argname = self.argname.transform(tw);
193 self.body = do_list(self.body, tw);
196 def_transform(AST_Definitions, function(self, tw) {
197 self.definitions = do_list(self.definitions, tw);
200 def_transform(AST_VarDef, function(self, tw) {
201 self.name = self.name.transform(tw);
202 if (self.value) self.value = self.value.transform(tw);
205 def_transform(AST_Destructuring, function(self, tw) {
206 self.names = do_list(self.names, tw);
209 def_transform(AST_Lambda, function(self, tw) {
210 if (self.name) self.name = self.name.transform(tw);
211 self.argnames = do_list(self.argnames, tw);
212 if (self.body instanceof AST_Node) {
213 self.body = self.body.transform(tw);
215 self.body = do_list(self.body, tw);
219 def_transform(AST_Call, function(self, tw) {
220 self.expression = self.expression.transform(tw);
221 self.args = do_list(self.args, tw);
224 def_transform(AST_Sequence, function(self, tw) {
225 const result = do_list(self.expressions, tw);
226 self.expressions = result.length
228 : [new AST_Number({ value: 0 })];
231 def_transform(AST_PropAccess, function(self, tw) {
232 self.expression = self.expression.transform(tw);
235 def_transform(AST_Sub, function(self, tw) {
236 self.expression = self.expression.transform(tw);
237 self.property = self.property.transform(tw);
240 def_transform(AST_Chain, function(self, tw) {
241 self.expression = self.expression.transform(tw);
244 def_transform(AST_Yield, function(self, tw) {
245 if (self.expression) self.expression = self.expression.transform(tw);
248 def_transform(AST_Await, function(self, tw) {
249 self.expression = self.expression.transform(tw);
252 def_transform(AST_Unary, function(self, tw) {
253 self.expression = self.expression.transform(tw);
256 def_transform(AST_Binary, function(self, tw) {
257 self.left = self.left.transform(tw);
258 self.right = self.right.transform(tw);
261 def_transform(AST_Conditional, function(self, tw) {
262 self.condition = self.condition.transform(tw);
263 self.consequent = self.consequent.transform(tw);
264 self.alternative = self.alternative.transform(tw);
267 def_transform(AST_Array, function(self, tw) {
268 self.elements = do_list(self.elements, tw);
271 def_transform(AST_Object, function(self, tw) {
272 self.properties = do_list(self.properties, tw);
275 def_transform(AST_ObjectProperty, function(self, tw) {
276 if (self.key instanceof AST_Node) {
277 self.key = self.key.transform(tw);
279 if (self.value) self.value = self.value.transform(tw);
282 def_transform(AST_Class, function(self, tw) {
283 if (self.name) self.name = self.name.transform(tw);
284 if (self.extends) self.extends = self.extends.transform(tw);
285 self.properties = do_list(self.properties, tw);
288 def_transform(AST_Expansion, function(self, tw) {
289 self.expression = self.expression.transform(tw);
292 def_transform(AST_NameMapping, function(self, tw) {
293 self.foreign_name = self.foreign_name.transform(tw);
294 self.name = self.name.transform(tw);
297 def_transform(AST_Import, function(self, tw) {
298 if (self.imported_name) self.imported_name = self.imported_name.transform(tw);
299 if (self.imported_names) do_list(self.imported_names, tw);
300 self.module_name = self.module_name.transform(tw);
303 def_transform(AST_Export, function(self, tw) {
304 if (self.exported_definition) self.exported_definition = self.exported_definition.transform(tw);
305 if (self.exported_value) self.exported_value = self.exported_value.transform(tw);
306 if (self.exported_names) do_list(self.exported_names, tw);
307 if (self.module_name) self.module_name = self.module_name.transform(tw);
310 def_transform(AST_TemplateString, function(self, tw) {
311 self.segments = do_list(self.segments, tw);
314 def_transform(AST_PrefixedTemplateString, function(self, tw) {
315 self.prefix = self.prefix.transform(tw);
316 self.template_string = self.template_string.transform(tw);