terser 5.7.0 → 5.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,350 @@
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
+ import {
45
+ AST_Accessor,
46
+ AST_Array,
47
+ AST_Arrow,
48
+ AST_Assign,
49
+ AST_Binary,
50
+ AST_Call,
51
+ AST_Chain,
52
+ AST_Class,
53
+ AST_ClassProperty,
54
+ AST_ConciseMethod,
55
+ AST_Conditional,
56
+ AST_Constant,
57
+ AST_Dot,
58
+ AST_Expansion,
59
+ AST_Function,
60
+ AST_Node,
61
+ AST_Number,
62
+ AST_Object,
63
+ AST_ObjectGetter,
64
+ AST_ObjectKeyVal,
65
+ AST_ObjectProperty,
66
+ AST_ObjectSetter,
67
+ AST_PropAccess,
68
+ AST_Scope,
69
+ AST_Sequence,
70
+ AST_Sub,
71
+ AST_SymbolRef,
72
+ AST_TemplateSegment,
73
+ AST_TemplateString,
74
+ AST_This,
75
+ AST_Unary,
76
+ } from "../ast.js";
77
+ import { make_node, return_null, return_this } from "../utils/index.js";
78
+ import { first_in_statement } from "../utils/first_in_statement.js";
79
+
80
+ import { pure_prop_access_globals } from "./native-objects.js";
81
+ import { lazy_op, unary_side_effects, is_nullish_shortcircuited } from "./inference.js";
82
+ import { WRITE_ONLY, set_flag, clear_flag } from "./compressor-flags.js";
83
+ import { make_sequence, is_func_expr, is_iife_call } from "./common.js";
84
+
85
+ // AST_Node#drop_side_effect_free() gets called when we don't care about the value,
86
+ // only about side effects. We'll be defining this method for each node type in this module
87
+ //
88
+ // Examples:
89
+ // foo++ -> foo++
90
+ // 1 + func() -> func()
91
+ // 10 -> (nothing)
92
+ // knownPureFunc(foo++) -> foo++
93
+
94
+ function def_drop_side_effect_free(node, func) {
95
+ node.DEFMETHOD("drop_side_effect_free", func);
96
+ }
97
+
98
+ // Drop side-effect-free elements from an array of expressions.
99
+ // Returns an array of expressions with side-effects or null
100
+ // if all elements were dropped. Note: original array may be
101
+ // returned if nothing changed.
102
+ function trim(nodes, compressor, first_in_statement) {
103
+ var len = nodes.length;
104
+ if (!len) return null;
105
+
106
+ var ret = [], changed = false;
107
+ for (var i = 0; i < len; i++) {
108
+ var node = nodes[i].drop_side_effect_free(compressor, first_in_statement);
109
+ changed |= node !== nodes[i];
110
+ if (node) {
111
+ ret.push(node);
112
+ first_in_statement = false;
113
+ }
114
+ }
115
+ return changed ? ret.length ? ret : null : nodes;
116
+ }
117
+
118
+ def_drop_side_effect_free(AST_Node, return_this);
119
+ def_drop_side_effect_free(AST_Constant, return_null);
120
+ def_drop_side_effect_free(AST_This, return_null);
121
+
122
+ def_drop_side_effect_free(AST_Call, function (compressor, first_in_statement) {
123
+ if (is_nullish_shortcircuited(this, compressor)) {
124
+ return this.expression.drop_side_effect_free(compressor, first_in_statement);
125
+ }
126
+
127
+ if (!this.is_callee_pure(compressor)) {
128
+ if (this.expression.is_call_pure(compressor)) {
129
+ var exprs = this.args.slice();
130
+ exprs.unshift(this.expression.expression);
131
+ exprs = trim(exprs, compressor, first_in_statement);
132
+ return exprs && make_sequence(this, exprs);
133
+ }
134
+ if (is_func_expr(this.expression)
135
+ && (!this.expression.name || !this.expression.name.definition().references.length)) {
136
+ var node = this.clone();
137
+ node.expression.process_expression(false, compressor);
138
+ return node;
139
+ }
140
+ return this;
141
+ }
142
+
143
+ var args = trim(this.args, compressor, first_in_statement);
144
+ return args && make_sequence(this, args);
145
+ });
146
+
147
+ def_drop_side_effect_free(AST_Accessor, return_null);
148
+
149
+ def_drop_side_effect_free(AST_Function, return_null);
150
+
151
+ def_drop_side_effect_free(AST_Arrow, return_null);
152
+
153
+ def_drop_side_effect_free(AST_Class, function (compressor) {
154
+ const with_effects = [];
155
+ const trimmed_extends = this.extends && this.extends.drop_side_effect_free(compressor);
156
+ if (trimmed_extends)
157
+ with_effects.push(trimmed_extends);
158
+ for (const prop of this.properties) {
159
+ const trimmed_prop = prop.drop_side_effect_free(compressor);
160
+ if (trimmed_prop)
161
+ with_effects.push(trimmed_prop);
162
+ }
163
+ if (!with_effects.length)
164
+ return null;
165
+ return make_sequence(this, with_effects);
166
+ });
167
+
168
+ def_drop_side_effect_free(AST_Binary, function (compressor, first_in_statement) {
169
+ var right = this.right.drop_side_effect_free(compressor);
170
+ if (!right)
171
+ return this.left.drop_side_effect_free(compressor, first_in_statement);
172
+ if (lazy_op.has(this.operator)) {
173
+ if (right === this.right)
174
+ return this;
175
+ var node = this.clone();
176
+ node.right = right;
177
+ return node;
178
+ } else {
179
+ var left = this.left.drop_side_effect_free(compressor, first_in_statement);
180
+ if (!left)
181
+ return this.right.drop_side_effect_free(compressor, first_in_statement);
182
+ return make_sequence(this, [left, right]);
183
+ }
184
+ });
185
+
186
+ def_drop_side_effect_free(AST_Assign, function (compressor) {
187
+ if (this.logical)
188
+ return this;
189
+
190
+ var left = this.left;
191
+ if (left.has_side_effects(compressor)
192
+ || compressor.has_directive("use strict")
193
+ && left instanceof AST_PropAccess
194
+ && left.expression.is_constant()) {
195
+ return this;
196
+ }
197
+ set_flag(this, WRITE_ONLY);
198
+ while (left instanceof AST_PropAccess) {
199
+ left = left.expression;
200
+ }
201
+ if (left.is_constant_expression(compressor.find_parent(AST_Scope))) {
202
+ return this.right.drop_side_effect_free(compressor);
203
+ }
204
+ return this;
205
+ });
206
+
207
+ def_drop_side_effect_free(AST_Conditional, function (compressor) {
208
+ var consequent = this.consequent.drop_side_effect_free(compressor);
209
+ var alternative = this.alternative.drop_side_effect_free(compressor);
210
+ if (consequent === this.consequent && alternative === this.alternative)
211
+ return this;
212
+ if (!consequent)
213
+ return alternative ? make_node(AST_Binary, this, {
214
+ operator: "||",
215
+ left: this.condition,
216
+ right: alternative
217
+ }) : this.condition.drop_side_effect_free(compressor);
218
+ if (!alternative)
219
+ return make_node(AST_Binary, this, {
220
+ operator: "&&",
221
+ left: this.condition,
222
+ right: consequent
223
+ });
224
+ var node = this.clone();
225
+ node.consequent = consequent;
226
+ node.alternative = alternative;
227
+ return node;
228
+ });
229
+
230
+ def_drop_side_effect_free(AST_Unary, function (compressor, first_in_statement) {
231
+ if (unary_side_effects.has(this.operator)) {
232
+ if (!this.expression.has_side_effects(compressor)) {
233
+ set_flag(this, WRITE_ONLY);
234
+ } else {
235
+ clear_flag(this, WRITE_ONLY);
236
+ }
237
+ return this;
238
+ }
239
+ if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef)
240
+ return null;
241
+ var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
242
+ if (first_in_statement && expression && is_iife_call(expression)) {
243
+ if (expression === this.expression && this.operator == "!")
244
+ return this;
245
+ return expression.negate(compressor, first_in_statement);
246
+ }
247
+ return expression;
248
+ });
249
+
250
+ def_drop_side_effect_free(AST_SymbolRef, function (compressor) {
251
+ const safe_access = this.is_declared(compressor)
252
+ || pure_prop_access_globals.has(this.name);
253
+ return safe_access ? null : this;
254
+ });
255
+
256
+ def_drop_side_effect_free(AST_Object, function (compressor, first_in_statement) {
257
+ var values = trim(this.properties, compressor, first_in_statement);
258
+ return values && make_sequence(this, values);
259
+ });
260
+
261
+ def_drop_side_effect_free(AST_ObjectProperty, function (compressor, first_in_statement) {
262
+ const computed_key = this instanceof AST_ObjectKeyVal && this.key instanceof AST_Node;
263
+ const key = computed_key && this.key.drop_side_effect_free(compressor, first_in_statement);
264
+ const value = this.value && this.value.drop_side_effect_free(compressor, first_in_statement);
265
+ if (key && value) {
266
+ return make_sequence(this, [key, value]);
267
+ }
268
+ return key || value;
269
+ });
270
+
271
+ def_drop_side_effect_free(AST_ClassProperty, function (compressor) {
272
+ const key = this.computed_key() && this.key.drop_side_effect_free(compressor);
273
+
274
+ const value = this.static && this.value
275
+ && this.value.drop_side_effect_free(compressor);
276
+
277
+ if (key && value)
278
+ return make_sequence(this, [key, value]);
279
+ return key || value || null;
280
+ });
281
+
282
+ def_drop_side_effect_free(AST_ConciseMethod, function () {
283
+ return this.computed_key() ? this.key : null;
284
+ });
285
+
286
+ def_drop_side_effect_free(AST_ObjectGetter, function () {
287
+ return this.computed_key() ? this.key : null;
288
+ });
289
+
290
+ def_drop_side_effect_free(AST_ObjectSetter, function () {
291
+ return this.computed_key() ? this.key : null;
292
+ });
293
+
294
+ def_drop_side_effect_free(AST_Array, function (compressor, first_in_statement) {
295
+ var values = trim(this.elements, compressor, first_in_statement);
296
+ return values && make_sequence(this, values);
297
+ });
298
+
299
+ def_drop_side_effect_free(AST_Dot, function (compressor, first_in_statement) {
300
+ if (is_nullish_shortcircuited(this, compressor)) {
301
+ return this.expression.drop_side_effect_free(compressor, first_in_statement);
302
+ }
303
+ if (this.expression.may_throw_on_access(compressor)) return this;
304
+
305
+ return this.expression.drop_side_effect_free(compressor, first_in_statement);
306
+ });
307
+
308
+ def_drop_side_effect_free(AST_Sub, function (compressor, first_in_statement) {
309
+ if (is_nullish_shortcircuited(this, compressor)) {
310
+ return this.expression.drop_side_effect_free(compressor, first_in_statement);
311
+ }
312
+ if (this.expression.may_throw_on_access(compressor)) return this;
313
+
314
+ var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
315
+ if (!expression)
316
+ return this.property.drop_side_effect_free(compressor, first_in_statement);
317
+ var property = this.property.drop_side_effect_free(compressor);
318
+ if (!property)
319
+ return expression;
320
+ return make_sequence(this, [expression, property]);
321
+ });
322
+
323
+ def_drop_side_effect_free(AST_Chain, function (compressor, first_in_statement) {
324
+ return this.expression.drop_side_effect_free(compressor, first_in_statement);
325
+ });
326
+
327
+ def_drop_side_effect_free(AST_Sequence, function (compressor) {
328
+ var last = this.tail_node();
329
+ var expr = last.drop_side_effect_free(compressor);
330
+ if (expr === last)
331
+ return this;
332
+ var expressions = this.expressions.slice(0, -1);
333
+ if (expr)
334
+ expressions.push(expr);
335
+ if (!expressions.length) {
336
+ return make_node(AST_Number, this, { value: 0 });
337
+ }
338
+ return make_sequence(this, expressions);
339
+ });
340
+
341
+ def_drop_side_effect_free(AST_Expansion, function (compressor, first_in_statement) {
342
+ return this.expression.drop_side_effect_free(compressor, first_in_statement);
343
+ });
344
+
345
+ def_drop_side_effect_free(AST_TemplateSegment, return_null);
346
+
347
+ def_drop_side_effect_free(AST_TemplateString, function (compressor) {
348
+ var values = trim(this.segments, compressor, first_in_statement);
349
+ return values && make_sequence(this, values);
350
+ });