[6a3a178] | 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 |
|
---|
| 46 | import {
|
---|
| 47 | AST_Array,
|
---|
| 48 | AST_Await,
|
---|
| 49 | AST_Binary,
|
---|
| 50 | AST_Block,
|
---|
| 51 | AST_Call,
|
---|
| 52 | AST_Case,
|
---|
| 53 | AST_Catch,
|
---|
| 54 | AST_Chain,
|
---|
| 55 | AST_Class,
|
---|
| 56 | AST_Conditional,
|
---|
| 57 | AST_Definitions,
|
---|
| 58 | AST_Destructuring,
|
---|
| 59 | AST_Do,
|
---|
| 60 | AST_Dot,
|
---|
| 61 | AST_Exit,
|
---|
| 62 | AST_Expansion,
|
---|
| 63 | AST_Export,
|
---|
| 64 | AST_For,
|
---|
| 65 | AST_ForIn,
|
---|
| 66 | AST_If,
|
---|
| 67 | AST_Import,
|
---|
| 68 | AST_LabeledStatement,
|
---|
| 69 | AST_Lambda,
|
---|
| 70 | AST_LoopControl,
|
---|
| 71 | AST_NameMapping,
|
---|
| 72 | AST_Node,
|
---|
| 73 | AST_Number,
|
---|
| 74 | AST_Object,
|
---|
| 75 | AST_ObjectProperty,
|
---|
| 76 | AST_PrefixedTemplateString,
|
---|
| 77 | AST_Sequence,
|
---|
| 78 | AST_SimpleStatement,
|
---|
| 79 | AST_Sub,
|
---|
| 80 | AST_Switch,
|
---|
| 81 | AST_TemplateString,
|
---|
| 82 | AST_Try,
|
---|
| 83 | AST_Unary,
|
---|
| 84 | AST_VarDef,
|
---|
| 85 | AST_While,
|
---|
| 86 | AST_With,
|
---|
| 87 | AST_Yield,
|
---|
| 88 | } from "./ast.js";
|
---|
| 89 | import {
|
---|
| 90 | MAP,
|
---|
| 91 | noop,
|
---|
| 92 | } from "./utils/index.js";
|
---|
| 93 |
|
---|
| 94 | function def_transform(node, descend) {
|
---|
| 95 | node.DEFMETHOD("transform", function(tw, in_list) {
|
---|
| 96 | let transformed = undefined;
|
---|
| 97 | tw.push(this);
|
---|
| 98 | if (tw.before) transformed = tw.before(this, descend, in_list);
|
---|
| 99 | if (transformed === undefined) {
|
---|
| 100 | transformed = this;
|
---|
| 101 | descend(transformed, tw);
|
---|
| 102 | if (tw.after) {
|
---|
| 103 | const after_ret = tw.after(transformed, in_list);
|
---|
| 104 | if (after_ret !== undefined) transformed = after_ret;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | tw.pop();
|
---|
| 108 | return transformed;
|
---|
| 109 | });
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | function do_list(list, tw) {
|
---|
| 113 | return MAP(list, function(node) {
|
---|
| 114 | return node.transform(tw, true);
|
---|
| 115 | });
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | def_transform(AST_Node, noop);
|
---|
| 119 |
|
---|
| 120 | def_transform(AST_LabeledStatement, function(self, tw) {
|
---|
| 121 | self.label = self.label.transform(tw);
|
---|
| 122 | self.body = self.body.transform(tw);
|
---|
| 123 | });
|
---|
| 124 |
|
---|
| 125 | def_transform(AST_SimpleStatement, function(self, tw) {
|
---|
| 126 | self.body = self.body.transform(tw);
|
---|
| 127 | });
|
---|
| 128 |
|
---|
| 129 | def_transform(AST_Block, function(self, tw) {
|
---|
| 130 | self.body = do_list(self.body, tw);
|
---|
| 131 | });
|
---|
| 132 |
|
---|
| 133 | def_transform(AST_Do, function(self, tw) {
|
---|
| 134 | self.body = self.body.transform(tw);
|
---|
| 135 | self.condition = self.condition.transform(tw);
|
---|
| 136 | });
|
---|
| 137 |
|
---|
| 138 | def_transform(AST_While, function(self, tw) {
|
---|
| 139 | self.condition = self.condition.transform(tw);
|
---|
| 140 | self.body = self.body.transform(tw);
|
---|
| 141 | });
|
---|
| 142 |
|
---|
| 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);
|
---|
| 148 | });
|
---|
| 149 |
|
---|
| 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);
|
---|
| 154 | });
|
---|
| 155 |
|
---|
| 156 | def_transform(AST_With, function(self, tw) {
|
---|
| 157 | self.expression = self.expression.transform(tw);
|
---|
| 158 | self.body = self.body.transform(tw);
|
---|
| 159 | });
|
---|
| 160 |
|
---|
| 161 | def_transform(AST_Exit, function(self, tw) {
|
---|
| 162 | if (self.value) self.value = self.value.transform(tw);
|
---|
| 163 | });
|
---|
| 164 |
|
---|
| 165 | def_transform(AST_LoopControl, function(self, tw) {
|
---|
| 166 | if (self.label) self.label = self.label.transform(tw);
|
---|
| 167 | });
|
---|
| 168 |
|
---|
| 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);
|
---|
| 173 | });
|
---|
| 174 |
|
---|
| 175 | def_transform(AST_Switch, function(self, tw) {
|
---|
| 176 | self.expression = self.expression.transform(tw);
|
---|
| 177 | self.body = do_list(self.body, tw);
|
---|
| 178 | });
|
---|
| 179 |
|
---|
| 180 | def_transform(AST_Case, function(self, tw) {
|
---|
| 181 | self.expression = self.expression.transform(tw);
|
---|
| 182 | self.body = do_list(self.body, tw);
|
---|
| 183 | });
|
---|
| 184 |
|
---|
| 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);
|
---|
| 189 | });
|
---|
| 190 |
|
---|
| 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);
|
---|
| 194 | });
|
---|
| 195 |
|
---|
| 196 | def_transform(AST_Definitions, function(self, tw) {
|
---|
| 197 | self.definitions = do_list(self.definitions, tw);
|
---|
| 198 | });
|
---|
| 199 |
|
---|
| 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);
|
---|
| 203 | });
|
---|
| 204 |
|
---|
| 205 | def_transform(AST_Destructuring, function(self, tw) {
|
---|
| 206 | self.names = do_list(self.names, tw);
|
---|
| 207 | });
|
---|
| 208 |
|
---|
| 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);
|
---|
| 214 | } else {
|
---|
| 215 | self.body = do_list(self.body, tw);
|
---|
| 216 | }
|
---|
| 217 | });
|
---|
| 218 |
|
---|
| 219 | def_transform(AST_Call, function(self, tw) {
|
---|
| 220 | self.expression = self.expression.transform(tw);
|
---|
| 221 | self.args = do_list(self.args, tw);
|
---|
| 222 | });
|
---|
| 223 |
|
---|
| 224 | def_transform(AST_Sequence, function(self, tw) {
|
---|
| 225 | const result = do_list(self.expressions, tw);
|
---|
| 226 | self.expressions = result.length
|
---|
| 227 | ? result
|
---|
| 228 | : [new AST_Number({ value: 0 })];
|
---|
| 229 | });
|
---|
| 230 |
|
---|
| 231 | def_transform(AST_Dot, function(self, tw) {
|
---|
| 232 | self.expression = self.expression.transform(tw);
|
---|
| 233 | });
|
---|
| 234 |
|
---|
| 235 | def_transform(AST_Sub, function(self, tw) {
|
---|
| 236 | self.expression = self.expression.transform(tw);
|
---|
| 237 | self.property = self.property.transform(tw);
|
---|
| 238 | });
|
---|
| 239 |
|
---|
| 240 | def_transform(AST_Chain, function(self, tw) {
|
---|
| 241 | self.expression = self.expression.transform(tw);
|
---|
| 242 | });
|
---|
| 243 |
|
---|
| 244 | def_transform(AST_Yield, function(self, tw) {
|
---|
| 245 | if (self.expression) self.expression = self.expression.transform(tw);
|
---|
| 246 | });
|
---|
| 247 |
|
---|
| 248 | def_transform(AST_Await, function(self, tw) {
|
---|
| 249 | self.expression = self.expression.transform(tw);
|
---|
| 250 | });
|
---|
| 251 |
|
---|
| 252 | def_transform(AST_Unary, function(self, tw) {
|
---|
| 253 | self.expression = self.expression.transform(tw);
|
---|
| 254 | });
|
---|
| 255 |
|
---|
| 256 | def_transform(AST_Binary, function(self, tw) {
|
---|
| 257 | self.left = self.left.transform(tw);
|
---|
| 258 | self.right = self.right.transform(tw);
|
---|
| 259 | });
|
---|
| 260 |
|
---|
| 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);
|
---|
| 265 | });
|
---|
| 266 |
|
---|
| 267 | def_transform(AST_Array, function(self, tw) {
|
---|
| 268 | self.elements = do_list(self.elements, tw);
|
---|
| 269 | });
|
---|
| 270 |
|
---|
| 271 | def_transform(AST_Object, function(self, tw) {
|
---|
| 272 | self.properties = do_list(self.properties, tw);
|
---|
| 273 | });
|
---|
| 274 |
|
---|
| 275 | def_transform(AST_ObjectProperty, function(self, tw) {
|
---|
| 276 | if (self.key instanceof AST_Node) {
|
---|
| 277 | self.key = self.key.transform(tw);
|
---|
| 278 | }
|
---|
| 279 | if (self.value) self.value = self.value.transform(tw);
|
---|
| 280 | });
|
---|
| 281 |
|
---|
| 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);
|
---|
| 286 | });
|
---|
| 287 |
|
---|
| 288 | def_transform(AST_Expansion, function(self, tw) {
|
---|
| 289 | self.expression = self.expression.transform(tw);
|
---|
| 290 | });
|
---|
| 291 |
|
---|
| 292 | def_transform(AST_NameMapping, function(self, tw) {
|
---|
| 293 | self.foreign_name = self.foreign_name.transform(tw);
|
---|
| 294 | self.name = self.name.transform(tw);
|
---|
| 295 | });
|
---|
| 296 |
|
---|
| 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);
|
---|
| 301 | });
|
---|
| 302 |
|
---|
| 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);
|
---|
| 308 | });
|
---|
| 309 |
|
---|
| 310 | def_transform(AST_TemplateString, function(self, tw) {
|
---|
| 311 | self.segments = do_list(self.segments, tw);
|
---|
| 312 | });
|
---|
| 313 |
|
---|
| 314 | def_transform(AST_PrefixedTemplateString, function(self, tw) {
|
---|
| 315 | self.prefix = self.prefix.transform(tw);
|
---|
| 316 | self.template_string = self.template_string.transform(tw);
|
---|
| 317 | });
|
---|
| 318 |
|
---|