drop-side-effect-free.js
12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/***********************************************************************
A JavaScript tokenizer / parser / beautifier / compressor.
https://github.com/mishoo/UglifyJS2
-------------------------------- (C) ---------------------------------
Author: Mihai Bazon
<mihai.bazon@gmail.com>
http://mihai.bazon.net/blog
Distributed under the BSD license:
Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
***********************************************************************/
import {
AST_Accessor,
AST_Array,
AST_Arrow,
AST_Assign,
AST_Binary,
AST_Call,
AST_Chain,
AST_Class,
AST_ClassStaticBlock,
AST_ClassProperty,
AST_ConciseMethod,
AST_Conditional,
AST_Constant,
AST_DefClass,
AST_Dot,
AST_Expansion,
AST_Function,
AST_Node,
AST_Number,
AST_Object,
AST_ObjectGetter,
AST_ObjectKeyVal,
AST_ObjectProperty,
AST_ObjectSetter,
AST_PropAccess,
AST_Scope,
AST_Sequence,
AST_SimpleStatement,
AST_Sub,
AST_SymbolRef,
AST_TemplateSegment,
AST_TemplateString,
AST_This,
AST_Unary,
} from "../ast.js";
import { make_node, return_null, return_this } from "../utils/index.js";
import { first_in_statement } from "../utils/first_in_statement.js";
import { pure_prop_access_globals } from "./native-objects.js";
import { lazy_op, unary_side_effects, is_nullish_shortcircuited } from "./inference.js";
import { WRITE_ONLY, set_flag, clear_flag } from "./compressor-flags.js";
import { make_sequence, is_func_expr, is_iife_call } from "./common.js";
// AST_Node#drop_side_effect_free() gets called when we don't care about the value,
// only about side effects. We'll be defining this method for each node type in this module
//
// Examples:
// foo++ -> foo++
// 1 + func() -> func()
// 10 -> (nothing)
// knownPureFunc(foo++) -> foo++
function def_drop_side_effect_free(node, func) {
node.DEFMETHOD("drop_side_effect_free", func);
}
// Drop side-effect-free elements from an array of expressions.
// Returns an array of expressions with side-effects or null
// if all elements were dropped. Note: original array may be
// returned if nothing changed.
function trim(nodes, compressor, first_in_statement) {
var len = nodes.length;
if (!len) return null;
var ret = [], changed = false;
for (var i = 0; i < len; i++) {
var node = nodes[i].drop_side_effect_free(compressor, first_in_statement);
changed |= node !== nodes[i];
if (node) {
ret.push(node);
first_in_statement = false;
}
}
return changed ? ret.length ? ret : null : nodes;
}
def_drop_side_effect_free(AST_Node, return_this);
def_drop_side_effect_free(AST_Constant, return_null);
def_drop_side_effect_free(AST_This, return_null);
def_drop_side_effect_free(AST_Call, function (compressor, first_in_statement) {
if (is_nullish_shortcircuited(this, compressor)) {
return this.expression.drop_side_effect_free(compressor, first_in_statement);
}
if (!this.is_callee_pure(compressor)) {
if (this.expression.is_call_pure(compressor)) {
var exprs = this.args.slice();
exprs.unshift(this.expression.expression);
exprs = trim(exprs, compressor, first_in_statement);
return exprs && make_sequence(this, exprs);
}
if (is_func_expr(this.expression)
&& (!this.expression.name || !this.expression.name.definition().references.length)) {
var node = this.clone();
node.expression.process_expression(false, compressor);
return node;
}
return this;
}
var args = trim(this.args, compressor, first_in_statement);
return args && make_sequence(this, args);
});
def_drop_side_effect_free(AST_Accessor, return_null);
def_drop_side_effect_free(AST_Function, return_null);
def_drop_side_effect_free(AST_Arrow, return_null);
def_drop_side_effect_free(AST_Class, function (compressor) {
const with_effects = [];
const trimmed_extends = this.extends && this.extends.drop_side_effect_free(compressor);
if (trimmed_extends)
with_effects.push(trimmed_extends);
for (const prop of this.properties) {
if (prop instanceof AST_ClassStaticBlock) {
if (prop.has_side_effects(compressor)) {
return this; // Be cautious about these
}
} else {
const trimmed_prop = prop.drop_side_effect_free(compressor);
if (trimmed_prop) {
if (trimmed_prop.contains_this()) return this;
with_effects.push(trimmed_prop);
}
}
}
if (!with_effects.length)
return null;
const exprs = make_sequence(this, with_effects);
if (this instanceof AST_DefClass) {
// We want a statement
return make_node(AST_SimpleStatement, this, { body: exprs });
} else {
return exprs;
}
});
def_drop_side_effect_free(AST_ClassProperty, function (compressor) {
const key = this.computed_key() && this.key.drop_side_effect_free(compressor);
const value = this.static && this.value
&& this.value.drop_side_effect_free(compressor);
if (key && value)
return make_sequence(this, [key, value]);
return key || value || null;
});
def_drop_side_effect_free(AST_Binary, function (compressor, first_in_statement) {
var right = this.right.drop_side_effect_free(compressor);
if (!right)
return this.left.drop_side_effect_free(compressor, first_in_statement);
if (lazy_op.has(this.operator)) {
if (right === this.right)
return this;
var node = this.clone();
node.right = right;
return node;
} else {
var left = this.left.drop_side_effect_free(compressor, first_in_statement);
if (!left)
return this.right.drop_side_effect_free(compressor, first_in_statement);
return make_sequence(this, [left, right]);
}
});
def_drop_side_effect_free(AST_Assign, function (compressor) {
if (this.logical)
return this;
var left = this.left;
if (left.has_side_effects(compressor)
|| compressor.has_directive("use strict")
&& left instanceof AST_PropAccess
&& left.expression.is_constant()) {
return this;
}
set_flag(this, WRITE_ONLY);
while (left instanceof AST_PropAccess) {
left = left.expression;
}
if (left.is_constant_expression(compressor.find_parent(AST_Scope))) {
return this.right.drop_side_effect_free(compressor);
}
return this;
});
def_drop_side_effect_free(AST_Conditional, function (compressor) {
var consequent = this.consequent.drop_side_effect_free(compressor);
var alternative = this.alternative.drop_side_effect_free(compressor);
if (consequent === this.consequent && alternative === this.alternative)
return this;
if (!consequent)
return alternative ? make_node(AST_Binary, this, {
operator: "||",
left: this.condition,
right: alternative
}) : this.condition.drop_side_effect_free(compressor);
if (!alternative)
return make_node(AST_Binary, this, {
operator: "&&",
left: this.condition,
right: consequent
});
var node = this.clone();
node.consequent = consequent;
node.alternative = alternative;
return node;
});
def_drop_side_effect_free(AST_Unary, function (compressor, first_in_statement) {
if (unary_side_effects.has(this.operator)) {
if (!this.expression.has_side_effects(compressor)) {
set_flag(this, WRITE_ONLY);
} else {
clear_flag(this, WRITE_ONLY);
}
return this;
}
if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef)
return null;
var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
if (first_in_statement && expression && is_iife_call(expression)) {
if (expression === this.expression && this.operator == "!")
return this;
return expression.negate(compressor, first_in_statement);
}
return expression;
});
def_drop_side_effect_free(AST_SymbolRef, function (compressor) {
const safe_access = this.is_declared(compressor)
|| pure_prop_access_globals.has(this.name);
return safe_access ? null : this;
});
def_drop_side_effect_free(AST_Object, function (compressor, first_in_statement) {
var values = trim(this.properties, compressor, first_in_statement);
return values && make_sequence(this, values);
});
def_drop_side_effect_free(AST_ObjectProperty, function (compressor, first_in_statement) {
const computed_key = this instanceof AST_ObjectKeyVal && this.key instanceof AST_Node;
const key = computed_key && this.key.drop_side_effect_free(compressor, first_in_statement);
const value = this.value && this.value.drop_side_effect_free(compressor, first_in_statement);
if (key && value) {
return make_sequence(this, [key, value]);
}
return key || value;
});
def_drop_side_effect_free(AST_ConciseMethod, function () {
return this.computed_key() ? this.key : null;
});
def_drop_side_effect_free(AST_ObjectGetter, function () {
return this.computed_key() ? this.key : null;
});
def_drop_side_effect_free(AST_ObjectSetter, function () {
return this.computed_key() ? this.key : null;
});
def_drop_side_effect_free(AST_Array, function (compressor, first_in_statement) {
var values = trim(this.elements, compressor, first_in_statement);
return values && make_sequence(this, values);
});
def_drop_side_effect_free(AST_Dot, function (compressor, first_in_statement) {
if (is_nullish_shortcircuited(this, compressor)) {
return this.expression.drop_side_effect_free(compressor, first_in_statement);
}
if (this.expression.may_throw_on_access(compressor)) return this;
return this.expression.drop_side_effect_free(compressor, first_in_statement);
});
def_drop_side_effect_free(AST_Sub, function (compressor, first_in_statement) {
if (is_nullish_shortcircuited(this, compressor)) {
return this.expression.drop_side_effect_free(compressor, first_in_statement);
}
if (this.expression.may_throw_on_access(compressor)) return this;
var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
if (!expression)
return this.property.drop_side_effect_free(compressor, first_in_statement);
var property = this.property.drop_side_effect_free(compressor);
if (!property)
return expression;
return make_sequence(this, [expression, property]);
});
def_drop_side_effect_free(AST_Chain, function (compressor, first_in_statement) {
return this.expression.drop_side_effect_free(compressor, first_in_statement);
});
def_drop_side_effect_free(AST_Sequence, function (compressor) {
var last = this.tail_node();
var expr = last.drop_side_effect_free(compressor);
if (expr === last)
return this;
var expressions = this.expressions.slice(0, -1);
if (expr)
expressions.push(expr);
if (!expressions.length) {
return make_node(AST_Number, this, { value: 0 });
}
return make_sequence(this, expressions);
});
def_drop_side_effect_free(AST_Expansion, function (compressor, first_in_statement) {
return this.expression.drop_side_effect_free(compressor, first_in_statement);
});
def_drop_side_effect_free(AST_TemplateSegment, return_null);
def_drop_side_effect_free(AST_TemplateString, function (compressor) {
var values = trim(this.segments, compressor, first_in_statement);
return values && make_sequence(this, values);
});