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.
- package/CHANGELOG.md +38 -0
- package/README.md +23 -4
- package/dist/bundle.min.js +4706 -3723
- package/lib/ast.js +1 -7
- package/lib/cli.js +0 -2
- package/lib/compress/common.js +296 -0
- package/lib/compress/compressor-flags.js +63 -0
- package/lib/compress/drop-side-effect-free.js +350 -0
- package/lib/compress/evaluate.js +461 -0
- package/lib/compress/index.js +489 -3603
- package/lib/compress/inference.js +948 -0
- package/lib/compress/native-objects.js +183 -0
- package/lib/compress/reduce-vars.js +675 -0
- package/lib/compress/tighten-body.js +1461 -0
- package/lib/equivalent-to.js +5 -0
- package/lib/minify.js +3 -1
- package/lib/output.js +20 -7
- package/lib/parse.js +4 -2
- package/lib/propmangle.js +57 -34
- package/lib/scope.js +49 -24
- package/lib/transform.js +2 -2
- package/lib/utils/index.js +2 -4
- package/package.json +10 -10
- package/tools/terser.d.ts +37 -0
- package/bin/terser.mjs +0 -21
package/lib/compress/index.js
CHANGED
@@ -41,34 +41,11 @@
|
|
41
41
|
|
42
42
|
***********************************************************************/
|
43
43
|
|
44
|
-
"use strict";
|
45
|
-
|
46
|
-
import {
|
47
|
-
defaults,
|
48
|
-
HOP,
|
49
|
-
keep_name,
|
50
|
-
make_node,
|
51
|
-
makePredicate,
|
52
|
-
map_add,
|
53
|
-
MAP,
|
54
|
-
member,
|
55
|
-
noop,
|
56
|
-
remove,
|
57
|
-
return_false,
|
58
|
-
return_null,
|
59
|
-
return_this,
|
60
|
-
return_true,
|
61
|
-
string_template,
|
62
|
-
regexp_source_fix,
|
63
|
-
has_annotation
|
64
|
-
} from "../utils/index.js";
|
65
|
-
import { first_in_statement, } from "../utils/first_in_statement.js";
|
66
44
|
import {
|
67
45
|
AST_Accessor,
|
68
46
|
AST_Array,
|
69
47
|
AST_Arrow,
|
70
48
|
AST_Assign,
|
71
|
-
AST_Await,
|
72
49
|
AST_BigInt,
|
73
50
|
AST_Binary,
|
74
51
|
AST_Block,
|
@@ -76,7 +53,6 @@ import {
|
|
76
53
|
AST_Boolean,
|
77
54
|
AST_Break,
|
78
55
|
AST_Call,
|
79
|
-
AST_Case,
|
80
56
|
AST_Catch,
|
81
57
|
AST_Chain,
|
82
58
|
AST_Class,
|
@@ -86,7 +62,6 @@ import {
|
|
86
62
|
AST_Conditional,
|
87
63
|
AST_Const,
|
88
64
|
AST_Constant,
|
89
|
-
AST_Continue,
|
90
65
|
AST_Debugger,
|
91
66
|
AST_Default,
|
92
67
|
AST_DefaultAssign,
|
@@ -103,7 +78,6 @@ import {
|
|
103
78
|
AST_Expansion,
|
104
79
|
AST_Export,
|
105
80
|
AST_False,
|
106
|
-
AST_Finally,
|
107
81
|
AST_For,
|
108
82
|
AST_ForIn,
|
109
83
|
AST_Function,
|
@@ -112,21 +86,17 @@ import {
|
|
112
86
|
AST_Import,
|
113
87
|
AST_Infinity,
|
114
88
|
AST_IterationStatement,
|
115
|
-
AST_Jump,
|
116
89
|
AST_LabeledStatement,
|
117
90
|
AST_Lambda,
|
118
91
|
AST_Let,
|
119
|
-
AST_LoopControl,
|
120
92
|
AST_NaN,
|
121
93
|
AST_New,
|
122
94
|
AST_Node,
|
123
95
|
AST_Null,
|
124
96
|
AST_Number,
|
125
97
|
AST_Object,
|
126
|
-
AST_ObjectGetter,
|
127
98
|
AST_ObjectKeyVal,
|
128
99
|
AST_ObjectProperty,
|
129
|
-
AST_ObjectSetter,
|
130
100
|
AST_PrefixedTemplateString,
|
131
101
|
AST_PropAccess,
|
132
102
|
AST_RegExp,
|
@@ -143,7 +113,6 @@ import {
|
|
143
113
|
AST_SymbolBlockDeclaration,
|
144
114
|
AST_SymbolCatch,
|
145
115
|
AST_SymbolClassProperty,
|
146
|
-
AST_SymbolConst,
|
147
116
|
AST_SymbolDeclaration,
|
148
117
|
AST_SymbolDefun,
|
149
118
|
AST_SymbolExport,
|
@@ -153,7 +122,6 @@ import {
|
|
153
122
|
AST_SymbolMethod,
|
154
123
|
AST_SymbolRef,
|
155
124
|
AST_SymbolVar,
|
156
|
-
AST_TemplateSegment,
|
157
125
|
AST_TemplateString,
|
158
126
|
AST_This,
|
159
127
|
AST_Toplevel,
|
@@ -173,13 +141,28 @@ import {
|
|
173
141
|
TreeWalker,
|
174
142
|
walk,
|
175
143
|
walk_abort,
|
176
|
-
walk_body,
|
177
144
|
walk_parent,
|
178
145
|
|
179
146
|
_INLINE,
|
180
147
|
_NOINLINE,
|
181
148
|
_PURE
|
182
149
|
} from "../ast.js";
|
150
|
+
import {
|
151
|
+
defaults,
|
152
|
+
HOP,
|
153
|
+
keep_name,
|
154
|
+
make_node,
|
155
|
+
makePredicate,
|
156
|
+
map_add,
|
157
|
+
MAP,
|
158
|
+
member,
|
159
|
+
remove,
|
160
|
+
return_false,
|
161
|
+
return_true,
|
162
|
+
regexp_source_fix,
|
163
|
+
has_annotation
|
164
|
+
} from "../utils/index.js";
|
165
|
+
import { first_in_statement } from "../utils/first_in_statement.js";
|
183
166
|
import { equivalent_to } from "../equivalent-to.js";
|
184
167
|
import {
|
185
168
|
is_basic_identifier_string,
|
@@ -194,24 +177,52 @@ import {
|
|
194
177
|
} from "../scope.js";
|
195
178
|
import "../size.js";
|
196
179
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
180
|
+
import "./evaluate.js";
|
181
|
+
import "./drop-side-effect-free.js";
|
182
|
+
import "./reduce-vars.js";
|
183
|
+
import {
|
184
|
+
is_undeclared_ref,
|
185
|
+
lazy_op,
|
186
|
+
is_nullish,
|
187
|
+
is_undefined,
|
188
|
+
is_lhs,
|
189
|
+
aborts,
|
190
|
+
} from "./inference.js";
|
191
|
+
import {
|
192
|
+
SQUEEZED,
|
193
|
+
OPTIMIZED,
|
194
|
+
INLINED,
|
195
|
+
CLEAR_BETWEEN_PASSES,
|
196
|
+
TOP,
|
197
|
+
WRITE_ONLY,
|
198
|
+
UNDEFINED,
|
199
|
+
UNUSED,
|
200
|
+
TRUTHY,
|
201
|
+
FALSY,
|
202
|
+
|
203
|
+
has_flag,
|
204
|
+
set_flag,
|
205
|
+
clear_flag,
|
206
|
+
} from "./compressor-flags.js";
|
207
|
+
import {
|
208
|
+
make_sequence,
|
209
|
+
best_of,
|
210
|
+
best_of_expression,
|
211
|
+
make_node_from_constant,
|
212
|
+
merge_sequence,
|
213
|
+
get_simple_key,
|
214
|
+
has_break_or_continue,
|
215
|
+
maintain_this_binding,
|
216
|
+
identifier_atom,
|
217
|
+
is_identifier_atom,
|
218
|
+
is_func_expr,
|
219
|
+
is_ref_of,
|
220
|
+
can_be_evicted_from_block,
|
221
|
+
as_statement_array,
|
222
|
+
is_iife_call,
|
223
|
+
is_recursive_ref
|
224
|
+
} from "./common.js";
|
225
|
+
import { tighten_body, trim_unreachable_code } from "./tighten-body.js";
|
215
226
|
|
216
227
|
class Compressor extends TreeWalker {
|
217
228
|
constructor(options, { false_by_default = false, mangle_options = false }) {
|
@@ -379,7 +390,8 @@ class Compressor extends TreeWalker {
|
|
379
390
|
var passes = +this.options.passes || 1;
|
380
391
|
var min_count = 1 / 0;
|
381
392
|
var stopping = false;
|
382
|
-
var
|
393
|
+
var nth_identifier = this.mangle_options && this.mangle_options.nth_identifier || base54;
|
394
|
+
var mangle = { ie8: this.option("ie8"), nth_identifier: nth_identifier };
|
383
395
|
for (var pass = 0; pass < passes; pass++) {
|
384
396
|
this._toplevel.figure_out_scope(mangle);
|
385
397
|
if (pass === 0 && this.option("drop_console")) {
|
@@ -488,9 +500,9 @@ AST_Scope.DEFMETHOD("process_expression", function(insert, compressor) {
|
|
488
500
|
if (!insert && node instanceof AST_Return) {
|
489
501
|
if (compressor) {
|
490
502
|
var value = node.value && node.value.drop_side_effect_free(compressor, true);
|
491
|
-
return value
|
492
|
-
body: value
|
493
|
-
|
503
|
+
return value
|
504
|
+
? make_node(AST_SimpleStatement, node, { body: value })
|
505
|
+
: make_node(AST_EmptyStatement, node);
|
494
506
|
}
|
495
507
|
return make_node(AST_SimpleStatement, node, {
|
496
508
|
body: node.value || make_node(AST_UnaryPrefix, node, {
|
@@ -514,3233 +526,83 @@ AST_Scope.DEFMETHOD("process_expression", function(insert, compressor) {
|
|
514
526
|
if (node.alternative) {
|
515
527
|
node.alternative = node.alternative.transform(tt);
|
516
528
|
}
|
517
|
-
} else if (node instanceof AST_With) {
|
518
|
-
node.body = node.body.transform(tt);
|
519
|
-
}
|
520
|
-
return node;
|
521
|
-
});
|
522
|
-
self.transform(tt);
|
523
|
-
});
|
524
|
-
|
525
|
-
function read_property(obj, key) {
|
526
|
-
key = get_value(key);
|
527
|
-
if (key instanceof AST_Node) return;
|
528
|
-
|
529
|
-
var value;
|
530
|
-
if (obj instanceof AST_Array) {
|
531
|
-
var elements = obj.elements;
|
532
|
-
if (key == "length") return make_node_from_constant(elements.length, obj);
|
533
|
-
if (typeof key == "number" && key in elements) value = elements[key];
|
534
|
-
} else if (obj instanceof AST_Object) {
|
535
|
-
key = "" + key;
|
536
|
-
var props = obj.properties;
|
537
|
-
for (var i = props.length; --i >= 0;) {
|
538
|
-
var prop = props[i];
|
539
|
-
if (!(prop instanceof AST_ObjectKeyVal)) return;
|
540
|
-
if (!value && props[i].key === key) value = props[i].value;
|
541
|
-
}
|
542
|
-
}
|
543
|
-
|
544
|
-
return value instanceof AST_SymbolRef && value.fixed_value() || value;
|
545
|
-
}
|
546
|
-
|
547
|
-
function is_modified(compressor, tw, node, value, level, immutable) {
|
548
|
-
var parent = tw.parent(level);
|
549
|
-
var lhs = is_lhs(node, parent);
|
550
|
-
if (lhs) return lhs;
|
551
|
-
if (!immutable
|
552
|
-
&& parent instanceof AST_Call
|
553
|
-
&& parent.expression === node
|
554
|
-
&& !(value instanceof AST_Arrow)
|
555
|
-
&& !(value instanceof AST_Class)
|
556
|
-
&& !parent.is_callee_pure(compressor)
|
557
|
-
&& (!(value instanceof AST_Function)
|
558
|
-
|| !(parent instanceof AST_New) && value.contains_this())) {
|
559
|
-
return true;
|
560
|
-
}
|
561
|
-
if (parent instanceof AST_Array) {
|
562
|
-
return is_modified(compressor, tw, parent, parent, level + 1);
|
563
|
-
}
|
564
|
-
if (parent instanceof AST_ObjectKeyVal && node === parent.value) {
|
565
|
-
var obj = tw.parent(level + 1);
|
566
|
-
return is_modified(compressor, tw, obj, obj, level + 2);
|
567
|
-
}
|
568
|
-
if (parent instanceof AST_PropAccess && parent.expression === node) {
|
569
|
-
var prop = read_property(value, parent.property);
|
570
|
-
return !immutable && is_modified(compressor, tw, parent, prop, level + 1);
|
571
|
-
}
|
572
|
-
}
|
573
|
-
|
574
|
-
(function(def_reduce_vars) {
|
575
|
-
def_reduce_vars(AST_Node, noop);
|
576
|
-
|
577
|
-
function reset_def(compressor, def) {
|
578
|
-
def.assignments = 0;
|
579
|
-
def.chained = false;
|
580
|
-
def.direct_access = false;
|
581
|
-
def.escaped = 0;
|
582
|
-
def.recursive_refs = 0;
|
583
|
-
def.references = [];
|
584
|
-
def.single_use = undefined;
|
585
|
-
if (def.scope.pinned()) {
|
586
|
-
def.fixed = false;
|
587
|
-
} else if (def.orig[0] instanceof AST_SymbolConst || !compressor.exposed(def)) {
|
588
|
-
def.fixed = def.init;
|
589
|
-
} else {
|
590
|
-
def.fixed = false;
|
591
|
-
}
|
592
|
-
}
|
593
|
-
|
594
|
-
function reset_variables(tw, compressor, node) {
|
595
|
-
node.variables.forEach(function(def) {
|
596
|
-
reset_def(compressor, def);
|
597
|
-
if (def.fixed === null) {
|
598
|
-
tw.defs_to_safe_ids.set(def.id, tw.safe_ids);
|
599
|
-
mark(tw, def, true);
|
600
|
-
} else if (def.fixed) {
|
601
|
-
tw.loop_ids.set(def.id, tw.in_loop);
|
602
|
-
mark(tw, def, true);
|
603
|
-
}
|
604
|
-
});
|
605
|
-
}
|
606
|
-
|
607
|
-
function reset_block_variables(compressor, node) {
|
608
|
-
if (node.block_scope) node.block_scope.variables.forEach((def) => {
|
609
|
-
reset_def(compressor, def);
|
610
|
-
});
|
611
|
-
}
|
612
|
-
|
613
|
-
function push(tw) {
|
614
|
-
tw.safe_ids = Object.create(tw.safe_ids);
|
615
|
-
}
|
616
|
-
|
617
|
-
function pop(tw) {
|
618
|
-
tw.safe_ids = Object.getPrototypeOf(tw.safe_ids);
|
619
|
-
}
|
620
|
-
|
621
|
-
function mark(tw, def, safe) {
|
622
|
-
tw.safe_ids[def.id] = safe;
|
623
|
-
}
|
624
|
-
|
625
|
-
function safe_to_read(tw, def) {
|
626
|
-
if (def.single_use == "m") return false;
|
627
|
-
if (tw.safe_ids[def.id]) {
|
628
|
-
if (def.fixed == null) {
|
629
|
-
var orig = def.orig[0];
|
630
|
-
if (orig instanceof AST_SymbolFunarg || orig.name == "arguments") return false;
|
631
|
-
def.fixed = make_node(AST_Undefined, orig);
|
632
|
-
}
|
633
|
-
return true;
|
634
|
-
}
|
635
|
-
return def.fixed instanceof AST_Defun;
|
636
|
-
}
|
637
|
-
|
638
|
-
function safe_to_assign(tw, def, scope, value) {
|
639
|
-
if (def.fixed === undefined) return true;
|
640
|
-
let def_safe_ids;
|
641
|
-
if (def.fixed === null
|
642
|
-
&& (def_safe_ids = tw.defs_to_safe_ids.get(def.id))
|
643
|
-
) {
|
644
|
-
def_safe_ids[def.id] = false;
|
645
|
-
tw.defs_to_safe_ids.delete(def.id);
|
646
|
-
return true;
|
647
|
-
}
|
648
|
-
if (!HOP(tw.safe_ids, def.id)) return false;
|
649
|
-
if (!safe_to_read(tw, def)) return false;
|
650
|
-
if (def.fixed === false) return false;
|
651
|
-
if (def.fixed != null && (!value || def.references.length > def.assignments)) return false;
|
652
|
-
if (def.fixed instanceof AST_Defun) {
|
653
|
-
return value instanceof AST_Node && def.fixed.parent_scope === scope;
|
654
|
-
}
|
655
|
-
return def.orig.every((sym) => {
|
656
|
-
return !(sym instanceof AST_SymbolConst
|
657
|
-
|| sym instanceof AST_SymbolDefun
|
658
|
-
|| sym instanceof AST_SymbolLambda);
|
659
|
-
});
|
660
|
-
}
|
661
|
-
|
662
|
-
function ref_once(tw, compressor, def) {
|
663
|
-
return compressor.option("unused")
|
664
|
-
&& !def.scope.pinned()
|
665
|
-
&& def.references.length - def.recursive_refs == 1
|
666
|
-
&& tw.loop_ids.get(def.id) === tw.in_loop;
|
667
|
-
}
|
668
|
-
|
669
|
-
function is_immutable(value) {
|
670
|
-
if (!value) return false;
|
671
|
-
return value.is_constant()
|
672
|
-
|| value instanceof AST_Lambda
|
673
|
-
|| value instanceof AST_This;
|
674
|
-
}
|
675
|
-
|
676
|
-
// A definition "escapes" when its value can leave the point of use.
|
677
|
-
// Example: `a = b || c`
|
678
|
-
// In this example, "b" and "c" are escaping, because they're going into "a"
|
679
|
-
//
|
680
|
-
// def.escaped is != 0 when it escapes.
|
681
|
-
//
|
682
|
-
// When greater than 1, it means that N chained properties will be read off
|
683
|
-
// of that def before an escape occurs. This is useful for evaluating
|
684
|
-
// property accesses, where you need to know when to stop.
|
685
|
-
function mark_escaped(tw, d, scope, node, value, level = 0, depth = 1) {
|
686
|
-
var parent = tw.parent(level);
|
687
|
-
if (value) {
|
688
|
-
if (value.is_constant()) return;
|
689
|
-
if (value instanceof AST_ClassExpression) return;
|
690
|
-
}
|
691
|
-
|
692
|
-
if (
|
693
|
-
parent instanceof AST_Assign && (parent.operator === "=" || parent.logical) && node === parent.right
|
694
|
-
|| parent instanceof AST_Call && (node !== parent.expression || parent instanceof AST_New)
|
695
|
-
|| parent instanceof AST_Exit && node === parent.value && node.scope !== d.scope
|
696
|
-
|| parent instanceof AST_VarDef && node === parent.value
|
697
|
-
|| parent instanceof AST_Yield && node === parent.value && node.scope !== d.scope
|
698
|
-
) {
|
699
|
-
if (depth > 1 && !(value && value.is_constant_expression(scope))) depth = 1;
|
700
|
-
if (!d.escaped || d.escaped > depth) d.escaped = depth;
|
701
|
-
return;
|
702
|
-
} else if (
|
703
|
-
parent instanceof AST_Array
|
704
|
-
|| parent instanceof AST_Await
|
705
|
-
|| parent instanceof AST_Binary && lazy_op.has(parent.operator)
|
706
|
-
|| parent instanceof AST_Conditional && node !== parent.condition
|
707
|
-
|| parent instanceof AST_Expansion
|
708
|
-
|| parent instanceof AST_Sequence && node === parent.tail_node()
|
709
|
-
) {
|
710
|
-
mark_escaped(tw, d, scope, parent, parent, level + 1, depth);
|
711
|
-
} else if (parent instanceof AST_ObjectKeyVal && node === parent.value) {
|
712
|
-
var obj = tw.parent(level + 1);
|
713
|
-
|
714
|
-
mark_escaped(tw, d, scope, obj, obj, level + 2, depth);
|
715
|
-
} else if (parent instanceof AST_PropAccess && node === parent.expression) {
|
716
|
-
value = read_property(value, parent.property);
|
717
|
-
|
718
|
-
mark_escaped(tw, d, scope, parent, value, level + 1, depth + 1);
|
719
|
-
if (value) return;
|
720
|
-
}
|
721
|
-
|
722
|
-
if (level > 0) return;
|
723
|
-
if (parent instanceof AST_Sequence && node !== parent.tail_node()) return;
|
724
|
-
if (parent instanceof AST_SimpleStatement) return;
|
725
|
-
|
726
|
-
d.direct_access = true;
|
727
|
-
}
|
728
|
-
|
729
|
-
const suppress = node => walk(node, node => {
|
730
|
-
if (!(node instanceof AST_Symbol)) return;
|
731
|
-
var d = node.definition();
|
732
|
-
if (!d) return;
|
733
|
-
if (node instanceof AST_SymbolRef) d.references.push(node);
|
734
|
-
d.fixed = false;
|
735
|
-
});
|
736
|
-
def_reduce_vars(AST_Accessor, function(tw, descend, compressor) {
|
737
|
-
push(tw);
|
738
|
-
reset_variables(tw, compressor, this);
|
739
|
-
descend();
|
740
|
-
pop(tw);
|
741
|
-
return true;
|
742
|
-
});
|
743
|
-
def_reduce_vars(AST_Assign, function(tw, descend, compressor) {
|
744
|
-
var node = this;
|
745
|
-
if (node.left instanceof AST_Destructuring) {
|
746
|
-
suppress(node.left);
|
747
|
-
return;
|
748
|
-
}
|
749
|
-
|
750
|
-
const finish_walk = () => {
|
751
|
-
if (node.logical) {
|
752
|
-
node.left.walk(tw);
|
753
|
-
|
754
|
-
push(tw);
|
755
|
-
node.right.walk(tw);
|
756
|
-
pop(tw);
|
757
|
-
|
758
|
-
return true;
|
759
|
-
}
|
760
|
-
};
|
761
|
-
|
762
|
-
var sym = node.left;
|
763
|
-
if (!(sym instanceof AST_SymbolRef)) return finish_walk();
|
764
|
-
|
765
|
-
var def = sym.definition();
|
766
|
-
var safe = safe_to_assign(tw, def, sym.scope, node.right);
|
767
|
-
def.assignments++;
|
768
|
-
if (!safe) return finish_walk();
|
769
|
-
|
770
|
-
var fixed = def.fixed;
|
771
|
-
if (!fixed && node.operator != "=" && !node.logical) return finish_walk();
|
772
|
-
|
773
|
-
var eq = node.operator == "=";
|
774
|
-
var value = eq ? node.right : node;
|
775
|
-
if (is_modified(compressor, tw, node, value, 0)) return finish_walk();
|
776
|
-
|
777
|
-
def.references.push(sym);
|
778
|
-
|
779
|
-
if (!node.logical) {
|
780
|
-
if (!eq) def.chained = true;
|
781
|
-
|
782
|
-
def.fixed = eq ? function() {
|
783
|
-
return node.right;
|
784
|
-
} : function() {
|
785
|
-
return make_node(AST_Binary, node, {
|
786
|
-
operator: node.operator.slice(0, -1),
|
787
|
-
left: fixed instanceof AST_Node ? fixed : fixed(),
|
788
|
-
right: node.right
|
789
|
-
});
|
790
|
-
};
|
791
|
-
}
|
792
|
-
|
793
|
-
if (node.logical) {
|
794
|
-
mark(tw, def, false);
|
795
|
-
push(tw);
|
796
|
-
node.right.walk(tw);
|
797
|
-
pop(tw);
|
798
|
-
return true;
|
799
|
-
}
|
800
|
-
|
801
|
-
mark(tw, def, false);
|
802
|
-
node.right.walk(tw);
|
803
|
-
mark(tw, def, true);
|
804
|
-
|
805
|
-
mark_escaped(tw, def, sym.scope, node, value, 0, 1);
|
806
|
-
|
807
|
-
return true;
|
808
|
-
});
|
809
|
-
def_reduce_vars(AST_Binary, function(tw) {
|
810
|
-
if (!lazy_op.has(this.operator)) return;
|
811
|
-
this.left.walk(tw);
|
812
|
-
push(tw);
|
813
|
-
this.right.walk(tw);
|
814
|
-
pop(tw);
|
815
|
-
return true;
|
816
|
-
});
|
817
|
-
def_reduce_vars(AST_Block, function(tw, descend, compressor) {
|
818
|
-
reset_block_variables(compressor, this);
|
819
|
-
});
|
820
|
-
def_reduce_vars(AST_Case, function(tw) {
|
821
|
-
push(tw);
|
822
|
-
this.expression.walk(tw);
|
823
|
-
pop(tw);
|
824
|
-
push(tw);
|
825
|
-
walk_body(this, tw);
|
826
|
-
pop(tw);
|
827
|
-
return true;
|
828
|
-
});
|
829
|
-
def_reduce_vars(AST_Class, function(tw, descend) {
|
830
|
-
clear_flag(this, INLINED);
|
831
|
-
push(tw);
|
832
|
-
descend();
|
833
|
-
pop(tw);
|
834
|
-
return true;
|
835
|
-
});
|
836
|
-
def_reduce_vars(AST_Conditional, function(tw) {
|
837
|
-
this.condition.walk(tw);
|
838
|
-
push(tw);
|
839
|
-
this.consequent.walk(tw);
|
840
|
-
pop(tw);
|
841
|
-
push(tw);
|
842
|
-
this.alternative.walk(tw);
|
843
|
-
pop(tw);
|
844
|
-
return true;
|
845
|
-
});
|
846
|
-
|
847
|
-
def_reduce_vars(AST_Chain, function(tw, descend) {
|
848
|
-
// Chains' conditions apply left-to-right, cumulatively.
|
849
|
-
// If we walk normally we don't go in that order because we would pop before pushing again
|
850
|
-
// Solution: AST_PropAccess and AST_Call push when they are optional, and never pop.
|
851
|
-
// Then we pop everything when they are done being walked.
|
852
|
-
const safe_ids = tw.safe_ids;
|
853
|
-
|
854
|
-
descend();
|
855
|
-
|
856
|
-
// Unroll back to start
|
857
|
-
tw.safe_ids = safe_ids;
|
858
|
-
return true;
|
859
|
-
});
|
860
|
-
def_reduce_vars(AST_Call, function (tw) {
|
861
|
-
this.expression.walk(tw);
|
862
|
-
|
863
|
-
if (this.optional) {
|
864
|
-
// Never pop -- it's popped at AST_Chain above
|
865
|
-
push(tw);
|
866
|
-
}
|
867
|
-
|
868
|
-
for (const arg of this.args) arg.walk(tw);
|
869
|
-
|
870
|
-
return true;
|
871
|
-
});
|
872
|
-
def_reduce_vars(AST_PropAccess, function (tw) {
|
873
|
-
if (!this.optional) return;
|
874
|
-
|
875
|
-
this.expression.walk(tw);
|
876
|
-
|
877
|
-
// Never pop -- it's popped at AST_Chain above
|
878
|
-
push(tw);
|
879
|
-
|
880
|
-
if (this.property instanceof AST_Node) this.property.walk(tw);
|
881
|
-
|
882
|
-
return true;
|
883
|
-
});
|
884
|
-
def_reduce_vars(AST_Default, function(tw, descend) {
|
885
|
-
push(tw);
|
886
|
-
descend();
|
887
|
-
pop(tw);
|
888
|
-
return true;
|
889
|
-
});
|
890
|
-
function mark_lambda(tw, descend, compressor) {
|
891
|
-
clear_flag(this, INLINED);
|
892
|
-
push(tw);
|
893
|
-
reset_variables(tw, compressor, this);
|
894
|
-
if (this.uses_arguments) {
|
895
|
-
descend();
|
896
|
-
pop(tw);
|
897
|
-
return;
|
898
|
-
}
|
899
|
-
var iife;
|
900
|
-
if (!this.name
|
901
|
-
&& (iife = tw.parent()) instanceof AST_Call
|
902
|
-
&& iife.expression === this
|
903
|
-
&& !iife.args.some(arg => arg instanceof AST_Expansion)
|
904
|
-
&& this.argnames.every(arg_name => arg_name instanceof AST_Symbol)
|
905
|
-
) {
|
906
|
-
// Virtually turn IIFE parameters into variable definitions:
|
907
|
-
// (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
|
908
|
-
// So existing transformation rules can work on them.
|
909
|
-
this.argnames.forEach((arg, i) => {
|
910
|
-
if (!arg.definition) return;
|
911
|
-
var d = arg.definition();
|
912
|
-
// Avoid setting fixed when there's more than one origin for a variable value
|
913
|
-
if (d.orig.length > 1) return;
|
914
|
-
if (d.fixed === undefined && (!this.uses_arguments || tw.has_directive("use strict"))) {
|
915
|
-
d.fixed = function() {
|
916
|
-
return iife.args[i] || make_node(AST_Undefined, iife);
|
917
|
-
};
|
918
|
-
tw.loop_ids.set(d.id, tw.in_loop);
|
919
|
-
mark(tw, d, true);
|
920
|
-
} else {
|
921
|
-
d.fixed = false;
|
922
|
-
}
|
923
|
-
});
|
924
|
-
}
|
925
|
-
descend();
|
926
|
-
pop(tw);
|
927
|
-
return true;
|
928
|
-
}
|
929
|
-
|
930
|
-
def_reduce_vars(AST_Lambda, mark_lambda);
|
931
|
-
|
932
|
-
def_reduce_vars(AST_Do, function(tw, descend, compressor) {
|
933
|
-
reset_block_variables(compressor, this);
|
934
|
-
const saved_loop = tw.in_loop;
|
935
|
-
tw.in_loop = this;
|
936
|
-
push(tw);
|
937
|
-
this.body.walk(tw);
|
938
|
-
if (has_break_or_continue(this)) {
|
939
|
-
pop(tw);
|
940
|
-
push(tw);
|
941
|
-
}
|
942
|
-
this.condition.walk(tw);
|
943
|
-
pop(tw);
|
944
|
-
tw.in_loop = saved_loop;
|
945
|
-
return true;
|
946
|
-
});
|
947
|
-
def_reduce_vars(AST_For, function(tw, descend, compressor) {
|
948
|
-
reset_block_variables(compressor, this);
|
949
|
-
if (this.init) this.init.walk(tw);
|
950
|
-
const saved_loop = tw.in_loop;
|
951
|
-
tw.in_loop = this;
|
952
|
-
push(tw);
|
953
|
-
if (this.condition) this.condition.walk(tw);
|
954
|
-
this.body.walk(tw);
|
955
|
-
if (this.step) {
|
956
|
-
if (has_break_or_continue(this)) {
|
957
|
-
pop(tw);
|
958
|
-
push(tw);
|
959
|
-
}
|
960
|
-
this.step.walk(tw);
|
961
|
-
}
|
962
|
-
pop(tw);
|
963
|
-
tw.in_loop = saved_loop;
|
964
|
-
return true;
|
965
|
-
});
|
966
|
-
def_reduce_vars(AST_ForIn, function(tw, descend, compressor) {
|
967
|
-
reset_block_variables(compressor, this);
|
968
|
-
suppress(this.init);
|
969
|
-
this.object.walk(tw);
|
970
|
-
const saved_loop = tw.in_loop;
|
971
|
-
tw.in_loop = this;
|
972
|
-
push(tw);
|
973
|
-
this.body.walk(tw);
|
974
|
-
pop(tw);
|
975
|
-
tw.in_loop = saved_loop;
|
976
|
-
return true;
|
977
|
-
});
|
978
|
-
|
979
|
-
def_reduce_vars(AST_If, function(tw) {
|
980
|
-
this.condition.walk(tw);
|
981
|
-
push(tw);
|
982
|
-
this.body.walk(tw);
|
983
|
-
pop(tw);
|
984
|
-
if (this.alternative) {
|
985
|
-
push(tw);
|
986
|
-
this.alternative.walk(tw);
|
987
|
-
pop(tw);
|
988
|
-
}
|
989
|
-
return true;
|
990
|
-
});
|
991
|
-
def_reduce_vars(AST_LabeledStatement, function(tw) {
|
992
|
-
push(tw);
|
993
|
-
this.body.walk(tw);
|
994
|
-
pop(tw);
|
995
|
-
return true;
|
996
|
-
});
|
997
|
-
def_reduce_vars(AST_SymbolCatch, function() {
|
998
|
-
this.definition().fixed = false;
|
999
|
-
});
|
1000
|
-
|
1001
|
-
def_reduce_vars(AST_SymbolRef, function(tw, descend, compressor) {
|
1002
|
-
var d = this.definition();
|
1003
|
-
d.references.push(this);
|
1004
|
-
if (d.references.length == 1
|
1005
|
-
&& !d.fixed
|
1006
|
-
&& d.orig[0] instanceof AST_SymbolDefun) {
|
1007
|
-
tw.loop_ids.set(d.id, tw.in_loop);
|
1008
|
-
}
|
1009
|
-
var fixed_value;
|
1010
|
-
if (d.fixed === undefined || !safe_to_read(tw, d)) {
|
1011
|
-
d.fixed = false;
|
1012
|
-
} else if (d.fixed) {
|
1013
|
-
fixed_value = this.fixed_value();
|
1014
|
-
if (
|
1015
|
-
fixed_value instanceof AST_Lambda
|
1016
|
-
&& recursive_ref(tw, d)
|
1017
|
-
) {
|
1018
|
-
d.recursive_refs++;
|
1019
|
-
} else if (fixed_value
|
1020
|
-
&& !compressor.exposed(d)
|
1021
|
-
&& ref_once(tw, compressor, d)
|
1022
|
-
) {
|
1023
|
-
d.single_use =
|
1024
|
-
fixed_value instanceof AST_Lambda && !fixed_value.pinned()
|
1025
|
-
|| fixed_value instanceof AST_Class
|
1026
|
-
|| d.scope === this.scope && fixed_value.is_constant_expression();
|
1027
|
-
} else {
|
1028
|
-
d.single_use = false;
|
1029
|
-
}
|
1030
|
-
if (is_modified(compressor, tw, this, fixed_value, 0, is_immutable(fixed_value))) {
|
1031
|
-
if (d.single_use) {
|
1032
|
-
d.single_use = "m";
|
1033
|
-
} else {
|
1034
|
-
d.fixed = false;
|
1035
|
-
}
|
1036
|
-
}
|
1037
|
-
}
|
1038
|
-
mark_escaped(tw, d, this.scope, this, fixed_value, 0, 1);
|
1039
|
-
});
|
1040
|
-
def_reduce_vars(AST_Toplevel, function(tw, descend, compressor) {
|
1041
|
-
this.globals.forEach(function(def) {
|
1042
|
-
reset_def(compressor, def);
|
1043
|
-
});
|
1044
|
-
reset_variables(tw, compressor, this);
|
1045
|
-
});
|
1046
|
-
def_reduce_vars(AST_Try, function(tw, descend, compressor) {
|
1047
|
-
reset_block_variables(compressor, this);
|
1048
|
-
push(tw);
|
1049
|
-
walk_body(this, tw);
|
1050
|
-
pop(tw);
|
1051
|
-
if (this.bcatch) {
|
1052
|
-
push(tw);
|
1053
|
-
this.bcatch.walk(tw);
|
1054
|
-
pop(tw);
|
1055
|
-
}
|
1056
|
-
if (this.bfinally) this.bfinally.walk(tw);
|
1057
|
-
return true;
|
1058
|
-
});
|
1059
|
-
def_reduce_vars(AST_Unary, function(tw) {
|
1060
|
-
var node = this;
|
1061
|
-
if (node.operator !== "++" && node.operator !== "--") return;
|
1062
|
-
var exp = node.expression;
|
1063
|
-
if (!(exp instanceof AST_SymbolRef)) return;
|
1064
|
-
var def = exp.definition();
|
1065
|
-
var safe = safe_to_assign(tw, def, exp.scope, true);
|
1066
|
-
def.assignments++;
|
1067
|
-
if (!safe) return;
|
1068
|
-
var fixed = def.fixed;
|
1069
|
-
if (!fixed) return;
|
1070
|
-
def.references.push(exp);
|
1071
|
-
def.chained = true;
|
1072
|
-
def.fixed = function() {
|
1073
|
-
return make_node(AST_Binary, node, {
|
1074
|
-
operator: node.operator.slice(0, -1),
|
1075
|
-
left: make_node(AST_UnaryPrefix, node, {
|
1076
|
-
operator: "+",
|
1077
|
-
expression: fixed instanceof AST_Node ? fixed : fixed()
|
1078
|
-
}),
|
1079
|
-
right: make_node(AST_Number, node, {
|
1080
|
-
value: 1
|
1081
|
-
})
|
1082
|
-
});
|
1083
|
-
};
|
1084
|
-
mark(tw, def, true);
|
1085
|
-
return true;
|
1086
|
-
});
|
1087
|
-
def_reduce_vars(AST_VarDef, function(tw, descend) {
|
1088
|
-
var node = this;
|
1089
|
-
if (node.name instanceof AST_Destructuring) {
|
1090
|
-
suppress(node.name);
|
1091
|
-
return;
|
1092
|
-
}
|
1093
|
-
var d = node.name.definition();
|
1094
|
-
if (node.value) {
|
1095
|
-
if (safe_to_assign(tw, d, node.name.scope, node.value)) {
|
1096
|
-
d.fixed = function() {
|
1097
|
-
return node.value;
|
1098
|
-
};
|
1099
|
-
tw.loop_ids.set(d.id, tw.in_loop);
|
1100
|
-
mark(tw, d, false);
|
1101
|
-
descend();
|
1102
|
-
mark(tw, d, true);
|
1103
|
-
return true;
|
1104
|
-
} else {
|
1105
|
-
d.fixed = false;
|
1106
|
-
}
|
1107
|
-
}
|
1108
|
-
});
|
1109
|
-
def_reduce_vars(AST_While, function(tw, descend, compressor) {
|
1110
|
-
reset_block_variables(compressor, this);
|
1111
|
-
const saved_loop = tw.in_loop;
|
1112
|
-
tw.in_loop = this;
|
1113
|
-
push(tw);
|
1114
|
-
descend();
|
1115
|
-
pop(tw);
|
1116
|
-
tw.in_loop = saved_loop;
|
1117
|
-
return true;
|
1118
|
-
});
|
1119
|
-
})(function(node, func) {
|
1120
|
-
node.DEFMETHOD("reduce_vars", func);
|
1121
|
-
});
|
1122
|
-
|
1123
|
-
AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) {
|
1124
|
-
const self = this;
|
1125
|
-
const reduce_vars = compressor.option("reduce_vars");
|
1126
|
-
|
1127
|
-
const preparation = new TreeWalker(function(node, descend) {
|
1128
|
-
clear_flag(node, CLEAR_BETWEEN_PASSES);
|
1129
|
-
if (reduce_vars) {
|
1130
|
-
if (compressor.top_retain
|
1131
|
-
&& node instanceof AST_Defun // Only functions are retained
|
1132
|
-
&& preparation.parent() === self
|
1133
|
-
) {
|
1134
|
-
set_flag(node, TOP);
|
1135
|
-
}
|
1136
|
-
return node.reduce_vars(preparation, descend, compressor);
|
1137
|
-
}
|
1138
|
-
});
|
1139
|
-
// Stack of look-up tables to keep track of whether a `SymbolDef` has been
|
1140
|
-
// properly assigned before use:
|
1141
|
-
// - `push()` & `pop()` when visiting conditional branches
|
1142
|
-
preparation.safe_ids = Object.create(null);
|
1143
|
-
preparation.in_loop = null;
|
1144
|
-
preparation.loop_ids = new Map();
|
1145
|
-
preparation.defs_to_safe_ids = new Map();
|
1146
|
-
self.walk(preparation);
|
1147
|
-
});
|
1148
|
-
|
1149
|
-
AST_Symbol.DEFMETHOD("fixed_value", function() {
|
1150
|
-
var fixed = this.thedef.fixed;
|
1151
|
-
if (!fixed || fixed instanceof AST_Node) return fixed;
|
1152
|
-
return fixed();
|
1153
|
-
});
|
1154
|
-
|
1155
|
-
AST_SymbolRef.DEFMETHOD("is_immutable", function() {
|
1156
|
-
var orig = this.definition().orig;
|
1157
|
-
return orig.length == 1 && orig[0] instanceof AST_SymbolLambda;
|
1158
|
-
});
|
1159
|
-
|
1160
|
-
function is_func_expr(node) {
|
1161
|
-
return node instanceof AST_Arrow || node instanceof AST_Function;
|
1162
|
-
}
|
1163
|
-
|
1164
|
-
function is_lhs_read_only(lhs) {
|
1165
|
-
if (lhs instanceof AST_This) return true;
|
1166
|
-
if (lhs instanceof AST_SymbolRef) return lhs.definition().orig[0] instanceof AST_SymbolLambda;
|
1167
|
-
if (lhs instanceof AST_PropAccess) {
|
1168
|
-
lhs = lhs.expression;
|
1169
|
-
if (lhs instanceof AST_SymbolRef) {
|
1170
|
-
if (lhs.is_immutable()) return false;
|
1171
|
-
lhs = lhs.fixed_value();
|
1172
|
-
}
|
1173
|
-
if (!lhs) return true;
|
1174
|
-
if (lhs instanceof AST_RegExp) return false;
|
1175
|
-
if (lhs instanceof AST_Constant) return true;
|
1176
|
-
return is_lhs_read_only(lhs);
|
1177
|
-
}
|
1178
|
-
return false;
|
1179
|
-
}
|
1180
|
-
|
1181
|
-
function is_ref_of(ref, type) {
|
1182
|
-
if (!(ref instanceof AST_SymbolRef)) return false;
|
1183
|
-
var orig = ref.definition().orig;
|
1184
|
-
for (var i = orig.length; --i >= 0;) {
|
1185
|
-
if (orig[i] instanceof type) return true;
|
1186
|
-
}
|
1187
|
-
}
|
1188
|
-
|
1189
|
-
function find_scope(tw) {
|
1190
|
-
for (let i = 0;;i++) {
|
1191
|
-
const p = tw.parent(i);
|
1192
|
-
if (p instanceof AST_Toplevel) return p;
|
1193
|
-
if (p instanceof AST_Lambda) return p;
|
1194
|
-
if (p.block_scope) return p.block_scope;
|
1195
|
-
}
|
1196
|
-
}
|
1197
|
-
|
1198
|
-
function find_variable(compressor, name) {
|
1199
|
-
var scope, i = 0;
|
1200
|
-
while (scope = compressor.parent(i++)) {
|
1201
|
-
if (scope instanceof AST_Scope) break;
|
1202
|
-
if (scope instanceof AST_Catch && scope.argname) {
|
1203
|
-
scope = scope.argname.definition().scope;
|
1204
|
-
break;
|
1205
|
-
}
|
1206
|
-
}
|
1207
|
-
return scope.find_variable(name);
|
1208
|
-
}
|
1209
|
-
|
1210
|
-
function make_sequence(orig, expressions) {
|
1211
|
-
if (expressions.length == 1) return expressions[0];
|
1212
|
-
if (expressions.length == 0) throw new Error("trying to create a sequence with length zero!");
|
1213
|
-
return make_node(AST_Sequence, orig, {
|
1214
|
-
expressions: expressions.reduce(merge_sequence, [])
|
1215
|
-
});
|
1216
|
-
}
|
1217
|
-
|
1218
|
-
function make_node_from_constant(val, orig) {
|
1219
|
-
switch (typeof val) {
|
1220
|
-
case "string":
|
1221
|
-
return make_node(AST_String, orig, {
|
1222
|
-
value: val
|
1223
|
-
});
|
1224
|
-
case "number":
|
1225
|
-
if (isNaN(val)) return make_node(AST_NaN, orig);
|
1226
|
-
if (isFinite(val)) {
|
1227
|
-
return 1 / val < 0 ? make_node(AST_UnaryPrefix, orig, {
|
1228
|
-
operator: "-",
|
1229
|
-
expression: make_node(AST_Number, orig, { value: -val })
|
1230
|
-
}) : make_node(AST_Number, orig, { value: val });
|
1231
|
-
}
|
1232
|
-
return val < 0 ? make_node(AST_UnaryPrefix, orig, {
|
1233
|
-
operator: "-",
|
1234
|
-
expression: make_node(AST_Infinity, orig)
|
1235
|
-
}) : make_node(AST_Infinity, orig);
|
1236
|
-
case "boolean":
|
1237
|
-
return make_node(val ? AST_True : AST_False, orig);
|
1238
|
-
case "undefined":
|
1239
|
-
return make_node(AST_Undefined, orig);
|
1240
|
-
default:
|
1241
|
-
if (val === null) {
|
1242
|
-
return make_node(AST_Null, orig, { value: null });
|
1243
|
-
}
|
1244
|
-
if (val instanceof RegExp) {
|
1245
|
-
return make_node(AST_RegExp, orig, {
|
1246
|
-
value: {
|
1247
|
-
source: regexp_source_fix(val.source),
|
1248
|
-
flags: val.flags
|
1249
|
-
}
|
1250
|
-
});
|
1251
|
-
}
|
1252
|
-
throw new Error(string_template("Can't handle constant of type: {type}", {
|
1253
|
-
type: typeof val
|
1254
|
-
}));
|
1255
|
-
}
|
1256
|
-
}
|
1257
|
-
|
1258
|
-
// we shouldn't compress (1,func)(something) to
|
1259
|
-
// func(something) because that changes the meaning of
|
1260
|
-
// the func (becomes lexical instead of global).
|
1261
|
-
function maintain_this_binding(parent, orig, val) {
|
1262
|
-
if (parent instanceof AST_UnaryPrefix && parent.operator == "delete"
|
1263
|
-
|| parent instanceof AST_Call && parent.expression === orig
|
1264
|
-
&& (val instanceof AST_PropAccess || val instanceof AST_SymbolRef && val.name == "eval")) {
|
1265
|
-
return make_sequence(orig, [ make_node(AST_Number, orig, { value: 0 }), val ]);
|
1266
|
-
}
|
1267
|
-
return val;
|
1268
|
-
}
|
1269
|
-
|
1270
|
-
function merge_sequence(array, node) {
|
1271
|
-
if (node instanceof AST_Sequence) {
|
1272
|
-
array.push(...node.expressions);
|
1273
|
-
} else {
|
1274
|
-
array.push(node);
|
1275
|
-
}
|
1276
|
-
return array;
|
1277
|
-
}
|
1278
|
-
|
1279
|
-
function as_statement_array(thing) {
|
1280
|
-
if (thing === null) return [];
|
1281
|
-
if (thing instanceof AST_BlockStatement) return thing.body;
|
1282
|
-
if (thing instanceof AST_EmptyStatement) return [];
|
1283
|
-
if (thing instanceof AST_Statement) return [ thing ];
|
1284
|
-
throw new Error("Can't convert thing to statement array");
|
1285
|
-
}
|
1286
|
-
|
1287
|
-
function is_empty(thing) {
|
1288
|
-
if (thing === null) return true;
|
1289
|
-
if (thing instanceof AST_EmptyStatement) return true;
|
1290
|
-
if (thing instanceof AST_BlockStatement) return thing.body.length == 0;
|
1291
|
-
return false;
|
1292
|
-
}
|
1293
|
-
|
1294
|
-
function can_be_evicted_from_block(node) {
|
1295
|
-
return !(
|
1296
|
-
node instanceof AST_DefClass ||
|
1297
|
-
node instanceof AST_Defun ||
|
1298
|
-
node instanceof AST_Let ||
|
1299
|
-
node instanceof AST_Const ||
|
1300
|
-
node instanceof AST_Export ||
|
1301
|
-
node instanceof AST_Import
|
1302
|
-
);
|
1303
|
-
}
|
1304
|
-
|
1305
|
-
function loop_body(x) {
|
1306
|
-
if (x instanceof AST_IterationStatement) {
|
1307
|
-
return x.body instanceof AST_BlockStatement ? x.body : x;
|
1308
|
-
}
|
1309
|
-
return x;
|
1310
|
-
}
|
1311
|
-
|
1312
|
-
function is_iife_call(node) {
|
1313
|
-
// Used to determine whether the node can benefit from negation.
|
1314
|
-
// Not the case with arrow functions (you need an extra set of parens).
|
1315
|
-
if (node.TYPE != "Call") return false;
|
1316
|
-
return node.expression instanceof AST_Function || is_iife_call(node.expression);
|
1317
|
-
}
|
1318
|
-
|
1319
|
-
function is_undeclared_ref(node) {
|
1320
|
-
return node instanceof AST_SymbolRef && node.definition().undeclared;
|
1321
|
-
}
|
1322
|
-
|
1323
|
-
var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Math Number parseFloat parseInt RangeError ReferenceError RegExp Object setInterval setTimeout String SyntaxError TypeError unescape URIError");
|
1324
|
-
AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) {
|
1325
|
-
return !this.definition().undeclared
|
1326
|
-
|| compressor.option("unsafe") && global_names.has(this.name);
|
1327
|
-
});
|
1328
|
-
|
1329
|
-
var identifier_atom = makePredicate("Infinity NaN undefined");
|
1330
|
-
function is_identifier_atom(node) {
|
1331
|
-
return node instanceof AST_Infinity
|
1332
|
-
|| node instanceof AST_NaN
|
1333
|
-
|| node instanceof AST_Undefined;
|
1334
|
-
}
|
1335
|
-
|
1336
|
-
// Tighten a bunch of statements together. Used whenever there is a block.
|
1337
|
-
function tighten_body(statements, compressor) {
|
1338
|
-
var in_loop, in_try;
|
1339
|
-
var scope = compressor.find_parent(AST_Scope).get_defun_scope();
|
1340
|
-
find_loop_scope_try();
|
1341
|
-
var CHANGED, max_iter = 10;
|
1342
|
-
do {
|
1343
|
-
CHANGED = false;
|
1344
|
-
eliminate_spurious_blocks(statements);
|
1345
|
-
if (compressor.option("dead_code")) {
|
1346
|
-
eliminate_dead_code(statements, compressor);
|
1347
|
-
}
|
1348
|
-
if (compressor.option("if_return")) {
|
1349
|
-
handle_if_return(statements, compressor);
|
1350
|
-
}
|
1351
|
-
if (compressor.sequences_limit > 0) {
|
1352
|
-
sequencesize(statements, compressor);
|
1353
|
-
sequencesize_2(statements, compressor);
|
1354
|
-
}
|
1355
|
-
if (compressor.option("join_vars")) {
|
1356
|
-
join_consecutive_vars(statements);
|
1357
|
-
}
|
1358
|
-
if (compressor.option("collapse_vars")) {
|
1359
|
-
collapse(statements, compressor);
|
1360
|
-
}
|
1361
|
-
} while (CHANGED && max_iter-- > 0);
|
1362
|
-
|
1363
|
-
function find_loop_scope_try() {
|
1364
|
-
var node = compressor.self(), level = 0;
|
1365
|
-
do {
|
1366
|
-
if (node instanceof AST_Catch || node instanceof AST_Finally) {
|
1367
|
-
level++;
|
1368
|
-
} else if (node instanceof AST_IterationStatement) {
|
1369
|
-
in_loop = true;
|
1370
|
-
} else if (node instanceof AST_Scope) {
|
1371
|
-
scope = node;
|
1372
|
-
break;
|
1373
|
-
} else if (node instanceof AST_Try) {
|
1374
|
-
in_try = true;
|
1375
|
-
}
|
1376
|
-
} while (node = compressor.parent(level++));
|
1377
|
-
}
|
1378
|
-
|
1379
|
-
// Search from right to left for assignment-like expressions:
|
1380
|
-
// - `var a = x;`
|
1381
|
-
// - `a = x;`
|
1382
|
-
// - `++a`
|
1383
|
-
// For each candidate, scan from left to right for first usage, then try
|
1384
|
-
// to fold assignment into the site for compression.
|
1385
|
-
// Will not attempt to collapse assignments into or past code blocks
|
1386
|
-
// which are not sequentially executed, e.g. loops and conditionals.
|
1387
|
-
function collapse(statements, compressor) {
|
1388
|
-
if (scope.pinned()) return statements;
|
1389
|
-
var args;
|
1390
|
-
var candidates = [];
|
1391
|
-
var stat_index = statements.length;
|
1392
|
-
var scanner = new TreeTransformer(function(node) {
|
1393
|
-
if (abort) return node;
|
1394
|
-
// Skip nodes before `candidate` as quickly as possible
|
1395
|
-
if (!hit) {
|
1396
|
-
if (node !== hit_stack[hit_index]) return node;
|
1397
|
-
hit_index++;
|
1398
|
-
if (hit_index < hit_stack.length) return handle_custom_scan_order(node);
|
1399
|
-
hit = true;
|
1400
|
-
stop_after = find_stop(node, 0);
|
1401
|
-
if (stop_after === node) abort = true;
|
1402
|
-
return node;
|
1403
|
-
}
|
1404
|
-
// Stop immediately if these node types are encountered
|
1405
|
-
var parent = scanner.parent();
|
1406
|
-
if (node instanceof AST_Assign
|
1407
|
-
&& (node.logical || node.operator != "=" && lhs.equivalent_to(node.left))
|
1408
|
-
|| node instanceof AST_Await
|
1409
|
-
|| node instanceof AST_Call && lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression)
|
1410
|
-
|| node instanceof AST_Debugger
|
1411
|
-
|| node instanceof AST_Destructuring
|
1412
|
-
|| node instanceof AST_Expansion
|
1413
|
-
&& node.expression instanceof AST_Symbol
|
1414
|
-
&& (
|
1415
|
-
node.expression instanceof AST_This
|
1416
|
-
|| node.expression.definition().references.length > 1
|
1417
|
-
)
|
1418
|
-
|| node instanceof AST_IterationStatement && !(node instanceof AST_For)
|
1419
|
-
|| node instanceof AST_LoopControl
|
1420
|
-
|| node instanceof AST_Try
|
1421
|
-
|| node instanceof AST_With
|
1422
|
-
|| node instanceof AST_Yield
|
1423
|
-
|| node instanceof AST_Export
|
1424
|
-
|| node instanceof AST_Class
|
1425
|
-
|| parent instanceof AST_For && node !== parent.init
|
1426
|
-
|| !replace_all
|
1427
|
-
&& (
|
1428
|
-
node instanceof AST_SymbolRef
|
1429
|
-
&& !node.is_declared(compressor)
|
1430
|
-
&& !pure_prop_access_globals.has(node))
|
1431
|
-
|| node instanceof AST_SymbolRef
|
1432
|
-
&& parent instanceof AST_Call
|
1433
|
-
&& has_annotation(parent, _NOINLINE)
|
1434
|
-
) {
|
1435
|
-
abort = true;
|
1436
|
-
return node;
|
1437
|
-
}
|
1438
|
-
// Stop only if candidate is found within conditional branches
|
1439
|
-
if (!stop_if_hit && (!lhs_local || !replace_all)
|
1440
|
-
&& (parent instanceof AST_Binary && lazy_op.has(parent.operator) && parent.left !== node
|
1441
|
-
|| parent instanceof AST_Conditional && parent.condition !== node
|
1442
|
-
|| parent instanceof AST_If && parent.condition !== node)) {
|
1443
|
-
stop_if_hit = parent;
|
1444
|
-
}
|
1445
|
-
// Replace variable with assignment when found
|
1446
|
-
if (can_replace
|
1447
|
-
&& !(node instanceof AST_SymbolDeclaration)
|
1448
|
-
&& lhs.equivalent_to(node)
|
1449
|
-
) {
|
1450
|
-
if (stop_if_hit) {
|
1451
|
-
abort = true;
|
1452
|
-
return node;
|
1453
|
-
}
|
1454
|
-
if (is_lhs(node, parent)) {
|
1455
|
-
if (value_def) replaced++;
|
1456
|
-
return node;
|
1457
|
-
} else {
|
1458
|
-
replaced++;
|
1459
|
-
if (value_def && candidate instanceof AST_VarDef) return node;
|
1460
|
-
}
|
1461
|
-
CHANGED = abort = true;
|
1462
|
-
if (candidate instanceof AST_UnaryPostfix) {
|
1463
|
-
return make_node(AST_UnaryPrefix, candidate, candidate);
|
1464
|
-
}
|
1465
|
-
if (candidate instanceof AST_VarDef) {
|
1466
|
-
var def = candidate.name.definition();
|
1467
|
-
var value = candidate.value;
|
1468
|
-
if (def.references.length - def.replaced == 1 && !compressor.exposed(def)) {
|
1469
|
-
def.replaced++;
|
1470
|
-
if (funarg && is_identifier_atom(value)) {
|
1471
|
-
return value.transform(compressor);
|
1472
|
-
} else {
|
1473
|
-
return maintain_this_binding(parent, node, value);
|
1474
|
-
}
|
1475
|
-
}
|
1476
|
-
return make_node(AST_Assign, candidate, {
|
1477
|
-
operator: "=",
|
1478
|
-
logical: false,
|
1479
|
-
left: make_node(AST_SymbolRef, candidate.name, candidate.name),
|
1480
|
-
right: value
|
1481
|
-
});
|
1482
|
-
}
|
1483
|
-
clear_flag(candidate, WRITE_ONLY);
|
1484
|
-
return candidate;
|
1485
|
-
}
|
1486
|
-
// These node types have child nodes that execute sequentially,
|
1487
|
-
// but are otherwise not safe to scan into or beyond them.
|
1488
|
-
var sym;
|
1489
|
-
if (node instanceof AST_Call
|
1490
|
-
|| node instanceof AST_Exit
|
1491
|
-
&& (side_effects || lhs instanceof AST_PropAccess || may_modify(lhs))
|
1492
|
-
|| node instanceof AST_PropAccess
|
1493
|
-
&& (side_effects || node.expression.may_throw_on_access(compressor))
|
1494
|
-
|| node instanceof AST_SymbolRef
|
1495
|
-
&& (lvalues.get(node.name) || side_effects && may_modify(node))
|
1496
|
-
|| node instanceof AST_VarDef && node.value
|
1497
|
-
&& (lvalues.has(node.name.name) || side_effects && may_modify(node.name))
|
1498
|
-
|| (sym = is_lhs(node.left, node))
|
1499
|
-
&& (sym instanceof AST_PropAccess || lvalues.has(sym.name))
|
1500
|
-
|| may_throw
|
1501
|
-
&& (in_try ? node.has_side_effects(compressor) : side_effects_external(node))) {
|
1502
|
-
stop_after = node;
|
1503
|
-
if (node instanceof AST_Scope) abort = true;
|
1504
|
-
}
|
1505
|
-
return handle_custom_scan_order(node);
|
1506
|
-
}, function(node) {
|
1507
|
-
if (abort) return;
|
1508
|
-
if (stop_after === node) abort = true;
|
1509
|
-
if (stop_if_hit === node) stop_if_hit = null;
|
1510
|
-
});
|
1511
|
-
var multi_replacer = new TreeTransformer(function(node) {
|
1512
|
-
if (abort) return node;
|
1513
|
-
// Skip nodes before `candidate` as quickly as possible
|
1514
|
-
if (!hit) {
|
1515
|
-
if (node !== hit_stack[hit_index]) return node;
|
1516
|
-
hit_index++;
|
1517
|
-
if (hit_index < hit_stack.length) return;
|
1518
|
-
hit = true;
|
1519
|
-
return node;
|
1520
|
-
}
|
1521
|
-
// Replace variable when found
|
1522
|
-
if (node instanceof AST_SymbolRef
|
1523
|
-
&& node.name == def.name) {
|
1524
|
-
if (!--replaced) abort = true;
|
1525
|
-
if (is_lhs(node, multi_replacer.parent())) return node;
|
1526
|
-
def.replaced++;
|
1527
|
-
value_def.replaced--;
|
1528
|
-
return candidate.value;
|
1529
|
-
}
|
1530
|
-
// Skip (non-executed) functions and (leading) default case in switch statements
|
1531
|
-
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
|
1532
|
-
});
|
1533
|
-
while (--stat_index >= 0) {
|
1534
|
-
// Treat parameters as collapsible in IIFE, i.e.
|
1535
|
-
// function(a, b){ ... }(x());
|
1536
|
-
// would be translated into equivalent assignments:
|
1537
|
-
// var a = x(), b = undefined;
|
1538
|
-
if (stat_index == 0 && compressor.option("unused")) extract_args();
|
1539
|
-
// Find collapsible assignments
|
1540
|
-
var hit_stack = [];
|
1541
|
-
extract_candidates(statements[stat_index]);
|
1542
|
-
while (candidates.length > 0) {
|
1543
|
-
hit_stack = candidates.pop();
|
1544
|
-
var hit_index = 0;
|
1545
|
-
var candidate = hit_stack[hit_stack.length - 1];
|
1546
|
-
var value_def = null;
|
1547
|
-
var stop_after = null;
|
1548
|
-
var stop_if_hit = null;
|
1549
|
-
var lhs = get_lhs(candidate);
|
1550
|
-
if (!lhs || is_lhs_read_only(lhs) || lhs.has_side_effects(compressor)) continue;
|
1551
|
-
// Locate symbols which may execute code outside of scanning range
|
1552
|
-
var lvalues = get_lvalues(candidate);
|
1553
|
-
var lhs_local = is_lhs_local(lhs);
|
1554
|
-
if (lhs instanceof AST_SymbolRef) lvalues.set(lhs.name, false);
|
1555
|
-
var side_effects = value_has_side_effects(candidate);
|
1556
|
-
var replace_all = replace_all_symbols();
|
1557
|
-
var may_throw = candidate.may_throw(compressor);
|
1558
|
-
var funarg = candidate.name instanceof AST_SymbolFunarg;
|
1559
|
-
var hit = funarg;
|
1560
|
-
var abort = false, replaced = 0, can_replace = !args || !hit;
|
1561
|
-
if (!can_replace) {
|
1562
|
-
for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; !abort && j < args.length; j++) {
|
1563
|
-
args[j].transform(scanner);
|
1564
|
-
}
|
1565
|
-
can_replace = true;
|
1566
|
-
}
|
1567
|
-
for (var i = stat_index; !abort && i < statements.length; i++) {
|
1568
|
-
statements[i].transform(scanner);
|
1569
|
-
}
|
1570
|
-
if (value_def) {
|
1571
|
-
var def = candidate.name.definition();
|
1572
|
-
if (abort && def.references.length - def.replaced > replaced) replaced = false;
|
1573
|
-
else {
|
1574
|
-
abort = false;
|
1575
|
-
hit_index = 0;
|
1576
|
-
hit = funarg;
|
1577
|
-
for (var i = stat_index; !abort && i < statements.length; i++) {
|
1578
|
-
statements[i].transform(multi_replacer);
|
1579
|
-
}
|
1580
|
-
value_def.single_use = false;
|
1581
|
-
}
|
1582
|
-
}
|
1583
|
-
if (replaced && !remove_candidate(candidate)) statements.splice(stat_index, 1);
|
1584
|
-
}
|
1585
|
-
}
|
1586
|
-
|
1587
|
-
function handle_custom_scan_order(node) {
|
1588
|
-
// Skip (non-executed) functions
|
1589
|
-
if (node instanceof AST_Scope) return node;
|
1590
|
-
|
1591
|
-
// Scan case expressions first in a switch statement
|
1592
|
-
if (node instanceof AST_Switch) {
|
1593
|
-
node.expression = node.expression.transform(scanner);
|
1594
|
-
for (var i = 0, len = node.body.length; !abort && i < len; i++) {
|
1595
|
-
var branch = node.body[i];
|
1596
|
-
if (branch instanceof AST_Case) {
|
1597
|
-
if (!hit) {
|
1598
|
-
if (branch !== hit_stack[hit_index]) continue;
|
1599
|
-
hit_index++;
|
1600
|
-
}
|
1601
|
-
branch.expression = branch.expression.transform(scanner);
|
1602
|
-
if (!replace_all) break;
|
1603
|
-
}
|
1604
|
-
}
|
1605
|
-
abort = true;
|
1606
|
-
return node;
|
1607
|
-
}
|
1608
|
-
}
|
1609
|
-
|
1610
|
-
function redefined_within_scope(def, scope) {
|
1611
|
-
if (def.global) return false;
|
1612
|
-
let cur_scope = def.scope;
|
1613
|
-
while (cur_scope && cur_scope !== scope) {
|
1614
|
-
if (cur_scope.variables.has(def.name)) return true;
|
1615
|
-
cur_scope = cur_scope.parent_scope;
|
1616
|
-
}
|
1617
|
-
return false;
|
1618
|
-
}
|
1619
|
-
|
1620
|
-
function has_overlapping_symbol(fn, arg, fn_strict) {
|
1621
|
-
var found = false, scan_this = !(fn instanceof AST_Arrow);
|
1622
|
-
arg.walk(new TreeWalker(function(node, descend) {
|
1623
|
-
if (found) return true;
|
1624
|
-
if (node instanceof AST_SymbolRef && (fn.variables.has(node.name) || redefined_within_scope(node.definition(), fn))) {
|
1625
|
-
var s = node.definition().scope;
|
1626
|
-
if (s !== scope) while (s = s.parent_scope) {
|
1627
|
-
if (s === scope) return true;
|
1628
|
-
}
|
1629
|
-
return found = true;
|
1630
|
-
}
|
1631
|
-
if ((fn_strict || scan_this) && node instanceof AST_This) {
|
1632
|
-
return found = true;
|
1633
|
-
}
|
1634
|
-
if (node instanceof AST_Scope && !(node instanceof AST_Arrow)) {
|
1635
|
-
var prev = scan_this;
|
1636
|
-
scan_this = false;
|
1637
|
-
descend();
|
1638
|
-
scan_this = prev;
|
1639
|
-
return true;
|
1640
|
-
}
|
1641
|
-
}));
|
1642
|
-
return found;
|
1643
|
-
}
|
1644
|
-
|
1645
|
-
function extract_args() {
|
1646
|
-
var iife, fn = compressor.self();
|
1647
|
-
if (is_func_expr(fn)
|
1648
|
-
&& !fn.name
|
1649
|
-
&& !fn.uses_arguments
|
1650
|
-
&& !fn.pinned()
|
1651
|
-
&& (iife = compressor.parent()) instanceof AST_Call
|
1652
|
-
&& iife.expression === fn
|
1653
|
-
&& iife.args.every((arg) => !(arg instanceof AST_Expansion))
|
1654
|
-
) {
|
1655
|
-
var fn_strict = compressor.has_directive("use strict");
|
1656
|
-
if (fn_strict && !member(fn_strict, fn.body)) fn_strict = false;
|
1657
|
-
var len = fn.argnames.length;
|
1658
|
-
args = iife.args.slice(len);
|
1659
|
-
var names = new Set();
|
1660
|
-
for (var i = len; --i >= 0;) {
|
1661
|
-
var sym = fn.argnames[i];
|
1662
|
-
var arg = iife.args[i];
|
1663
|
-
// The following two line fix is a duplicate of the fix at
|
1664
|
-
// https://github.com/terser/terser/commit/011d3eb08cefe6922c7d1bdfa113fc4aeaca1b75
|
1665
|
-
// This might mean that these two pieces of code (one here in collapse_vars and another in reduce_vars
|
1666
|
-
// Might be doing the exact same thing.
|
1667
|
-
const def = sym.definition && sym.definition();
|
1668
|
-
const is_reassigned = def && def.orig.length > 1;
|
1669
|
-
if (is_reassigned) continue;
|
1670
|
-
args.unshift(make_node(AST_VarDef, sym, {
|
1671
|
-
name: sym,
|
1672
|
-
value: arg
|
1673
|
-
}));
|
1674
|
-
if (names.has(sym.name)) continue;
|
1675
|
-
names.add(sym.name);
|
1676
|
-
if (sym instanceof AST_Expansion) {
|
1677
|
-
var elements = iife.args.slice(i);
|
1678
|
-
if (elements.every((arg) =>
|
1679
|
-
!has_overlapping_symbol(fn, arg, fn_strict)
|
1680
|
-
)) {
|
1681
|
-
candidates.unshift([ make_node(AST_VarDef, sym, {
|
1682
|
-
name: sym.expression,
|
1683
|
-
value: make_node(AST_Array, iife, {
|
1684
|
-
elements: elements
|
1685
|
-
})
|
1686
|
-
}) ]);
|
1687
|
-
}
|
1688
|
-
} else {
|
1689
|
-
if (!arg) {
|
1690
|
-
arg = make_node(AST_Undefined, sym).transform(compressor);
|
1691
|
-
} else if (arg instanceof AST_Lambda && arg.pinned()
|
1692
|
-
|| has_overlapping_symbol(fn, arg, fn_strict)
|
1693
|
-
) {
|
1694
|
-
arg = null;
|
1695
|
-
}
|
1696
|
-
if (arg) candidates.unshift([ make_node(AST_VarDef, sym, {
|
1697
|
-
name: sym,
|
1698
|
-
value: arg
|
1699
|
-
}) ]);
|
1700
|
-
}
|
1701
|
-
}
|
1702
|
-
}
|
1703
|
-
}
|
1704
|
-
|
1705
|
-
function extract_candidates(expr) {
|
1706
|
-
hit_stack.push(expr);
|
1707
|
-
if (expr instanceof AST_Assign) {
|
1708
|
-
if (!expr.left.has_side_effects(compressor)) {
|
1709
|
-
candidates.push(hit_stack.slice());
|
1710
|
-
}
|
1711
|
-
extract_candidates(expr.right);
|
1712
|
-
} else if (expr instanceof AST_Binary) {
|
1713
|
-
extract_candidates(expr.left);
|
1714
|
-
extract_candidates(expr.right);
|
1715
|
-
} else if (expr instanceof AST_Call && !has_annotation(expr, _NOINLINE)) {
|
1716
|
-
extract_candidates(expr.expression);
|
1717
|
-
expr.args.forEach(extract_candidates);
|
1718
|
-
} else if (expr instanceof AST_Case) {
|
1719
|
-
extract_candidates(expr.expression);
|
1720
|
-
} else if (expr instanceof AST_Conditional) {
|
1721
|
-
extract_candidates(expr.condition);
|
1722
|
-
extract_candidates(expr.consequent);
|
1723
|
-
extract_candidates(expr.alternative);
|
1724
|
-
} else if (expr instanceof AST_Definitions) {
|
1725
|
-
var len = expr.definitions.length;
|
1726
|
-
// limit number of trailing variable definitions for consideration
|
1727
|
-
var i = len - 200;
|
1728
|
-
if (i < 0) i = 0;
|
1729
|
-
for (; i < len; i++) {
|
1730
|
-
extract_candidates(expr.definitions[i]);
|
1731
|
-
}
|
1732
|
-
} else if (expr instanceof AST_DWLoop) {
|
1733
|
-
extract_candidates(expr.condition);
|
1734
|
-
if (!(expr.body instanceof AST_Block)) {
|
1735
|
-
extract_candidates(expr.body);
|
1736
|
-
}
|
1737
|
-
} else if (expr instanceof AST_Exit) {
|
1738
|
-
if (expr.value) extract_candidates(expr.value);
|
1739
|
-
} else if (expr instanceof AST_For) {
|
1740
|
-
if (expr.init) extract_candidates(expr.init);
|
1741
|
-
if (expr.condition) extract_candidates(expr.condition);
|
1742
|
-
if (expr.step) extract_candidates(expr.step);
|
1743
|
-
if (!(expr.body instanceof AST_Block)) {
|
1744
|
-
extract_candidates(expr.body);
|
1745
|
-
}
|
1746
|
-
} else if (expr instanceof AST_ForIn) {
|
1747
|
-
extract_candidates(expr.object);
|
1748
|
-
if (!(expr.body instanceof AST_Block)) {
|
1749
|
-
extract_candidates(expr.body);
|
1750
|
-
}
|
1751
|
-
} else if (expr instanceof AST_If) {
|
1752
|
-
extract_candidates(expr.condition);
|
1753
|
-
if (!(expr.body instanceof AST_Block)) {
|
1754
|
-
extract_candidates(expr.body);
|
1755
|
-
}
|
1756
|
-
if (expr.alternative && !(expr.alternative instanceof AST_Block)) {
|
1757
|
-
extract_candidates(expr.alternative);
|
1758
|
-
}
|
1759
|
-
} else if (expr instanceof AST_Sequence) {
|
1760
|
-
expr.expressions.forEach(extract_candidates);
|
1761
|
-
} else if (expr instanceof AST_SimpleStatement) {
|
1762
|
-
extract_candidates(expr.body);
|
1763
|
-
} else if (expr instanceof AST_Switch) {
|
1764
|
-
extract_candidates(expr.expression);
|
1765
|
-
expr.body.forEach(extract_candidates);
|
1766
|
-
} else if (expr instanceof AST_Unary) {
|
1767
|
-
if (expr.operator == "++" || expr.operator == "--") {
|
1768
|
-
candidates.push(hit_stack.slice());
|
1769
|
-
}
|
1770
|
-
} else if (expr instanceof AST_VarDef) {
|
1771
|
-
if (expr.value) {
|
1772
|
-
candidates.push(hit_stack.slice());
|
1773
|
-
extract_candidates(expr.value);
|
1774
|
-
}
|
1775
|
-
}
|
1776
|
-
hit_stack.pop();
|
1777
|
-
}
|
1778
|
-
|
1779
|
-
function find_stop(node, level, write_only) {
|
1780
|
-
var parent = scanner.parent(level);
|
1781
|
-
if (parent instanceof AST_Assign) {
|
1782
|
-
if (write_only
|
1783
|
-
&& !parent.logical
|
1784
|
-
&& !(parent.left instanceof AST_PropAccess
|
1785
|
-
|| lvalues.has(parent.left.name))) {
|
1786
|
-
return find_stop(parent, level + 1, write_only);
|
1787
|
-
}
|
1788
|
-
return node;
|
1789
|
-
}
|
1790
|
-
if (parent instanceof AST_Binary) {
|
1791
|
-
if (write_only && (!lazy_op.has(parent.operator) || parent.left === node)) {
|
1792
|
-
return find_stop(parent, level + 1, write_only);
|
1793
|
-
}
|
1794
|
-
return node;
|
1795
|
-
}
|
1796
|
-
if (parent instanceof AST_Call) return node;
|
1797
|
-
if (parent instanceof AST_Case) return node;
|
1798
|
-
if (parent instanceof AST_Conditional) {
|
1799
|
-
if (write_only && parent.condition === node) {
|
1800
|
-
return find_stop(parent, level + 1, write_only);
|
1801
|
-
}
|
1802
|
-
return node;
|
1803
|
-
}
|
1804
|
-
if (parent instanceof AST_Definitions) {
|
1805
|
-
return find_stop(parent, level + 1, true);
|
1806
|
-
}
|
1807
|
-
if (parent instanceof AST_Exit) {
|
1808
|
-
return write_only ? find_stop(parent, level + 1, write_only) : node;
|
1809
|
-
}
|
1810
|
-
if (parent instanceof AST_If) {
|
1811
|
-
if (write_only && parent.condition === node) {
|
1812
|
-
return find_stop(parent, level + 1, write_only);
|
1813
|
-
}
|
1814
|
-
return node;
|
1815
|
-
}
|
1816
|
-
if (parent instanceof AST_IterationStatement) return node;
|
1817
|
-
if (parent instanceof AST_Sequence) {
|
1818
|
-
return find_stop(parent, level + 1, parent.tail_node() !== node);
|
1819
|
-
}
|
1820
|
-
if (parent instanceof AST_SimpleStatement) {
|
1821
|
-
return find_stop(parent, level + 1, true);
|
1822
|
-
}
|
1823
|
-
if (parent instanceof AST_Switch) return node;
|
1824
|
-
if (parent instanceof AST_VarDef) return node;
|
1825
|
-
return null;
|
1826
|
-
}
|
1827
|
-
|
1828
|
-
function mangleable_var(var_def) {
|
1829
|
-
var value = var_def.value;
|
1830
|
-
if (!(value instanceof AST_SymbolRef)) return;
|
1831
|
-
if (value.name == "arguments") return;
|
1832
|
-
var def = value.definition();
|
1833
|
-
if (def.undeclared) return;
|
1834
|
-
return value_def = def;
|
1835
|
-
}
|
1836
|
-
|
1837
|
-
function get_lhs(expr) {
|
1838
|
-
if (expr instanceof AST_Assign && expr.logical) {
|
1839
|
-
return false;
|
1840
|
-
} else if (expr instanceof AST_VarDef && expr.name instanceof AST_SymbolDeclaration) {
|
1841
|
-
var def = expr.name.definition();
|
1842
|
-
if (!member(expr.name, def.orig)) return;
|
1843
|
-
var referenced = def.references.length - def.replaced;
|
1844
|
-
if (!referenced) return;
|
1845
|
-
var declared = def.orig.length - def.eliminated;
|
1846
|
-
if (declared > 1 && !(expr.name instanceof AST_SymbolFunarg)
|
1847
|
-
|| (referenced > 1 ? mangleable_var(expr) : !compressor.exposed(def))) {
|
1848
|
-
return make_node(AST_SymbolRef, expr.name, expr.name);
|
1849
|
-
}
|
1850
|
-
} else {
|
1851
|
-
const lhs = expr instanceof AST_Assign
|
1852
|
-
? expr.left
|
1853
|
-
: expr.expression;
|
1854
|
-
return !is_ref_of(lhs, AST_SymbolConst)
|
1855
|
-
&& !is_ref_of(lhs, AST_SymbolLet) && lhs;
|
1856
|
-
}
|
1857
|
-
}
|
1858
|
-
|
1859
|
-
function get_rvalue(expr) {
|
1860
|
-
if (expr instanceof AST_Assign) {
|
1861
|
-
return expr.right;
|
1862
|
-
} else {
|
1863
|
-
return expr.value;
|
1864
|
-
}
|
1865
|
-
}
|
1866
|
-
|
1867
|
-
function get_lvalues(expr) {
|
1868
|
-
var lvalues = new Map();
|
1869
|
-
if (expr instanceof AST_Unary) return lvalues;
|
1870
|
-
var tw = new TreeWalker(function(node) {
|
1871
|
-
var sym = node;
|
1872
|
-
while (sym instanceof AST_PropAccess) sym = sym.expression;
|
1873
|
-
if (sym instanceof AST_SymbolRef || sym instanceof AST_This) {
|
1874
|
-
lvalues.set(sym.name, lvalues.get(sym.name) || is_modified(compressor, tw, node, node, 0));
|
1875
|
-
}
|
1876
|
-
});
|
1877
|
-
get_rvalue(expr).walk(tw);
|
1878
|
-
return lvalues;
|
1879
|
-
}
|
1880
|
-
|
1881
|
-
function remove_candidate(expr) {
|
1882
|
-
if (expr.name instanceof AST_SymbolFunarg) {
|
1883
|
-
var iife = compressor.parent(), argnames = compressor.self().argnames;
|
1884
|
-
var index = argnames.indexOf(expr.name);
|
1885
|
-
if (index < 0) {
|
1886
|
-
iife.args.length = Math.min(iife.args.length, argnames.length - 1);
|
1887
|
-
} else {
|
1888
|
-
var args = iife.args;
|
1889
|
-
if (args[index]) args[index] = make_node(AST_Number, args[index], {
|
1890
|
-
value: 0
|
1891
|
-
});
|
1892
|
-
}
|
1893
|
-
return true;
|
1894
|
-
}
|
1895
|
-
var found = false;
|
1896
|
-
return statements[stat_index].transform(new TreeTransformer(function(node, descend, in_list) {
|
1897
|
-
if (found) return node;
|
1898
|
-
if (node === expr || node.body === expr) {
|
1899
|
-
found = true;
|
1900
|
-
if (node instanceof AST_VarDef) {
|
1901
|
-
node.value = node.name instanceof AST_SymbolConst
|
1902
|
-
? make_node(AST_Undefined, node.value) // `const` always needs value.
|
1903
|
-
: null;
|
1904
|
-
return node;
|
1905
|
-
}
|
1906
|
-
return in_list ? MAP.skip : null;
|
1907
|
-
}
|
1908
|
-
}, function(node) {
|
1909
|
-
if (node instanceof AST_Sequence) switch (node.expressions.length) {
|
1910
|
-
case 0: return null;
|
1911
|
-
case 1: return node.expressions[0];
|
1912
|
-
}
|
1913
|
-
}));
|
1914
|
-
}
|
1915
|
-
|
1916
|
-
function is_lhs_local(lhs) {
|
1917
|
-
while (lhs instanceof AST_PropAccess) lhs = lhs.expression;
|
1918
|
-
return lhs instanceof AST_SymbolRef
|
1919
|
-
&& lhs.definition().scope === scope
|
1920
|
-
&& !(in_loop
|
1921
|
-
&& (lvalues.has(lhs.name)
|
1922
|
-
|| candidate instanceof AST_Unary
|
1923
|
-
|| (candidate instanceof AST_Assign
|
1924
|
-
&& !candidate.logical
|
1925
|
-
&& candidate.operator != "=")));
|
1926
|
-
}
|
1927
|
-
|
1928
|
-
function value_has_side_effects(expr) {
|
1929
|
-
if (expr instanceof AST_Unary) return unary_side_effects.has(expr.operator);
|
1930
|
-
return get_rvalue(expr).has_side_effects(compressor);
|
1931
|
-
}
|
1932
|
-
|
1933
|
-
function replace_all_symbols() {
|
1934
|
-
if (side_effects) return false;
|
1935
|
-
if (value_def) return true;
|
1936
|
-
if (lhs instanceof AST_SymbolRef) {
|
1937
|
-
var def = lhs.definition();
|
1938
|
-
if (def.references.length - def.replaced == (candidate instanceof AST_VarDef ? 1 : 2)) {
|
1939
|
-
return true;
|
1940
|
-
}
|
1941
|
-
}
|
1942
|
-
return false;
|
1943
|
-
}
|
1944
|
-
|
1945
|
-
function may_modify(sym) {
|
1946
|
-
if (!sym.definition) return true; // AST_Destructuring
|
1947
|
-
var def = sym.definition();
|
1948
|
-
if (def.orig.length == 1 && def.orig[0] instanceof AST_SymbolDefun) return false;
|
1949
|
-
if (def.scope.get_defun_scope() !== scope) return true;
|
1950
|
-
return !def.references.every((ref) => {
|
1951
|
-
var s = ref.scope.get_defun_scope();
|
1952
|
-
// "block" scope within AST_Catch
|
1953
|
-
if (s.TYPE == "Scope") s = s.parent_scope;
|
1954
|
-
return s === scope;
|
1955
|
-
});
|
1956
|
-
}
|
1957
|
-
|
1958
|
-
function side_effects_external(node, lhs) {
|
1959
|
-
if (node instanceof AST_Assign) return side_effects_external(node.left, true);
|
1960
|
-
if (node instanceof AST_Unary) return side_effects_external(node.expression, true);
|
1961
|
-
if (node instanceof AST_VarDef) return node.value && side_effects_external(node.value);
|
1962
|
-
if (lhs) {
|
1963
|
-
if (node instanceof AST_Dot) return side_effects_external(node.expression, true);
|
1964
|
-
if (node instanceof AST_Sub) return side_effects_external(node.expression, true);
|
1965
|
-
if (node instanceof AST_SymbolRef) return node.definition().scope !== scope;
|
1966
|
-
}
|
1967
|
-
return false;
|
1968
|
-
}
|
1969
|
-
}
|
1970
|
-
|
1971
|
-
function eliminate_spurious_blocks(statements) {
|
1972
|
-
var seen_dirs = [];
|
1973
|
-
for (var i = 0; i < statements.length;) {
|
1974
|
-
var stat = statements[i];
|
1975
|
-
if (stat instanceof AST_BlockStatement && stat.body.every(can_be_evicted_from_block)) {
|
1976
|
-
CHANGED = true;
|
1977
|
-
eliminate_spurious_blocks(stat.body);
|
1978
|
-
statements.splice(i, 1, ...stat.body);
|
1979
|
-
i += stat.body.length;
|
1980
|
-
} else if (stat instanceof AST_EmptyStatement) {
|
1981
|
-
CHANGED = true;
|
1982
|
-
statements.splice(i, 1);
|
1983
|
-
} else if (stat instanceof AST_Directive) {
|
1984
|
-
if (seen_dirs.indexOf(stat.value) < 0) {
|
1985
|
-
i++;
|
1986
|
-
seen_dirs.push(stat.value);
|
1987
|
-
} else {
|
1988
|
-
CHANGED = true;
|
1989
|
-
statements.splice(i, 1);
|
1990
|
-
}
|
1991
|
-
} else i++;
|
1992
|
-
}
|
1993
|
-
}
|
1994
|
-
|
1995
|
-
function handle_if_return(statements, compressor) {
|
1996
|
-
var self = compressor.self();
|
1997
|
-
var multiple_if_returns = has_multiple_if_returns(statements);
|
1998
|
-
var in_lambda = self instanceof AST_Lambda;
|
1999
|
-
for (var i = statements.length; --i >= 0;) {
|
2000
|
-
var stat = statements[i];
|
2001
|
-
var j = next_index(i);
|
2002
|
-
var next = statements[j];
|
2003
|
-
|
2004
|
-
if (in_lambda && !next && stat instanceof AST_Return) {
|
2005
|
-
if (!stat.value) {
|
2006
|
-
CHANGED = true;
|
2007
|
-
statements.splice(i, 1);
|
2008
|
-
continue;
|
2009
|
-
}
|
2010
|
-
if (stat.value instanceof AST_UnaryPrefix && stat.value.operator == "void") {
|
2011
|
-
CHANGED = true;
|
2012
|
-
statements[i] = make_node(AST_SimpleStatement, stat, {
|
2013
|
-
body: stat.value.expression
|
2014
|
-
});
|
2015
|
-
continue;
|
2016
|
-
}
|
2017
|
-
}
|
2018
|
-
|
2019
|
-
if (stat instanceof AST_If) {
|
2020
|
-
var ab = aborts(stat.body);
|
2021
|
-
if (can_merge_flow(ab)) {
|
2022
|
-
if (ab.label) {
|
2023
|
-
remove(ab.label.thedef.references, ab);
|
2024
|
-
}
|
2025
|
-
CHANGED = true;
|
2026
|
-
stat = stat.clone();
|
2027
|
-
stat.condition = stat.condition.negate(compressor);
|
2028
|
-
var body = as_statement_array_with_return(stat.body, ab);
|
2029
|
-
stat.body = make_node(AST_BlockStatement, stat, {
|
2030
|
-
body: as_statement_array(stat.alternative).concat(extract_functions())
|
2031
|
-
});
|
2032
|
-
stat.alternative = make_node(AST_BlockStatement, stat, {
|
2033
|
-
body: body
|
2034
|
-
});
|
2035
|
-
statements[i] = stat.transform(compressor);
|
2036
|
-
continue;
|
2037
|
-
}
|
2038
|
-
|
2039
|
-
var ab = aborts(stat.alternative);
|
2040
|
-
if (can_merge_flow(ab)) {
|
2041
|
-
if (ab.label) {
|
2042
|
-
remove(ab.label.thedef.references, ab);
|
2043
|
-
}
|
2044
|
-
CHANGED = true;
|
2045
|
-
stat = stat.clone();
|
2046
|
-
stat.body = make_node(AST_BlockStatement, stat.body, {
|
2047
|
-
body: as_statement_array(stat.body).concat(extract_functions())
|
2048
|
-
});
|
2049
|
-
var body = as_statement_array_with_return(stat.alternative, ab);
|
2050
|
-
stat.alternative = make_node(AST_BlockStatement, stat.alternative, {
|
2051
|
-
body: body
|
2052
|
-
});
|
2053
|
-
statements[i] = stat.transform(compressor);
|
2054
|
-
continue;
|
2055
|
-
}
|
2056
|
-
}
|
2057
|
-
|
2058
|
-
if (stat instanceof AST_If && stat.body instanceof AST_Return) {
|
2059
|
-
var value = stat.body.value;
|
2060
|
-
//---
|
2061
|
-
// pretty silly case, but:
|
2062
|
-
// if (foo()) return; return; ==> foo(); return;
|
2063
|
-
if (!value && !stat.alternative
|
2064
|
-
&& (in_lambda && !next || next instanceof AST_Return && !next.value)) {
|
2065
|
-
CHANGED = true;
|
2066
|
-
statements[i] = make_node(AST_SimpleStatement, stat.condition, {
|
2067
|
-
body: stat.condition
|
2068
|
-
});
|
2069
|
-
continue;
|
2070
|
-
}
|
2071
|
-
//---
|
2072
|
-
// if (foo()) return x; return y; ==> return foo() ? x : y;
|
2073
|
-
if (value && !stat.alternative && next instanceof AST_Return && next.value) {
|
2074
|
-
CHANGED = true;
|
2075
|
-
stat = stat.clone();
|
2076
|
-
stat.alternative = next;
|
2077
|
-
statements[i] = stat.transform(compressor);
|
2078
|
-
statements.splice(j, 1);
|
2079
|
-
continue;
|
2080
|
-
}
|
2081
|
-
//---
|
2082
|
-
// if (foo()) return x; [ return ; ] ==> return foo() ? x : undefined;
|
2083
|
-
if (value && !stat.alternative
|
2084
|
-
&& (!next && in_lambda && multiple_if_returns
|
2085
|
-
|| next instanceof AST_Return)) {
|
2086
|
-
CHANGED = true;
|
2087
|
-
stat = stat.clone();
|
2088
|
-
stat.alternative = next || make_node(AST_Return, stat, {
|
2089
|
-
value: null
|
2090
|
-
});
|
2091
|
-
statements[i] = stat.transform(compressor);
|
2092
|
-
if (next) statements.splice(j, 1);
|
2093
|
-
continue;
|
2094
|
-
}
|
2095
|
-
//---
|
2096
|
-
// if (a) return b; if (c) return d; e; ==> return a ? b : c ? d : void e;
|
2097
|
-
//
|
2098
|
-
// if sequences is not enabled, this can lead to an endless loop (issue #866).
|
2099
|
-
// however, with sequences on this helps producing slightly better output for
|
2100
|
-
// the example code.
|
2101
|
-
var prev = statements[prev_index(i)];
|
2102
|
-
if (compressor.option("sequences") && in_lambda && !stat.alternative
|
2103
|
-
&& prev instanceof AST_If && prev.body instanceof AST_Return
|
2104
|
-
&& next_index(j) == statements.length && next instanceof AST_SimpleStatement) {
|
2105
|
-
CHANGED = true;
|
2106
|
-
stat = stat.clone();
|
2107
|
-
stat.alternative = make_node(AST_BlockStatement, next, {
|
2108
|
-
body: [
|
2109
|
-
next,
|
2110
|
-
make_node(AST_Return, next, {
|
2111
|
-
value: null
|
2112
|
-
})
|
2113
|
-
]
|
2114
|
-
});
|
2115
|
-
statements[i] = stat.transform(compressor);
|
2116
|
-
statements.splice(j, 1);
|
2117
|
-
continue;
|
2118
|
-
}
|
2119
|
-
}
|
2120
|
-
}
|
2121
|
-
|
2122
|
-
function has_multiple_if_returns(statements) {
|
2123
|
-
var n = 0;
|
2124
|
-
for (var i = statements.length; --i >= 0;) {
|
2125
|
-
var stat = statements[i];
|
2126
|
-
if (stat instanceof AST_If && stat.body instanceof AST_Return) {
|
2127
|
-
if (++n > 1) return true;
|
2128
|
-
}
|
2129
|
-
}
|
2130
|
-
return false;
|
2131
|
-
}
|
2132
|
-
|
2133
|
-
function is_return_void(value) {
|
2134
|
-
return !value || value instanceof AST_UnaryPrefix && value.operator == "void";
|
2135
|
-
}
|
2136
|
-
|
2137
|
-
function can_merge_flow(ab) {
|
2138
|
-
if (!ab) return false;
|
2139
|
-
for (var j = i + 1, len = statements.length; j < len; j++) {
|
2140
|
-
var stat = statements[j];
|
2141
|
-
if (stat instanceof AST_Const || stat instanceof AST_Let) return false;
|
2142
|
-
}
|
2143
|
-
var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab) : null;
|
2144
|
-
return ab instanceof AST_Return && in_lambda && is_return_void(ab.value)
|
2145
|
-
|| ab instanceof AST_Continue && self === loop_body(lct)
|
2146
|
-
|| ab instanceof AST_Break && lct instanceof AST_BlockStatement && self === lct;
|
2147
|
-
}
|
2148
|
-
|
2149
|
-
function extract_functions() {
|
2150
|
-
var tail = statements.slice(i + 1);
|
2151
|
-
statements.length = i + 1;
|
2152
|
-
return tail.filter(function(stat) {
|
2153
|
-
if (stat instanceof AST_Defun) {
|
2154
|
-
statements.push(stat);
|
2155
|
-
return false;
|
2156
|
-
}
|
2157
|
-
return true;
|
2158
|
-
});
|
2159
|
-
}
|
2160
|
-
|
2161
|
-
function as_statement_array_with_return(node, ab) {
|
2162
|
-
var body = as_statement_array(node).slice(0, -1);
|
2163
|
-
if (ab.value) {
|
2164
|
-
body.push(make_node(AST_SimpleStatement, ab.value, {
|
2165
|
-
body: ab.value.expression
|
2166
|
-
}));
|
2167
|
-
}
|
2168
|
-
return body;
|
2169
|
-
}
|
2170
|
-
|
2171
|
-
function next_index(i) {
|
2172
|
-
for (var j = i + 1, len = statements.length; j < len; j++) {
|
2173
|
-
var stat = statements[j];
|
2174
|
-
if (!(stat instanceof AST_Var && declarations_only(stat))) {
|
2175
|
-
break;
|
2176
|
-
}
|
2177
|
-
}
|
2178
|
-
return j;
|
2179
|
-
}
|
2180
|
-
|
2181
|
-
function prev_index(i) {
|
2182
|
-
for (var j = i; --j >= 0;) {
|
2183
|
-
var stat = statements[j];
|
2184
|
-
if (!(stat instanceof AST_Var && declarations_only(stat))) {
|
2185
|
-
break;
|
2186
|
-
}
|
2187
|
-
}
|
2188
|
-
return j;
|
2189
|
-
}
|
2190
|
-
}
|
2191
|
-
|
2192
|
-
function eliminate_dead_code(statements, compressor) {
|
2193
|
-
var has_quit;
|
2194
|
-
var self = compressor.self();
|
2195
|
-
for (var i = 0, n = 0, len = statements.length; i < len; i++) {
|
2196
|
-
var stat = statements[i];
|
2197
|
-
if (stat instanceof AST_LoopControl) {
|
2198
|
-
var lct = compressor.loopcontrol_target(stat);
|
2199
|
-
if (stat instanceof AST_Break
|
2200
|
-
&& !(lct instanceof AST_IterationStatement)
|
2201
|
-
&& loop_body(lct) === self
|
2202
|
-
|| stat instanceof AST_Continue
|
2203
|
-
&& loop_body(lct) === self) {
|
2204
|
-
if (stat.label) {
|
2205
|
-
remove(stat.label.thedef.references, stat);
|
2206
|
-
}
|
2207
|
-
} else {
|
2208
|
-
statements[n++] = stat;
|
2209
|
-
}
|
2210
|
-
} else {
|
2211
|
-
statements[n++] = stat;
|
2212
|
-
}
|
2213
|
-
if (aborts(stat)) {
|
2214
|
-
has_quit = statements.slice(i + 1);
|
2215
|
-
break;
|
2216
|
-
}
|
2217
|
-
}
|
2218
|
-
statements.length = n;
|
2219
|
-
CHANGED = n != len;
|
2220
|
-
if (has_quit) has_quit.forEach(function(stat) {
|
2221
|
-
trim_unreachable_code(compressor, stat, statements);
|
2222
|
-
});
|
2223
|
-
}
|
2224
|
-
|
2225
|
-
function declarations_only(node) {
|
2226
|
-
return node.definitions.every((var_def) =>
|
2227
|
-
!var_def.value
|
2228
|
-
);
|
2229
|
-
}
|
2230
|
-
|
2231
|
-
function sequencesize(statements, compressor) {
|
2232
|
-
if (statements.length < 2) return;
|
2233
|
-
var seq = [], n = 0;
|
2234
|
-
function push_seq() {
|
2235
|
-
if (!seq.length) return;
|
2236
|
-
var body = make_sequence(seq[0], seq);
|
2237
|
-
statements[n++] = make_node(AST_SimpleStatement, body, { body: body });
|
2238
|
-
seq = [];
|
2239
|
-
}
|
2240
|
-
for (var i = 0, len = statements.length; i < len; i++) {
|
2241
|
-
var stat = statements[i];
|
2242
|
-
if (stat instanceof AST_SimpleStatement) {
|
2243
|
-
if (seq.length >= compressor.sequences_limit) push_seq();
|
2244
|
-
var body = stat.body;
|
2245
|
-
if (seq.length > 0) body = body.drop_side_effect_free(compressor);
|
2246
|
-
if (body) merge_sequence(seq, body);
|
2247
|
-
} else if (stat instanceof AST_Definitions && declarations_only(stat)
|
2248
|
-
|| stat instanceof AST_Defun) {
|
2249
|
-
statements[n++] = stat;
|
2250
|
-
} else {
|
2251
|
-
push_seq();
|
2252
|
-
statements[n++] = stat;
|
2253
|
-
}
|
2254
|
-
}
|
2255
|
-
push_seq();
|
2256
|
-
statements.length = n;
|
2257
|
-
if (n != len) CHANGED = true;
|
2258
|
-
}
|
2259
|
-
|
2260
|
-
function to_simple_statement(block, decls) {
|
2261
|
-
if (!(block instanceof AST_BlockStatement)) return block;
|
2262
|
-
var stat = null;
|
2263
|
-
for (var i = 0, len = block.body.length; i < len; i++) {
|
2264
|
-
var line = block.body[i];
|
2265
|
-
if (line instanceof AST_Var && declarations_only(line)) {
|
2266
|
-
decls.push(line);
|
2267
|
-
} else if (stat) {
|
2268
|
-
return false;
|
2269
|
-
} else {
|
2270
|
-
stat = line;
|
2271
|
-
}
|
2272
|
-
}
|
2273
|
-
return stat;
|
2274
|
-
}
|
2275
|
-
|
2276
|
-
function sequencesize_2(statements, compressor) {
|
2277
|
-
function cons_seq(right) {
|
2278
|
-
n--;
|
2279
|
-
CHANGED = true;
|
2280
|
-
var left = prev.body;
|
2281
|
-
return make_sequence(left, [ left, right ]).transform(compressor);
|
2282
|
-
}
|
2283
|
-
var n = 0, prev;
|
2284
|
-
for (var i = 0; i < statements.length; i++) {
|
2285
|
-
var stat = statements[i];
|
2286
|
-
if (prev) {
|
2287
|
-
if (stat instanceof AST_Exit) {
|
2288
|
-
stat.value = cons_seq(stat.value || make_node(AST_Undefined, stat).transform(compressor));
|
2289
|
-
} else if (stat instanceof AST_For) {
|
2290
|
-
if (!(stat.init instanceof AST_Definitions)) {
|
2291
|
-
const abort = walk(prev.body, node => {
|
2292
|
-
if (node instanceof AST_Scope) return true;
|
2293
|
-
if (
|
2294
|
-
node instanceof AST_Binary
|
2295
|
-
&& node.operator === "in"
|
2296
|
-
) {
|
2297
|
-
return walk_abort;
|
2298
|
-
}
|
2299
|
-
});
|
2300
|
-
if (!abort) {
|
2301
|
-
if (stat.init) stat.init = cons_seq(stat.init);
|
2302
|
-
else {
|
2303
|
-
stat.init = prev.body;
|
2304
|
-
n--;
|
2305
|
-
CHANGED = true;
|
2306
|
-
}
|
2307
|
-
}
|
2308
|
-
}
|
2309
|
-
} else if (stat instanceof AST_ForIn) {
|
2310
|
-
if (!(stat.init instanceof AST_Const) && !(stat.init instanceof AST_Let)) {
|
2311
|
-
stat.object = cons_seq(stat.object);
|
2312
|
-
}
|
2313
|
-
} else if (stat instanceof AST_If) {
|
2314
|
-
stat.condition = cons_seq(stat.condition);
|
2315
|
-
} else if (stat instanceof AST_Switch) {
|
2316
|
-
stat.expression = cons_seq(stat.expression);
|
2317
|
-
} else if (stat instanceof AST_With) {
|
2318
|
-
stat.expression = cons_seq(stat.expression);
|
2319
|
-
}
|
2320
|
-
}
|
2321
|
-
if (compressor.option("conditionals") && stat instanceof AST_If) {
|
2322
|
-
var decls = [];
|
2323
|
-
var body = to_simple_statement(stat.body, decls);
|
2324
|
-
var alt = to_simple_statement(stat.alternative, decls);
|
2325
|
-
if (body !== false && alt !== false && decls.length > 0) {
|
2326
|
-
var len = decls.length;
|
2327
|
-
decls.push(make_node(AST_If, stat, {
|
2328
|
-
condition: stat.condition,
|
2329
|
-
body: body || make_node(AST_EmptyStatement, stat.body),
|
2330
|
-
alternative: alt
|
2331
|
-
}));
|
2332
|
-
decls.unshift(n, 1);
|
2333
|
-
[].splice.apply(statements, decls);
|
2334
|
-
i += len;
|
2335
|
-
n += len + 1;
|
2336
|
-
prev = null;
|
2337
|
-
CHANGED = true;
|
2338
|
-
continue;
|
2339
|
-
}
|
2340
|
-
}
|
2341
|
-
statements[n++] = stat;
|
2342
|
-
prev = stat instanceof AST_SimpleStatement ? stat : null;
|
2343
|
-
}
|
2344
|
-
statements.length = n;
|
2345
|
-
}
|
2346
|
-
|
2347
|
-
function join_object_assignments(defn, body) {
|
2348
|
-
if (!(defn instanceof AST_Definitions)) return;
|
2349
|
-
var def = defn.definitions[defn.definitions.length - 1];
|
2350
|
-
if (!(def.value instanceof AST_Object)) return;
|
2351
|
-
var exprs;
|
2352
|
-
if (body instanceof AST_Assign && !body.logical) {
|
2353
|
-
exprs = [ body ];
|
2354
|
-
} else if (body instanceof AST_Sequence) {
|
2355
|
-
exprs = body.expressions.slice();
|
2356
|
-
}
|
2357
|
-
if (!exprs) return;
|
2358
|
-
var trimmed = false;
|
2359
|
-
do {
|
2360
|
-
var node = exprs[0];
|
2361
|
-
if (!(node instanceof AST_Assign)) break;
|
2362
|
-
if (node.operator != "=") break;
|
2363
|
-
if (!(node.left instanceof AST_PropAccess)) break;
|
2364
|
-
var sym = node.left.expression;
|
2365
|
-
if (!(sym instanceof AST_SymbolRef)) break;
|
2366
|
-
if (def.name.name != sym.name) break;
|
2367
|
-
if (!node.right.is_constant_expression(scope)) break;
|
2368
|
-
var prop = node.left.property;
|
2369
|
-
if (prop instanceof AST_Node) {
|
2370
|
-
prop = prop.evaluate(compressor);
|
2371
|
-
}
|
2372
|
-
if (prop instanceof AST_Node) break;
|
2373
|
-
prop = "" + prop;
|
2374
|
-
var diff = compressor.option("ecma") < 2015
|
2375
|
-
&& compressor.has_directive("use strict") ? function(node) {
|
2376
|
-
return node.key != prop && (node.key && node.key.name != prop);
|
2377
|
-
} : function(node) {
|
2378
|
-
return node.key && node.key.name != prop;
|
2379
|
-
};
|
2380
|
-
if (!def.value.properties.every(diff)) break;
|
2381
|
-
var p = def.value.properties.filter(function (p) { return p.key === prop; })[0];
|
2382
|
-
if (!p) {
|
2383
|
-
def.value.properties.push(make_node(AST_ObjectKeyVal, node, {
|
2384
|
-
key: prop,
|
2385
|
-
value: node.right
|
2386
|
-
}));
|
2387
|
-
} else {
|
2388
|
-
p.value = new AST_Sequence({
|
2389
|
-
start: p.start,
|
2390
|
-
expressions: [p.value.clone(), node.right.clone()],
|
2391
|
-
end: p.end
|
2392
|
-
});
|
2393
|
-
}
|
2394
|
-
exprs.shift();
|
2395
|
-
trimmed = true;
|
2396
|
-
} while (exprs.length);
|
2397
|
-
return trimmed && exprs;
|
2398
|
-
}
|
2399
|
-
|
2400
|
-
function join_consecutive_vars(statements) {
|
2401
|
-
var defs;
|
2402
|
-
for (var i = 0, j = -1, len = statements.length; i < len; i++) {
|
2403
|
-
var stat = statements[i];
|
2404
|
-
var prev = statements[j];
|
2405
|
-
if (stat instanceof AST_Definitions) {
|
2406
|
-
if (prev && prev.TYPE == stat.TYPE) {
|
2407
|
-
prev.definitions = prev.definitions.concat(stat.definitions);
|
2408
|
-
CHANGED = true;
|
2409
|
-
} else if (defs && defs.TYPE == stat.TYPE && declarations_only(stat)) {
|
2410
|
-
defs.definitions = defs.definitions.concat(stat.definitions);
|
2411
|
-
CHANGED = true;
|
2412
|
-
} else {
|
2413
|
-
statements[++j] = stat;
|
2414
|
-
defs = stat;
|
2415
|
-
}
|
2416
|
-
} else if (stat instanceof AST_Exit) {
|
2417
|
-
stat.value = extract_object_assignments(stat.value);
|
2418
|
-
} else if (stat instanceof AST_For) {
|
2419
|
-
var exprs = join_object_assignments(prev, stat.init);
|
2420
|
-
if (exprs) {
|
2421
|
-
CHANGED = true;
|
2422
|
-
stat.init = exprs.length ? make_sequence(stat.init, exprs) : null;
|
2423
|
-
statements[++j] = stat;
|
2424
|
-
} else if (prev instanceof AST_Var && (!stat.init || stat.init.TYPE == prev.TYPE)) {
|
2425
|
-
if (stat.init) {
|
2426
|
-
prev.definitions = prev.definitions.concat(stat.init.definitions);
|
2427
|
-
}
|
2428
|
-
stat.init = prev;
|
2429
|
-
statements[j] = stat;
|
2430
|
-
CHANGED = true;
|
2431
|
-
} else if (defs && stat.init && defs.TYPE == stat.init.TYPE && declarations_only(stat.init)) {
|
2432
|
-
defs.definitions = defs.definitions.concat(stat.init.definitions);
|
2433
|
-
stat.init = null;
|
2434
|
-
statements[++j] = stat;
|
2435
|
-
CHANGED = true;
|
2436
|
-
} else {
|
2437
|
-
statements[++j] = stat;
|
2438
|
-
}
|
2439
|
-
} else if (stat instanceof AST_ForIn) {
|
2440
|
-
stat.object = extract_object_assignments(stat.object);
|
2441
|
-
} else if (stat instanceof AST_If) {
|
2442
|
-
stat.condition = extract_object_assignments(stat.condition);
|
2443
|
-
} else if (stat instanceof AST_SimpleStatement) {
|
2444
|
-
var exprs = join_object_assignments(prev, stat.body);
|
2445
|
-
if (exprs) {
|
2446
|
-
CHANGED = true;
|
2447
|
-
if (!exprs.length) continue;
|
2448
|
-
stat.body = make_sequence(stat.body, exprs);
|
2449
|
-
}
|
2450
|
-
statements[++j] = stat;
|
2451
|
-
} else if (stat instanceof AST_Switch) {
|
2452
|
-
stat.expression = extract_object_assignments(stat.expression);
|
2453
|
-
} else if (stat instanceof AST_With) {
|
2454
|
-
stat.expression = extract_object_assignments(stat.expression);
|
2455
|
-
} else {
|
2456
|
-
statements[++j] = stat;
|
2457
|
-
}
|
2458
|
-
}
|
2459
|
-
statements.length = j + 1;
|
2460
|
-
|
2461
|
-
function extract_object_assignments(value) {
|
2462
|
-
statements[++j] = stat;
|
2463
|
-
var exprs = join_object_assignments(prev, value);
|
2464
|
-
if (exprs) {
|
2465
|
-
CHANGED = true;
|
2466
|
-
if (exprs.length) {
|
2467
|
-
return make_sequence(value, exprs);
|
2468
|
-
} else if (value instanceof AST_Sequence) {
|
2469
|
-
return value.tail_node().left;
|
2470
|
-
} else {
|
2471
|
-
return value.left;
|
2472
|
-
}
|
2473
|
-
}
|
2474
|
-
return value;
|
2475
|
-
}
|
2476
|
-
}
|
2477
|
-
}
|
2478
|
-
|
2479
|
-
function trim_unreachable_code(compressor, stat, target) {
|
2480
|
-
walk(stat, node => {
|
2481
|
-
if (node instanceof AST_Var) {
|
2482
|
-
node.remove_initializers();
|
2483
|
-
target.push(node);
|
2484
|
-
return true;
|
2485
|
-
}
|
2486
|
-
if (
|
2487
|
-
node instanceof AST_Defun
|
2488
|
-
&& (node === stat || !compressor.has_directive("use strict"))
|
2489
|
-
) {
|
2490
|
-
target.push(node === stat ? node : make_node(AST_Var, node, {
|
2491
|
-
definitions: [
|
2492
|
-
make_node(AST_VarDef, node, {
|
2493
|
-
name: make_node(AST_SymbolVar, node.name, node.name),
|
2494
|
-
value: null
|
2495
|
-
})
|
2496
|
-
]
|
2497
|
-
}));
|
2498
|
-
return true;
|
2499
|
-
}
|
2500
|
-
if (node instanceof AST_Export || node instanceof AST_Import) {
|
2501
|
-
target.push(node);
|
2502
|
-
return true;
|
2503
|
-
}
|
2504
|
-
if (node instanceof AST_Scope) {
|
2505
|
-
return true;
|
2506
|
-
}
|
2507
|
-
});
|
2508
|
-
}
|
2509
|
-
|
2510
|
-
function get_value(key) {
|
2511
|
-
if (key instanceof AST_Constant) {
|
2512
|
-
return key.getValue();
|
2513
|
-
}
|
2514
|
-
if (key instanceof AST_UnaryPrefix
|
2515
|
-
&& key.operator == "void"
|
2516
|
-
&& key.expression instanceof AST_Constant) {
|
2517
|
-
return;
|
2518
|
-
}
|
2519
|
-
return key;
|
2520
|
-
}
|
2521
|
-
|
2522
|
-
function is_undefined(node, compressor) {
|
2523
|
-
return (
|
2524
|
-
has_flag(node, UNDEFINED)
|
2525
|
-
|| node instanceof AST_Undefined
|
2526
|
-
|| node instanceof AST_UnaryPrefix
|
2527
|
-
&& node.operator == "void"
|
2528
|
-
&& !node.expression.has_side_effects(compressor)
|
2529
|
-
);
|
2530
|
-
}
|
2531
|
-
|
2532
|
-
// may_throw_on_access()
|
2533
|
-
// returns true if this node may be null, undefined or contain `AST_Accessor`
|
2534
|
-
(function(def_may_throw_on_access) {
|
2535
|
-
AST_Node.DEFMETHOD("may_throw_on_access", function(compressor) {
|
2536
|
-
return !compressor.option("pure_getters")
|
2537
|
-
|| this._dot_throw(compressor);
|
2538
|
-
});
|
2539
|
-
|
2540
|
-
function is_strict(compressor) {
|
2541
|
-
return /strict/.test(compressor.option("pure_getters"));
|
2542
|
-
}
|
2543
|
-
|
2544
|
-
def_may_throw_on_access(AST_Node, is_strict);
|
2545
|
-
def_may_throw_on_access(AST_Null, return_true);
|
2546
|
-
def_may_throw_on_access(AST_Undefined, return_true);
|
2547
|
-
def_may_throw_on_access(AST_Constant, return_false);
|
2548
|
-
def_may_throw_on_access(AST_Array, return_false);
|
2549
|
-
def_may_throw_on_access(AST_Object, function(compressor) {
|
2550
|
-
if (!is_strict(compressor)) return false;
|
2551
|
-
for (var i = this.properties.length; --i >=0;)
|
2552
|
-
if (this.properties[i]._dot_throw(compressor)) return true;
|
2553
|
-
return false;
|
2554
|
-
});
|
2555
|
-
// Do not be as strict with classes as we are with objects.
|
2556
|
-
// Hopefully the community is not going to abuse static getters and setters.
|
2557
|
-
// https://github.com/terser/terser/issues/724#issuecomment-643655656
|
2558
|
-
def_may_throw_on_access(AST_Class, return_false);
|
2559
|
-
def_may_throw_on_access(AST_ObjectProperty, return_false);
|
2560
|
-
def_may_throw_on_access(AST_ObjectGetter, return_true);
|
2561
|
-
def_may_throw_on_access(AST_Expansion, function(compressor) {
|
2562
|
-
return this.expression._dot_throw(compressor);
|
2563
|
-
});
|
2564
|
-
def_may_throw_on_access(AST_Function, return_false);
|
2565
|
-
def_may_throw_on_access(AST_Arrow, return_false);
|
2566
|
-
def_may_throw_on_access(AST_UnaryPostfix, return_false);
|
2567
|
-
def_may_throw_on_access(AST_UnaryPrefix, function() {
|
2568
|
-
return this.operator == "void";
|
2569
|
-
});
|
2570
|
-
def_may_throw_on_access(AST_Binary, function(compressor) {
|
2571
|
-
return (this.operator == "&&" || this.operator == "||" || this.operator == "??")
|
2572
|
-
&& (this.left._dot_throw(compressor) || this.right._dot_throw(compressor));
|
2573
|
-
});
|
2574
|
-
def_may_throw_on_access(AST_Assign, function(compressor) {
|
2575
|
-
if (this.logical) return true;
|
2576
|
-
|
2577
|
-
return this.operator == "="
|
2578
|
-
&& this.right._dot_throw(compressor);
|
2579
|
-
});
|
2580
|
-
def_may_throw_on_access(AST_Conditional, function(compressor) {
|
2581
|
-
return this.consequent._dot_throw(compressor)
|
2582
|
-
|| this.alternative._dot_throw(compressor);
|
2583
|
-
});
|
2584
|
-
def_may_throw_on_access(AST_Dot, function(compressor) {
|
2585
|
-
if (!is_strict(compressor)) return false;
|
2586
|
-
|
2587
|
-
if (this.property == "prototype") {
|
2588
|
-
return !(
|
2589
|
-
this.expression instanceof AST_Function
|
2590
|
-
|| this.expression instanceof AST_Class
|
2591
|
-
);
|
2592
|
-
}
|
2593
|
-
return true;
|
2594
|
-
});
|
2595
|
-
def_may_throw_on_access(AST_Chain, function(compressor) {
|
2596
|
-
return this.expression._dot_throw(compressor);
|
2597
|
-
});
|
2598
|
-
def_may_throw_on_access(AST_Sequence, function(compressor) {
|
2599
|
-
return this.tail_node()._dot_throw(compressor);
|
2600
|
-
});
|
2601
|
-
def_may_throw_on_access(AST_SymbolRef, function(compressor) {
|
2602
|
-
if (this.name === "arguments") return false;
|
2603
|
-
if (has_flag(this, UNDEFINED)) return true;
|
2604
|
-
if (!is_strict(compressor)) return false;
|
2605
|
-
if (is_undeclared_ref(this) && this.is_declared(compressor)) return false;
|
2606
|
-
if (this.is_immutable()) return false;
|
2607
|
-
var fixed = this.fixed_value();
|
2608
|
-
return !fixed || fixed._dot_throw(compressor);
|
2609
|
-
});
|
2610
|
-
})(function(node, func) {
|
2611
|
-
node.DEFMETHOD("_dot_throw", func);
|
2612
|
-
});
|
2613
|
-
|
2614
|
-
/* -----[ boolean/negation helpers ]----- */
|
2615
|
-
|
2616
|
-
// methods to determine whether an expression has a boolean result type
|
2617
|
-
(function(def_is_boolean) {
|
2618
|
-
const unary_bool = makePredicate("! delete");
|
2619
|
-
const binary_bool = makePredicate("in instanceof == != === !== < <= >= >");
|
2620
|
-
def_is_boolean(AST_Node, return_false);
|
2621
|
-
def_is_boolean(AST_UnaryPrefix, function() {
|
2622
|
-
return unary_bool.has(this.operator);
|
2623
|
-
});
|
2624
|
-
def_is_boolean(AST_Binary, function() {
|
2625
|
-
return binary_bool.has(this.operator)
|
2626
|
-
|| lazy_op.has(this.operator)
|
2627
|
-
&& this.left.is_boolean()
|
2628
|
-
&& this.right.is_boolean();
|
2629
|
-
});
|
2630
|
-
def_is_boolean(AST_Conditional, function() {
|
2631
|
-
return this.consequent.is_boolean() && this.alternative.is_boolean();
|
2632
|
-
});
|
2633
|
-
def_is_boolean(AST_Assign, function() {
|
2634
|
-
return this.operator == "=" && this.right.is_boolean();
|
2635
|
-
});
|
2636
|
-
def_is_boolean(AST_Sequence, function() {
|
2637
|
-
return this.tail_node().is_boolean();
|
2638
|
-
});
|
2639
|
-
def_is_boolean(AST_True, return_true);
|
2640
|
-
def_is_boolean(AST_False, return_true);
|
2641
|
-
})(function(node, func) {
|
2642
|
-
node.DEFMETHOD("is_boolean", func);
|
2643
|
-
});
|
2644
|
-
|
2645
|
-
// methods to determine if an expression has a numeric result type
|
2646
|
-
(function(def_is_number) {
|
2647
|
-
def_is_number(AST_Node, return_false);
|
2648
|
-
def_is_number(AST_Number, return_true);
|
2649
|
-
var unary = makePredicate("+ - ~ ++ --");
|
2650
|
-
def_is_number(AST_Unary, function() {
|
2651
|
-
return unary.has(this.operator);
|
2652
|
-
});
|
2653
|
-
var binary = makePredicate("- * / % & | ^ << >> >>>");
|
2654
|
-
def_is_number(AST_Binary, function(compressor) {
|
2655
|
-
return binary.has(this.operator) || this.operator == "+"
|
2656
|
-
&& this.left.is_number(compressor)
|
2657
|
-
&& this.right.is_number(compressor);
|
2658
|
-
});
|
2659
|
-
def_is_number(AST_Assign, function(compressor) {
|
2660
|
-
return binary.has(this.operator.slice(0, -1))
|
2661
|
-
|| this.operator == "=" && this.right.is_number(compressor);
|
2662
|
-
});
|
2663
|
-
def_is_number(AST_Sequence, function(compressor) {
|
2664
|
-
return this.tail_node().is_number(compressor);
|
2665
|
-
});
|
2666
|
-
def_is_number(AST_Conditional, function(compressor) {
|
2667
|
-
return this.consequent.is_number(compressor) && this.alternative.is_number(compressor);
|
2668
|
-
});
|
2669
|
-
})(function(node, func) {
|
2670
|
-
node.DEFMETHOD("is_number", func);
|
2671
|
-
});
|
2672
|
-
|
2673
|
-
// methods to determine if an expression has a string result type
|
2674
|
-
(function(def_is_string) {
|
2675
|
-
def_is_string(AST_Node, return_false);
|
2676
|
-
def_is_string(AST_String, return_true);
|
2677
|
-
def_is_string(AST_TemplateString, return_true);
|
2678
|
-
def_is_string(AST_UnaryPrefix, function() {
|
2679
|
-
return this.operator == "typeof";
|
2680
|
-
});
|
2681
|
-
def_is_string(AST_Binary, function(compressor) {
|
2682
|
-
return this.operator == "+" &&
|
2683
|
-
(this.left.is_string(compressor) || this.right.is_string(compressor));
|
2684
|
-
});
|
2685
|
-
def_is_string(AST_Assign, function(compressor) {
|
2686
|
-
return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
|
2687
|
-
});
|
2688
|
-
def_is_string(AST_Sequence, function(compressor) {
|
2689
|
-
return this.tail_node().is_string(compressor);
|
2690
|
-
});
|
2691
|
-
def_is_string(AST_Conditional, function(compressor) {
|
2692
|
-
return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
|
2693
|
-
});
|
2694
|
-
})(function(node, func) {
|
2695
|
-
node.DEFMETHOD("is_string", func);
|
2696
|
-
});
|
2697
|
-
|
2698
|
-
var lazy_op = makePredicate("&& || ??");
|
2699
|
-
var unary_side_effects = makePredicate("delete ++ --");
|
2700
|
-
|
2701
|
-
function is_lhs(node, parent) {
|
2702
|
-
if (parent instanceof AST_Unary && unary_side_effects.has(parent.operator)) return parent.expression;
|
2703
|
-
if (parent instanceof AST_Assign && parent.left === node) return node;
|
2704
|
-
}
|
2705
|
-
|
2706
|
-
(function(def_find_defs) {
|
2707
|
-
function to_node(value, orig) {
|
2708
|
-
if (value instanceof AST_Node) return make_node(value.CTOR, orig, value);
|
2709
|
-
if (Array.isArray(value)) return make_node(AST_Array, orig, {
|
2710
|
-
elements: value.map(function(value) {
|
2711
|
-
return to_node(value, orig);
|
2712
|
-
})
|
2713
|
-
});
|
2714
|
-
if (value && typeof value == "object") {
|
2715
|
-
var props = [];
|
2716
|
-
for (var key in value) if (HOP(value, key)) {
|
2717
|
-
props.push(make_node(AST_ObjectKeyVal, orig, {
|
2718
|
-
key: key,
|
2719
|
-
value: to_node(value[key], orig)
|
2720
|
-
}));
|
2721
|
-
}
|
2722
|
-
return make_node(AST_Object, orig, {
|
2723
|
-
properties: props
|
2724
|
-
});
|
2725
|
-
}
|
2726
|
-
return make_node_from_constant(value, orig);
|
2727
|
-
}
|
2728
|
-
|
2729
|
-
AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) {
|
2730
|
-
if (!compressor.option("global_defs")) return this;
|
2731
|
-
this.figure_out_scope({ ie8: compressor.option("ie8") });
|
2732
|
-
return this.transform(new TreeTransformer(function(node) {
|
2733
|
-
var def = node._find_defs(compressor, "");
|
2734
|
-
if (!def) return;
|
2735
|
-
var level = 0, child = node, parent;
|
2736
|
-
while (parent = this.parent(level++)) {
|
2737
|
-
if (!(parent instanceof AST_PropAccess)) break;
|
2738
|
-
if (parent.expression !== child) break;
|
2739
|
-
child = parent;
|
2740
|
-
}
|
2741
|
-
if (is_lhs(child, parent)) {
|
2742
|
-
return;
|
2743
|
-
}
|
2744
|
-
return def;
|
2745
|
-
}));
|
2746
|
-
});
|
2747
|
-
def_find_defs(AST_Node, noop);
|
2748
|
-
def_find_defs(AST_Chain, function(compressor, suffix) {
|
2749
|
-
return this.expression._find_defs(compressor, suffix);
|
2750
|
-
});
|
2751
|
-
def_find_defs(AST_Dot, function(compressor, suffix) {
|
2752
|
-
return this.expression._find_defs(compressor, "." + this.property + suffix);
|
2753
|
-
});
|
2754
|
-
def_find_defs(AST_SymbolDeclaration, function() {
|
2755
|
-
if (!this.global()) return;
|
2756
|
-
});
|
2757
|
-
def_find_defs(AST_SymbolRef, function(compressor, suffix) {
|
2758
|
-
if (!this.global()) return;
|
2759
|
-
var defines = compressor.option("global_defs");
|
2760
|
-
var name = this.name + suffix;
|
2761
|
-
if (HOP(defines, name)) return to_node(defines[name], this);
|
2762
|
-
});
|
2763
|
-
})(function(node, func) {
|
2764
|
-
node.DEFMETHOD("_find_defs", func);
|
2765
|
-
});
|
2766
|
-
|
2767
|
-
function best_of_expression(ast1, ast2) {
|
2768
|
-
return ast1.size() > ast2.size() ? ast2 : ast1;
|
2769
|
-
}
|
2770
|
-
|
2771
|
-
function best_of_statement(ast1, ast2) {
|
2772
|
-
return best_of_expression(
|
2773
|
-
make_node(AST_SimpleStatement, ast1, {
|
2774
|
-
body: ast1
|
2775
|
-
}),
|
2776
|
-
make_node(AST_SimpleStatement, ast2, {
|
2777
|
-
body: ast2
|
2778
|
-
})
|
2779
|
-
).body;
|
2780
|
-
}
|
2781
|
-
|
2782
|
-
function best_of(compressor, ast1, ast2) {
|
2783
|
-
return (first_in_statement(compressor) ? best_of_statement : best_of_expression)(ast1, ast2);
|
2784
|
-
}
|
2785
|
-
|
2786
|
-
function convert_to_predicate(obj) {
|
2787
|
-
const out = new Map();
|
2788
|
-
for (var key of Object.keys(obj)) {
|
2789
|
-
out.set(key, makePredicate(obj[key]));
|
2790
|
-
}
|
2791
|
-
return out;
|
2792
|
-
}
|
2793
|
-
|
2794
|
-
var object_fns = [
|
2795
|
-
"constructor",
|
2796
|
-
"toString",
|
2797
|
-
"valueOf",
|
2798
|
-
];
|
2799
|
-
var native_fns = convert_to_predicate({
|
2800
|
-
Array: [
|
2801
|
-
"indexOf",
|
2802
|
-
"join",
|
2803
|
-
"lastIndexOf",
|
2804
|
-
"slice",
|
2805
|
-
].concat(object_fns),
|
2806
|
-
Boolean: object_fns,
|
2807
|
-
Function: object_fns,
|
2808
|
-
Number: [
|
2809
|
-
"toExponential",
|
2810
|
-
"toFixed",
|
2811
|
-
"toPrecision",
|
2812
|
-
].concat(object_fns),
|
2813
|
-
Object: object_fns,
|
2814
|
-
RegExp: [
|
2815
|
-
"test",
|
2816
|
-
].concat(object_fns),
|
2817
|
-
String: [
|
2818
|
-
"charAt",
|
2819
|
-
"charCodeAt",
|
2820
|
-
"concat",
|
2821
|
-
"indexOf",
|
2822
|
-
"italics",
|
2823
|
-
"lastIndexOf",
|
2824
|
-
"match",
|
2825
|
-
"replace",
|
2826
|
-
"search",
|
2827
|
-
"slice",
|
2828
|
-
"split",
|
2829
|
-
"substr",
|
2830
|
-
"substring",
|
2831
|
-
"toLowerCase",
|
2832
|
-
"toUpperCase",
|
2833
|
-
"trim",
|
2834
|
-
].concat(object_fns),
|
2835
|
-
});
|
2836
|
-
var static_fns = convert_to_predicate({
|
2837
|
-
Array: [
|
2838
|
-
"isArray",
|
2839
|
-
],
|
2840
|
-
Math: [
|
2841
|
-
"abs",
|
2842
|
-
"acos",
|
2843
|
-
"asin",
|
2844
|
-
"atan",
|
2845
|
-
"ceil",
|
2846
|
-
"cos",
|
2847
|
-
"exp",
|
2848
|
-
"floor",
|
2849
|
-
"log",
|
2850
|
-
"round",
|
2851
|
-
"sin",
|
2852
|
-
"sqrt",
|
2853
|
-
"tan",
|
2854
|
-
"atan2",
|
2855
|
-
"pow",
|
2856
|
-
"max",
|
2857
|
-
"min",
|
2858
|
-
],
|
2859
|
-
Number: [
|
2860
|
-
"isFinite",
|
2861
|
-
"isNaN",
|
2862
|
-
],
|
2863
|
-
Object: [
|
2864
|
-
"create",
|
2865
|
-
"getOwnPropertyDescriptor",
|
2866
|
-
"getOwnPropertyNames",
|
2867
|
-
"getPrototypeOf",
|
2868
|
-
"isExtensible",
|
2869
|
-
"isFrozen",
|
2870
|
-
"isSealed",
|
2871
|
-
"keys",
|
2872
|
-
],
|
2873
|
-
String: [
|
2874
|
-
"fromCharCode",
|
2875
|
-
],
|
2876
|
-
});
|
2877
|
-
|
2878
|
-
// methods to evaluate a constant expression
|
2879
|
-
(function(def_eval) {
|
2880
|
-
// If the node has been successfully reduced to a constant,
|
2881
|
-
// then its value is returned; otherwise the element itself
|
2882
|
-
// is returned.
|
2883
|
-
// They can be distinguished as constant value is never a
|
2884
|
-
// descendant of AST_Node.
|
2885
|
-
AST_Node.DEFMETHOD("evaluate", function(compressor) {
|
2886
|
-
if (!compressor.option("evaluate")) return this;
|
2887
|
-
var val = this._eval(compressor, 1);
|
2888
|
-
if (!val || val instanceof RegExp) return val;
|
2889
|
-
if (typeof val == "function" || typeof val == "object") return this;
|
2890
|
-
return val;
|
2891
|
-
});
|
2892
|
-
var unaryPrefix = makePredicate("! ~ - + void");
|
2893
|
-
AST_Node.DEFMETHOD("is_constant", function() {
|
2894
|
-
// Accomodate when compress option evaluate=false
|
2895
|
-
// as well as the common constant expressions !0 and -1
|
2896
|
-
if (this instanceof AST_Constant) {
|
2897
|
-
return !(this instanceof AST_RegExp);
|
2898
|
-
} else {
|
2899
|
-
return this instanceof AST_UnaryPrefix
|
2900
|
-
&& this.expression instanceof AST_Constant
|
2901
|
-
&& unaryPrefix.has(this.operator);
|
2902
|
-
}
|
2903
|
-
});
|
2904
|
-
def_eval(AST_Statement, function() {
|
2905
|
-
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
|
2906
|
-
});
|
2907
|
-
def_eval(AST_Lambda, return_this);
|
2908
|
-
def_eval(AST_Class, return_this);
|
2909
|
-
def_eval(AST_Node, return_this);
|
2910
|
-
def_eval(AST_Constant, function() {
|
2911
|
-
return this.getValue();
|
2912
|
-
});
|
2913
|
-
def_eval(AST_BigInt, return_this);
|
2914
|
-
def_eval(AST_RegExp, function(compressor) {
|
2915
|
-
let evaluated = compressor.evaluated_regexps.get(this);
|
2916
|
-
if (evaluated === undefined) {
|
2917
|
-
try {
|
2918
|
-
evaluated = (0, eval)(this.print_to_string());
|
2919
|
-
} catch (e) {
|
2920
|
-
evaluated = null;
|
2921
|
-
}
|
2922
|
-
compressor.evaluated_regexps.set(this, evaluated);
|
2923
|
-
}
|
2924
|
-
return evaluated || this;
|
2925
|
-
});
|
2926
|
-
def_eval(AST_TemplateString, function() {
|
2927
|
-
if (this.segments.length !== 1) return this;
|
2928
|
-
return this.segments[0].value;
|
2929
|
-
});
|
2930
|
-
def_eval(AST_Function, function(compressor) {
|
2931
|
-
if (compressor.option("unsafe")) {
|
2932
|
-
var fn = function() {};
|
2933
|
-
fn.node = this;
|
2934
|
-
fn.toString = () => this.print_to_string();
|
2935
|
-
return fn;
|
2936
|
-
}
|
2937
|
-
return this;
|
2938
|
-
});
|
2939
|
-
def_eval(AST_Array, function(compressor, depth) {
|
2940
|
-
if (compressor.option("unsafe")) {
|
2941
|
-
var elements = [];
|
2942
|
-
for (var i = 0, len = this.elements.length; i < len; i++) {
|
2943
|
-
var element = this.elements[i];
|
2944
|
-
var value = element._eval(compressor, depth);
|
2945
|
-
if (element === value) return this;
|
2946
|
-
elements.push(value);
|
2947
|
-
}
|
2948
|
-
return elements;
|
2949
|
-
}
|
2950
|
-
return this;
|
2951
|
-
});
|
2952
|
-
def_eval(AST_Object, function(compressor, depth) {
|
2953
|
-
if (compressor.option("unsafe")) {
|
2954
|
-
var val = {};
|
2955
|
-
for (var i = 0, len = this.properties.length; i < len; i++) {
|
2956
|
-
var prop = this.properties[i];
|
2957
|
-
if (prop instanceof AST_Expansion) return this;
|
2958
|
-
var key = prop.key;
|
2959
|
-
if (key instanceof AST_Symbol) {
|
2960
|
-
key = key.name;
|
2961
|
-
} else if (key instanceof AST_Node) {
|
2962
|
-
key = key._eval(compressor, depth);
|
2963
|
-
if (key === prop.key) return this;
|
2964
|
-
}
|
2965
|
-
if (typeof Object.prototype[key] === "function") {
|
2966
|
-
return this;
|
2967
|
-
}
|
2968
|
-
if (prop.value instanceof AST_Function) continue;
|
2969
|
-
val[key] = prop.value._eval(compressor, depth);
|
2970
|
-
if (val[key] === prop.value) return this;
|
2971
|
-
}
|
2972
|
-
return val;
|
2973
|
-
}
|
2974
|
-
return this;
|
2975
|
-
});
|
2976
|
-
var non_converting_unary = makePredicate("! typeof void");
|
2977
|
-
def_eval(AST_UnaryPrefix, function(compressor, depth) {
|
2978
|
-
var e = this.expression;
|
2979
|
-
// Function would be evaluated to an array and so typeof would
|
2980
|
-
// incorrectly return 'object'. Hence making is a special case.
|
2981
|
-
if (compressor.option("typeofs")
|
2982
|
-
&& this.operator == "typeof"
|
2983
|
-
&& (e instanceof AST_Lambda
|
2984
|
-
|| e instanceof AST_SymbolRef
|
2985
|
-
&& e.fixed_value() instanceof AST_Lambda)) {
|
2986
|
-
return typeof function() {};
|
2987
|
-
}
|
2988
|
-
if (!non_converting_unary.has(this.operator)) depth++;
|
2989
|
-
e = e._eval(compressor, depth);
|
2990
|
-
if (e === this.expression) return this;
|
2991
|
-
switch (this.operator) {
|
2992
|
-
case "!": return !e;
|
2993
|
-
case "typeof":
|
2994
|
-
// typeof <RegExp> returns "object" or "function" on different platforms
|
2995
|
-
// so cannot evaluate reliably
|
2996
|
-
if (e instanceof RegExp) return this;
|
2997
|
-
return typeof e;
|
2998
|
-
case "void": return void e;
|
2999
|
-
case "~": return ~e;
|
3000
|
-
case "-": return -e;
|
3001
|
-
case "+": return +e;
|
3002
|
-
}
|
3003
|
-
return this;
|
3004
|
-
});
|
3005
|
-
var non_converting_binary = makePredicate("&& || ?? === !==");
|
3006
|
-
const identity_comparison = makePredicate("== != === !==");
|
3007
|
-
const has_identity = value =>
|
3008
|
-
typeof value === "object"
|
3009
|
-
|| typeof value === "function"
|
3010
|
-
|| typeof value === "symbol";
|
3011
|
-
|
3012
|
-
def_eval(AST_Binary, function(compressor, depth) {
|
3013
|
-
if (!non_converting_binary.has(this.operator)) depth++;
|
3014
|
-
|
3015
|
-
var left = this.left._eval(compressor, depth);
|
3016
|
-
if (left === this.left) return this;
|
3017
|
-
var right = this.right._eval(compressor, depth);
|
3018
|
-
if (right === this.right) return this;
|
3019
|
-
var result;
|
3020
|
-
|
3021
|
-
if (
|
3022
|
-
left != null
|
3023
|
-
&& right != null
|
3024
|
-
&& identity_comparison.has(this.operator)
|
3025
|
-
&& has_identity(left)
|
3026
|
-
&& has_identity(right)
|
3027
|
-
&& typeof left === typeof right
|
3028
|
-
) {
|
3029
|
-
// Do not compare by reference
|
3030
|
-
return this;
|
3031
|
-
}
|
3032
|
-
|
3033
|
-
switch (this.operator) {
|
3034
|
-
case "&&" : result = left && right; break;
|
3035
|
-
case "||" : result = left || right; break;
|
3036
|
-
case "??" : result = left != null ? left : right; break;
|
3037
|
-
case "|" : result = left | right; break;
|
3038
|
-
case "&" : result = left & right; break;
|
3039
|
-
case "^" : result = left ^ right; break;
|
3040
|
-
case "+" : result = left + right; break;
|
3041
|
-
case "*" : result = left * right; break;
|
3042
|
-
case "**" : result = Math.pow(left, right); break;
|
3043
|
-
case "/" : result = left / right; break;
|
3044
|
-
case "%" : result = left % right; break;
|
3045
|
-
case "-" : result = left - right; break;
|
3046
|
-
case "<<" : result = left << right; break;
|
3047
|
-
case ">>" : result = left >> right; break;
|
3048
|
-
case ">>>" : result = left >>> right; break;
|
3049
|
-
case "==" : result = left == right; break;
|
3050
|
-
case "===" : result = left === right; break;
|
3051
|
-
case "!=" : result = left != right; break;
|
3052
|
-
case "!==" : result = left !== right; break;
|
3053
|
-
case "<" : result = left < right; break;
|
3054
|
-
case "<=" : result = left <= right; break;
|
3055
|
-
case ">" : result = left > right; break;
|
3056
|
-
case ">=" : result = left >= right; break;
|
3057
|
-
default:
|
3058
|
-
return this;
|
3059
|
-
}
|
3060
|
-
if (isNaN(result) && compressor.find_parent(AST_With)) {
|
3061
|
-
// leave original expression as is
|
3062
|
-
return this;
|
3063
|
-
}
|
3064
|
-
return result;
|
3065
|
-
});
|
3066
|
-
def_eval(AST_Conditional, function(compressor, depth) {
|
3067
|
-
var condition = this.condition._eval(compressor, depth);
|
3068
|
-
if (condition === this.condition) return this;
|
3069
|
-
var node = condition ? this.consequent : this.alternative;
|
3070
|
-
var value = node._eval(compressor, depth);
|
3071
|
-
return value === node ? this : value;
|
3072
|
-
});
|
3073
|
-
|
3074
|
-
// Set of AST_SymbolRef which are currently being evaluated.
|
3075
|
-
// Avoids infinite recursion of ._eval()
|
3076
|
-
const reentrant_ref_eval = new Set();
|
3077
|
-
def_eval(AST_SymbolRef, function(compressor, depth) {
|
3078
|
-
if (reentrant_ref_eval.has(this)) return this;
|
3079
|
-
|
3080
|
-
var fixed = this.fixed_value();
|
3081
|
-
if (!fixed) return this;
|
3082
|
-
|
3083
|
-
reentrant_ref_eval.add(this);
|
3084
|
-
const value = fixed._eval(compressor, depth);
|
3085
|
-
reentrant_ref_eval.delete(this);
|
3086
|
-
|
3087
|
-
if (value === fixed) return this;
|
3088
|
-
|
3089
|
-
if (value && typeof value == "object") {
|
3090
|
-
var escaped = this.definition().escaped;
|
3091
|
-
if (escaped && depth > escaped) return this;
|
3092
|
-
}
|
3093
|
-
return value;
|
3094
|
-
});
|
3095
|
-
var global_objs = { Array, Math, Number, Object, String };
|
3096
|
-
var static_values = convert_to_predicate({
|
3097
|
-
Math: [
|
3098
|
-
"E",
|
3099
|
-
"LN10",
|
3100
|
-
"LN2",
|
3101
|
-
"LOG2E",
|
3102
|
-
"LOG10E",
|
3103
|
-
"PI",
|
3104
|
-
"SQRT1_2",
|
3105
|
-
"SQRT2",
|
3106
|
-
],
|
3107
|
-
Number: [
|
3108
|
-
"MAX_VALUE",
|
3109
|
-
"MIN_VALUE",
|
3110
|
-
"NaN",
|
3111
|
-
"NEGATIVE_INFINITY",
|
3112
|
-
"POSITIVE_INFINITY",
|
3113
|
-
],
|
3114
|
-
});
|
3115
|
-
const regexp_flags = new Set([
|
3116
|
-
"dotAll",
|
3117
|
-
"global",
|
3118
|
-
"ignoreCase",
|
3119
|
-
"multiline",
|
3120
|
-
"sticky",
|
3121
|
-
"unicode",
|
3122
|
-
]);
|
3123
|
-
def_eval(AST_PropAccess, function(compressor, depth) {
|
3124
|
-
if (this.optional) {
|
3125
|
-
const obj = this.expression._eval(compressor, depth);
|
3126
|
-
if (obj == null) return undefined;
|
3127
|
-
}
|
3128
|
-
if (compressor.option("unsafe")) {
|
3129
|
-
var key = this.property;
|
3130
|
-
if (key instanceof AST_Node) {
|
3131
|
-
key = key._eval(compressor, depth);
|
3132
|
-
if (key === this.property) return this;
|
3133
|
-
}
|
3134
|
-
var exp = this.expression;
|
3135
|
-
var val;
|
3136
|
-
if (is_undeclared_ref(exp)) {
|
3137
|
-
|
3138
|
-
var aa;
|
3139
|
-
var first_arg = exp.name === "hasOwnProperty"
|
3140
|
-
&& key === "call"
|
3141
|
-
&& (aa = compressor.parent() && compressor.parent().args)
|
3142
|
-
&& (aa && aa[0]
|
3143
|
-
&& aa[0].evaluate(compressor));
|
3144
|
-
|
3145
|
-
first_arg = first_arg instanceof AST_Dot ? first_arg.expression : first_arg;
|
3146
|
-
|
3147
|
-
if (first_arg == null || first_arg.thedef && first_arg.thedef.undeclared) {
|
3148
|
-
return this.clone();
|
3149
|
-
}
|
3150
|
-
var static_value = static_values.get(exp.name);
|
3151
|
-
if (!static_value || !static_value.has(key)) return this;
|
3152
|
-
val = global_objs[exp.name];
|
3153
|
-
} else {
|
3154
|
-
val = exp._eval(compressor, depth + 1);
|
3155
|
-
if (val instanceof RegExp) {
|
3156
|
-
if (key == "source") {
|
3157
|
-
return regexp_source_fix(val.source);
|
3158
|
-
} else if (key == "flags" || regexp_flags.has(key)) {
|
3159
|
-
return val[key];
|
3160
|
-
}
|
3161
|
-
}
|
3162
|
-
if (!val || val === exp || !HOP(val, key)) return this;
|
3163
|
-
if (typeof val == "function") switch (key) {
|
3164
|
-
case "name":
|
3165
|
-
return val.node.name ? val.node.name.name : "";
|
3166
|
-
case "length":
|
3167
|
-
return val.node.length_property();
|
3168
|
-
default:
|
3169
|
-
return this;
|
3170
|
-
}
|
3171
|
-
}
|
3172
|
-
return val[key];
|
3173
|
-
}
|
3174
|
-
return this;
|
3175
|
-
});
|
3176
|
-
def_eval(AST_Chain, function(compressor, depth) {
|
3177
|
-
const evaluated = this.expression._eval(compressor, depth);
|
3178
|
-
return evaluated === this.expression ? this : evaluated;
|
3179
|
-
});
|
3180
|
-
def_eval(AST_Call, function(compressor, depth) {
|
3181
|
-
var exp = this.expression;
|
3182
|
-
if (this.optional) {
|
3183
|
-
const callee = this.expression._eval(compressor, depth);
|
3184
|
-
if (callee == null) return undefined;
|
3185
|
-
}
|
3186
|
-
if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {
|
3187
|
-
var key = exp.property;
|
3188
|
-
if (key instanceof AST_Node) {
|
3189
|
-
key = key._eval(compressor, depth);
|
3190
|
-
if (key === exp.property) return this;
|
3191
|
-
}
|
3192
|
-
var val;
|
3193
|
-
var e = exp.expression;
|
3194
|
-
if (is_undeclared_ref(e)) {
|
3195
|
-
var first_arg =
|
3196
|
-
e.name === "hasOwnProperty" &&
|
3197
|
-
key === "call" &&
|
3198
|
-
(this.args[0] && this.args[0].evaluate(compressor));
|
3199
|
-
|
3200
|
-
first_arg = first_arg instanceof AST_Dot ? first_arg.expression : first_arg;
|
3201
|
-
|
3202
|
-
if ((first_arg == null || first_arg.thedef && first_arg.thedef.undeclared)) {
|
3203
|
-
return this.clone();
|
3204
|
-
}
|
3205
|
-
var static_fn = static_fns.get(e.name);
|
3206
|
-
if (!static_fn || !static_fn.has(key)) return this;
|
3207
|
-
val = global_objs[e.name];
|
3208
|
-
} else {
|
3209
|
-
val = e._eval(compressor, depth + 1);
|
3210
|
-
if (val === e || !val) return this;
|
3211
|
-
var native_fn = native_fns.get(val.constructor.name);
|
3212
|
-
if (!native_fn || !native_fn.has(key)) return this;
|
3213
|
-
}
|
3214
|
-
var args = [];
|
3215
|
-
for (var i = 0, len = this.args.length; i < len; i++) {
|
3216
|
-
var arg = this.args[i];
|
3217
|
-
var value = arg._eval(compressor, depth);
|
3218
|
-
if (arg === value) return this;
|
3219
|
-
if (arg instanceof AST_Lambda) return this;
|
3220
|
-
args.push(value);
|
3221
|
-
}
|
3222
|
-
try {
|
3223
|
-
return val[key].apply(val, args);
|
3224
|
-
} catch (ex) {
|
3225
|
-
// We don't really care
|
3226
|
-
}
|
3227
|
-
}
|
3228
|
-
return this;
|
3229
|
-
});
|
3230
|
-
def_eval(AST_New, return_this);
|
3231
|
-
})(function(node, func) {
|
3232
|
-
node.DEFMETHOD("_eval", func);
|
3233
|
-
});
|
3234
|
-
|
3235
|
-
// method to negate an expression
|
3236
|
-
(function(def_negate) {
|
3237
|
-
function basic_negation(exp) {
|
3238
|
-
return make_node(AST_UnaryPrefix, exp, {
|
3239
|
-
operator: "!",
|
3240
|
-
expression: exp
|
3241
|
-
});
|
3242
|
-
}
|
3243
|
-
function best(orig, alt, first_in_statement) {
|
3244
|
-
var negated = basic_negation(orig);
|
3245
|
-
if (first_in_statement) {
|
3246
|
-
var stat = make_node(AST_SimpleStatement, alt, {
|
3247
|
-
body: alt
|
3248
|
-
});
|
3249
|
-
return best_of_expression(negated, stat) === stat ? alt : negated;
|
3250
|
-
}
|
3251
|
-
return best_of_expression(negated, alt);
|
3252
|
-
}
|
3253
|
-
def_negate(AST_Node, function() {
|
3254
|
-
return basic_negation(this);
|
3255
|
-
});
|
3256
|
-
def_negate(AST_Statement, function() {
|
3257
|
-
throw new Error("Cannot negate a statement");
|
3258
|
-
});
|
3259
|
-
def_negate(AST_Function, function() {
|
3260
|
-
return basic_negation(this);
|
3261
|
-
});
|
3262
|
-
def_negate(AST_Arrow, function() {
|
3263
|
-
return basic_negation(this);
|
3264
|
-
});
|
3265
|
-
def_negate(AST_UnaryPrefix, function() {
|
3266
|
-
if (this.operator == "!")
|
3267
|
-
return this.expression;
|
3268
|
-
return basic_negation(this);
|
3269
|
-
});
|
3270
|
-
def_negate(AST_Sequence, function(compressor) {
|
3271
|
-
var expressions = this.expressions.slice();
|
3272
|
-
expressions.push(expressions.pop().negate(compressor));
|
3273
|
-
return make_sequence(this, expressions);
|
3274
|
-
});
|
3275
|
-
def_negate(AST_Conditional, function(compressor, first_in_statement) {
|
3276
|
-
var self = this.clone();
|
3277
|
-
self.consequent = self.consequent.negate(compressor);
|
3278
|
-
self.alternative = self.alternative.negate(compressor);
|
3279
|
-
return best(this, self, first_in_statement);
|
3280
|
-
});
|
3281
|
-
def_negate(AST_Binary, function(compressor, first_in_statement) {
|
3282
|
-
var self = this.clone(), op = this.operator;
|
3283
|
-
if (compressor.option("unsafe_comps")) {
|
3284
|
-
switch (op) {
|
3285
|
-
case "<=" : self.operator = ">" ; return self;
|
3286
|
-
case "<" : self.operator = ">=" ; return self;
|
3287
|
-
case ">=" : self.operator = "<" ; return self;
|
3288
|
-
case ">" : self.operator = "<=" ; return self;
|
3289
|
-
}
|
3290
|
-
}
|
3291
|
-
switch (op) {
|
3292
|
-
case "==" : self.operator = "!="; return self;
|
3293
|
-
case "!=" : self.operator = "=="; return self;
|
3294
|
-
case "===": self.operator = "!=="; return self;
|
3295
|
-
case "!==": self.operator = "==="; return self;
|
3296
|
-
case "&&":
|
3297
|
-
self.operator = "||";
|
3298
|
-
self.left = self.left.negate(compressor, first_in_statement);
|
3299
|
-
self.right = self.right.negate(compressor);
|
3300
|
-
return best(this, self, first_in_statement);
|
3301
|
-
case "||":
|
3302
|
-
self.operator = "&&";
|
3303
|
-
self.left = self.left.negate(compressor, first_in_statement);
|
3304
|
-
self.right = self.right.negate(compressor);
|
3305
|
-
return best(this, self, first_in_statement);
|
3306
|
-
case "??":
|
3307
|
-
self.right = self.right.negate(compressor);
|
3308
|
-
return best(this, self, first_in_statement);
|
3309
|
-
}
|
3310
|
-
return basic_negation(this);
|
3311
|
-
});
|
3312
|
-
})(function(node, func) {
|
3313
|
-
node.DEFMETHOD("negate", function(compressor, first_in_statement) {
|
3314
|
-
return func.call(this, compressor, first_in_statement);
|
3315
|
-
});
|
3316
|
-
});
|
3317
|
-
|
3318
|
-
var global_pure_fns = makePredicate("Boolean decodeURI decodeURIComponent Date encodeURI encodeURIComponent Error escape EvalError isFinite isNaN Number Object parseFloat parseInt RangeError ReferenceError String SyntaxError TypeError unescape URIError");
|
3319
|
-
AST_Call.DEFMETHOD("is_callee_pure", function(compressor) {
|
3320
|
-
if (compressor.option("unsafe")) {
|
3321
|
-
var expr = this.expression;
|
3322
|
-
var first_arg = (this.args && this.args[0] && this.args[0].evaluate(compressor));
|
3323
|
-
if (
|
3324
|
-
expr.expression && expr.expression.name === "hasOwnProperty" &&
|
3325
|
-
(first_arg == null || first_arg.thedef && first_arg.thedef.undeclared)
|
3326
|
-
) {
|
3327
|
-
return false;
|
3328
|
-
}
|
3329
|
-
if (is_undeclared_ref(expr) && global_pure_fns.has(expr.name)) return true;
|
3330
|
-
let static_fn;
|
3331
|
-
if (expr instanceof AST_Dot
|
3332
|
-
&& is_undeclared_ref(expr.expression)
|
3333
|
-
&& (static_fn = static_fns.get(expr.expression.name))
|
3334
|
-
&& static_fn.has(expr.property)) {
|
3335
|
-
return true;
|
3336
|
-
}
|
3337
|
-
}
|
3338
|
-
return !!has_annotation(this, _PURE) || !compressor.pure_funcs(this);
|
3339
|
-
});
|
3340
|
-
AST_Node.DEFMETHOD("is_call_pure", return_false);
|
3341
|
-
AST_Dot.DEFMETHOD("is_call_pure", function(compressor) {
|
3342
|
-
if (!compressor.option("unsafe")) return;
|
3343
|
-
const expr = this.expression;
|
3344
|
-
let map;
|
3345
|
-
if (expr instanceof AST_Array) {
|
3346
|
-
map = native_fns.get("Array");
|
3347
|
-
} else if (expr.is_boolean()) {
|
3348
|
-
map = native_fns.get("Boolean");
|
3349
|
-
} else if (expr.is_number(compressor)) {
|
3350
|
-
map = native_fns.get("Number");
|
3351
|
-
} else if (expr instanceof AST_RegExp) {
|
3352
|
-
map = native_fns.get("RegExp");
|
3353
|
-
} else if (expr.is_string(compressor)) {
|
3354
|
-
map = native_fns.get("String");
|
3355
|
-
} else if (!this.may_throw_on_access(compressor)) {
|
3356
|
-
map = native_fns.get("Object");
|
3357
|
-
}
|
3358
|
-
return map && map.has(this.property);
|
3359
|
-
});
|
3360
|
-
|
3361
|
-
const pure_prop_access_globals = new Set([
|
3362
|
-
"Number",
|
3363
|
-
"String",
|
3364
|
-
"Array",
|
3365
|
-
"Object",
|
3366
|
-
"Function",
|
3367
|
-
"Promise",
|
3368
|
-
]);
|
3369
|
-
// determine if expression has side effects
|
3370
|
-
(function(def_has_side_effects) {
|
3371
|
-
def_has_side_effects(AST_Node, return_true);
|
3372
|
-
|
3373
|
-
def_has_side_effects(AST_EmptyStatement, return_false);
|
3374
|
-
def_has_side_effects(AST_Constant, return_false);
|
3375
|
-
def_has_side_effects(AST_This, return_false);
|
3376
|
-
|
3377
|
-
function any(list, compressor) {
|
3378
|
-
for (var i = list.length; --i >= 0;)
|
3379
|
-
if (list[i].has_side_effects(compressor))
|
3380
|
-
return true;
|
3381
|
-
return false;
|
3382
|
-
}
|
3383
|
-
|
3384
|
-
def_has_side_effects(AST_Block, function(compressor) {
|
3385
|
-
return any(this.body, compressor);
|
3386
|
-
});
|
3387
|
-
def_has_side_effects(AST_Call, function(compressor) {
|
3388
|
-
if (
|
3389
|
-
!this.is_callee_pure(compressor)
|
3390
|
-
&& (!this.expression.is_call_pure(compressor)
|
3391
|
-
|| this.expression.has_side_effects(compressor))
|
3392
|
-
) {
|
3393
|
-
return true;
|
3394
|
-
}
|
3395
|
-
return any(this.args, compressor);
|
3396
|
-
});
|
3397
|
-
def_has_side_effects(AST_Switch, function(compressor) {
|
3398
|
-
return this.expression.has_side_effects(compressor)
|
3399
|
-
|| any(this.body, compressor);
|
3400
|
-
});
|
3401
|
-
def_has_side_effects(AST_Case, function(compressor) {
|
3402
|
-
return this.expression.has_side_effects(compressor)
|
3403
|
-
|| any(this.body, compressor);
|
3404
|
-
});
|
3405
|
-
def_has_side_effects(AST_Try, function(compressor) {
|
3406
|
-
return any(this.body, compressor)
|
3407
|
-
|| this.bcatch && this.bcatch.has_side_effects(compressor)
|
3408
|
-
|| this.bfinally && this.bfinally.has_side_effects(compressor);
|
3409
|
-
});
|
3410
|
-
def_has_side_effects(AST_If, function(compressor) {
|
3411
|
-
return this.condition.has_side_effects(compressor)
|
3412
|
-
|| this.body && this.body.has_side_effects(compressor)
|
3413
|
-
|| this.alternative && this.alternative.has_side_effects(compressor);
|
3414
|
-
});
|
3415
|
-
def_has_side_effects(AST_LabeledStatement, function(compressor) {
|
3416
|
-
return this.body.has_side_effects(compressor);
|
3417
|
-
});
|
3418
|
-
def_has_side_effects(AST_SimpleStatement, function(compressor) {
|
3419
|
-
return this.body.has_side_effects(compressor);
|
3420
|
-
});
|
3421
|
-
def_has_side_effects(AST_Lambda, return_false);
|
3422
|
-
def_has_side_effects(AST_Class, function (compressor) {
|
3423
|
-
if (this.extends && this.extends.has_side_effects(compressor)) {
|
3424
|
-
return true;
|
3425
|
-
}
|
3426
|
-
return any(this.properties, compressor);
|
3427
|
-
});
|
3428
|
-
def_has_side_effects(AST_Binary, function(compressor) {
|
3429
|
-
return this.left.has_side_effects(compressor)
|
3430
|
-
|| this.right.has_side_effects(compressor);
|
3431
|
-
});
|
3432
|
-
def_has_side_effects(AST_Assign, return_true);
|
3433
|
-
def_has_side_effects(AST_Conditional, function(compressor) {
|
3434
|
-
return this.condition.has_side_effects(compressor)
|
3435
|
-
|| this.consequent.has_side_effects(compressor)
|
3436
|
-
|| this.alternative.has_side_effects(compressor);
|
3437
|
-
});
|
3438
|
-
def_has_side_effects(AST_Unary, function(compressor) {
|
3439
|
-
return unary_side_effects.has(this.operator)
|
3440
|
-
|| this.expression.has_side_effects(compressor);
|
3441
|
-
});
|
3442
|
-
def_has_side_effects(AST_SymbolRef, function(compressor) {
|
3443
|
-
return !this.is_declared(compressor) && !pure_prop_access_globals.has(this.name);
|
3444
|
-
});
|
3445
|
-
def_has_side_effects(AST_SymbolClassProperty, return_false);
|
3446
|
-
def_has_side_effects(AST_SymbolDeclaration, return_false);
|
3447
|
-
def_has_side_effects(AST_Object, function(compressor) {
|
3448
|
-
return any(this.properties, compressor);
|
3449
|
-
});
|
3450
|
-
def_has_side_effects(AST_ObjectProperty, function(compressor) {
|
3451
|
-
return (
|
3452
|
-
this.computed_key() && this.key.has_side_effects(compressor)
|
3453
|
-
|| this.value && this.value.has_side_effects(compressor)
|
3454
|
-
);
|
3455
|
-
});
|
3456
|
-
def_has_side_effects(AST_ClassProperty, function(compressor) {
|
3457
|
-
return (
|
3458
|
-
this.computed_key() && this.key.has_side_effects(compressor)
|
3459
|
-
|| this.static && this.value && this.value.has_side_effects(compressor)
|
3460
|
-
);
|
3461
|
-
});
|
3462
|
-
def_has_side_effects(AST_ConciseMethod, function(compressor) {
|
3463
|
-
return this.computed_key() && this.key.has_side_effects(compressor);
|
3464
|
-
});
|
3465
|
-
def_has_side_effects(AST_ObjectGetter, function(compressor) {
|
3466
|
-
return this.computed_key() && this.key.has_side_effects(compressor);
|
3467
|
-
});
|
3468
|
-
def_has_side_effects(AST_ObjectSetter, function(compressor) {
|
3469
|
-
return this.computed_key() && this.key.has_side_effects(compressor);
|
3470
|
-
});
|
3471
|
-
def_has_side_effects(AST_Array, function(compressor) {
|
3472
|
-
return any(this.elements, compressor);
|
3473
|
-
});
|
3474
|
-
def_has_side_effects(AST_Dot, function(compressor) {
|
3475
|
-
return !this.optional && this.expression.may_throw_on_access(compressor)
|
3476
|
-
|| this.expression.has_side_effects(compressor);
|
3477
|
-
});
|
3478
|
-
def_has_side_effects(AST_Sub, function(compressor) {
|
3479
|
-
if (this.optional && is_nullish(this.expression)) {
|
3480
|
-
return false;
|
3481
|
-
}
|
3482
|
-
|
3483
|
-
return !this.optional && this.expression.may_throw_on_access(compressor)
|
3484
|
-
|| this.expression.has_side_effects(compressor)
|
3485
|
-
|| this.property.has_side_effects(compressor);
|
3486
|
-
});
|
3487
|
-
def_has_side_effects(AST_Chain, function (compressor) {
|
3488
|
-
return this.expression.has_side_effects(compressor);
|
3489
|
-
});
|
3490
|
-
def_has_side_effects(AST_Sequence, function(compressor) {
|
3491
|
-
return any(this.expressions, compressor);
|
3492
|
-
});
|
3493
|
-
def_has_side_effects(AST_Definitions, function(compressor) {
|
3494
|
-
return any(this.definitions, compressor);
|
3495
|
-
});
|
3496
|
-
def_has_side_effects(AST_VarDef, function() {
|
3497
|
-
return this.value;
|
3498
|
-
});
|
3499
|
-
def_has_side_effects(AST_TemplateSegment, return_false);
|
3500
|
-
def_has_side_effects(AST_TemplateString, function(compressor) {
|
3501
|
-
return any(this.segments, compressor);
|
3502
|
-
});
|
3503
|
-
})(function(node, func) {
|
3504
|
-
node.DEFMETHOD("has_side_effects", func);
|
3505
|
-
});
|
3506
|
-
|
3507
|
-
// determine if expression may throw
|
3508
|
-
(function(def_may_throw) {
|
3509
|
-
def_may_throw(AST_Node, return_true);
|
3510
|
-
|
3511
|
-
def_may_throw(AST_Constant, return_false);
|
3512
|
-
def_may_throw(AST_EmptyStatement, return_false);
|
3513
|
-
def_may_throw(AST_Lambda, return_false);
|
3514
|
-
def_may_throw(AST_SymbolDeclaration, return_false);
|
3515
|
-
def_may_throw(AST_This, return_false);
|
3516
|
-
|
3517
|
-
function any(list, compressor) {
|
3518
|
-
for (var i = list.length; --i >= 0;)
|
3519
|
-
if (list[i].may_throw(compressor))
|
3520
|
-
return true;
|
3521
|
-
return false;
|
3522
|
-
}
|
3523
|
-
|
3524
|
-
def_may_throw(AST_Class, function(compressor) {
|
3525
|
-
if (this.extends && this.extends.may_throw(compressor)) return true;
|
3526
|
-
return any(this.properties, compressor);
|
3527
|
-
});
|
3528
|
-
|
3529
|
-
def_may_throw(AST_Array, function(compressor) {
|
3530
|
-
return any(this.elements, compressor);
|
3531
|
-
});
|
3532
|
-
def_may_throw(AST_Assign, function(compressor) {
|
3533
|
-
if (this.right.may_throw(compressor)) return true;
|
3534
|
-
if (!compressor.has_directive("use strict")
|
3535
|
-
&& this.operator == "="
|
3536
|
-
&& this.left instanceof AST_SymbolRef) {
|
3537
|
-
return false;
|
3538
|
-
}
|
3539
|
-
return this.left.may_throw(compressor);
|
3540
|
-
});
|
3541
|
-
def_may_throw(AST_Binary, function(compressor) {
|
3542
|
-
return this.left.may_throw(compressor)
|
3543
|
-
|| this.right.may_throw(compressor);
|
3544
|
-
});
|
3545
|
-
def_may_throw(AST_Block, function(compressor) {
|
3546
|
-
return any(this.body, compressor);
|
3547
|
-
});
|
3548
|
-
def_may_throw(AST_Call, function(compressor) {
|
3549
|
-
if (this.optional && is_nullish(this.expression)) return false;
|
3550
|
-
if (any(this.args, compressor)) return true;
|
3551
|
-
if (this.is_callee_pure(compressor)) return false;
|
3552
|
-
if (this.expression.may_throw(compressor)) return true;
|
3553
|
-
return !(this.expression instanceof AST_Lambda)
|
3554
|
-
|| any(this.expression.body, compressor);
|
3555
|
-
});
|
3556
|
-
def_may_throw(AST_Case, function(compressor) {
|
3557
|
-
return this.expression.may_throw(compressor)
|
3558
|
-
|| any(this.body, compressor);
|
3559
|
-
});
|
3560
|
-
def_may_throw(AST_Conditional, function(compressor) {
|
3561
|
-
return this.condition.may_throw(compressor)
|
3562
|
-
|| this.consequent.may_throw(compressor)
|
3563
|
-
|| this.alternative.may_throw(compressor);
|
3564
|
-
});
|
3565
|
-
def_may_throw(AST_Definitions, function(compressor) {
|
3566
|
-
return any(this.definitions, compressor);
|
3567
|
-
});
|
3568
|
-
def_may_throw(AST_If, function(compressor) {
|
3569
|
-
return this.condition.may_throw(compressor)
|
3570
|
-
|| this.body && this.body.may_throw(compressor)
|
3571
|
-
|| this.alternative && this.alternative.may_throw(compressor);
|
3572
|
-
});
|
3573
|
-
def_may_throw(AST_LabeledStatement, function(compressor) {
|
3574
|
-
return this.body.may_throw(compressor);
|
3575
|
-
});
|
3576
|
-
def_may_throw(AST_Object, function(compressor) {
|
3577
|
-
return any(this.properties, compressor);
|
3578
|
-
});
|
3579
|
-
def_may_throw(AST_ObjectProperty, function(compressor) {
|
3580
|
-
// TODO key may throw too
|
3581
|
-
return this.value ? this.value.may_throw(compressor) : false;
|
3582
|
-
});
|
3583
|
-
def_may_throw(AST_ClassProperty, function(compressor) {
|
3584
|
-
return (
|
3585
|
-
this.computed_key() && this.key.may_throw(compressor)
|
3586
|
-
|| this.static && this.value && this.value.may_throw(compressor)
|
3587
|
-
);
|
3588
|
-
});
|
3589
|
-
def_may_throw(AST_ConciseMethod, function(compressor) {
|
3590
|
-
return this.computed_key() && this.key.may_throw(compressor);
|
3591
|
-
});
|
3592
|
-
def_may_throw(AST_ObjectGetter, function(compressor) {
|
3593
|
-
return this.computed_key() && this.key.may_throw(compressor);
|
3594
|
-
});
|
3595
|
-
def_may_throw(AST_ObjectSetter, function(compressor) {
|
3596
|
-
return this.computed_key() && this.key.may_throw(compressor);
|
3597
|
-
});
|
3598
|
-
def_may_throw(AST_Return, function(compressor) {
|
3599
|
-
return this.value && this.value.may_throw(compressor);
|
3600
|
-
});
|
3601
|
-
def_may_throw(AST_Sequence, function(compressor) {
|
3602
|
-
return any(this.expressions, compressor);
|
3603
|
-
});
|
3604
|
-
def_may_throw(AST_SimpleStatement, function(compressor) {
|
3605
|
-
return this.body.may_throw(compressor);
|
3606
|
-
});
|
3607
|
-
def_may_throw(AST_Dot, function(compressor) {
|
3608
|
-
return !this.optional && this.expression.may_throw_on_access(compressor)
|
3609
|
-
|| this.expression.may_throw(compressor);
|
3610
|
-
});
|
3611
|
-
def_may_throw(AST_Sub, function(compressor) {
|
3612
|
-
if (this.optional && is_nullish(this.expression)) return false;
|
3613
|
-
|
3614
|
-
return !this.optional && this.expression.may_throw_on_access(compressor)
|
3615
|
-
|| this.expression.may_throw(compressor)
|
3616
|
-
|| this.property.may_throw(compressor);
|
3617
|
-
});
|
3618
|
-
def_may_throw(AST_Chain, function(compressor) {
|
3619
|
-
return this.expression.may_throw(compressor);
|
3620
|
-
});
|
3621
|
-
def_may_throw(AST_Switch, function(compressor) {
|
3622
|
-
return this.expression.may_throw(compressor)
|
3623
|
-
|| any(this.body, compressor);
|
3624
|
-
});
|
3625
|
-
def_may_throw(AST_SymbolRef, function(compressor) {
|
3626
|
-
return !this.is_declared(compressor) && !pure_prop_access_globals.has(this.name);
|
3627
|
-
});
|
3628
|
-
def_may_throw(AST_SymbolClassProperty, return_false);
|
3629
|
-
def_may_throw(AST_Try, function(compressor) {
|
3630
|
-
return this.bcatch ? this.bcatch.may_throw(compressor) : any(this.body, compressor)
|
3631
|
-
|| this.bfinally && this.bfinally.may_throw(compressor);
|
3632
|
-
});
|
3633
|
-
def_may_throw(AST_Unary, function(compressor) {
|
3634
|
-
if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef)
|
3635
|
-
return false;
|
3636
|
-
return this.expression.may_throw(compressor);
|
3637
|
-
});
|
3638
|
-
def_may_throw(AST_VarDef, function(compressor) {
|
3639
|
-
if (!this.value) return false;
|
3640
|
-
return this.value.may_throw(compressor);
|
3641
|
-
});
|
3642
|
-
})(function(node, func) {
|
3643
|
-
node.DEFMETHOD("may_throw", func);
|
3644
|
-
});
|
3645
|
-
|
3646
|
-
// determine if expression is constant
|
3647
|
-
(function(def_is_constant_expression) {
|
3648
|
-
function all_refs_local(scope) {
|
3649
|
-
let result = true;
|
3650
|
-
walk(this, node => {
|
3651
|
-
if (node instanceof AST_SymbolRef) {
|
3652
|
-
if (has_flag(this, INLINED)) {
|
3653
|
-
result = false;
|
3654
|
-
return walk_abort;
|
3655
|
-
}
|
3656
|
-
var def = node.definition();
|
3657
|
-
if (
|
3658
|
-
member(def, this.enclosed)
|
3659
|
-
&& !this.variables.has(def.name)
|
3660
|
-
) {
|
3661
|
-
if (scope) {
|
3662
|
-
var scope_def = scope.find_variable(node);
|
3663
|
-
if (def.undeclared ? !scope_def : scope_def === def) {
|
3664
|
-
result = "f";
|
3665
|
-
return true;
|
3666
|
-
}
|
3667
|
-
}
|
3668
|
-
result = false;
|
3669
|
-
return walk_abort;
|
3670
|
-
}
|
3671
|
-
return true;
|
3672
|
-
}
|
3673
|
-
if (node instanceof AST_This && this instanceof AST_Arrow) {
|
3674
|
-
// TODO check arguments too!
|
3675
|
-
result = false;
|
3676
|
-
return walk_abort;
|
3677
|
-
}
|
3678
|
-
});
|
3679
|
-
return result;
|
3680
|
-
}
|
3681
|
-
|
3682
|
-
def_is_constant_expression(AST_Node, return_false);
|
3683
|
-
def_is_constant_expression(AST_Constant, return_true);
|
3684
|
-
def_is_constant_expression(AST_Class, function(scope) {
|
3685
|
-
if (this.extends && !this.extends.is_constant_expression(scope)) {
|
3686
|
-
return false;
|
529
|
+
} else if (node instanceof AST_With) {
|
530
|
+
node.body = node.body.transform(tt);
|
3687
531
|
}
|
532
|
+
return node;
|
533
|
+
});
|
534
|
+
self.transform(tt);
|
535
|
+
});
|
3688
536
|
|
3689
|
-
|
3690
|
-
|
3691
|
-
|
3692
|
-
|
3693
|
-
|
3694
|
-
|
537
|
+
AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) {
|
538
|
+
const self = this;
|
539
|
+
const reduce_vars = compressor.option("reduce_vars");
|
540
|
+
|
541
|
+
const preparation = new TreeWalker(function(node, descend) {
|
542
|
+
clear_flag(node, CLEAR_BETWEEN_PASSES);
|
543
|
+
if (reduce_vars) {
|
544
|
+
if (compressor.top_retain
|
545
|
+
&& node instanceof AST_Defun // Only functions are retained
|
546
|
+
&& preparation.parent() === self
|
547
|
+
) {
|
548
|
+
set_flag(node, TOP);
|
3695
549
|
}
|
550
|
+
return node.reduce_vars(preparation, descend, compressor);
|
3696
551
|
}
|
3697
|
-
|
3698
|
-
return all_refs_local.call(this, scope);
|
3699
|
-
});
|
3700
|
-
def_is_constant_expression(AST_Lambda, all_refs_local);
|
3701
|
-
def_is_constant_expression(AST_Unary, function() {
|
3702
|
-
return this.expression.is_constant_expression();
|
3703
|
-
});
|
3704
|
-
def_is_constant_expression(AST_Binary, function() {
|
3705
|
-
return this.left.is_constant_expression()
|
3706
|
-
&& this.right.is_constant_expression();
|
3707
552
|
});
|
3708
|
-
|
3709
|
-
|
3710
|
-
|
3711
|
-
|
3712
|
-
|
3713
|
-
|
3714
|
-
|
3715
|
-
|
3716
|
-
|
3717
|
-
|
3718
|
-
|
553
|
+
// Stack of look-up tables to keep track of whether a `SymbolDef` has been
|
554
|
+
// properly assigned before use:
|
555
|
+
// - `push()` & `pop()` when visiting conditional branches
|
556
|
+
preparation.safe_ids = Object.create(null);
|
557
|
+
preparation.in_loop = null;
|
558
|
+
preparation.loop_ids = new Map();
|
559
|
+
preparation.defs_to_safe_ids = new Map();
|
560
|
+
self.walk(preparation);
|
561
|
+
});
|
562
|
+
|
563
|
+
AST_Symbol.DEFMETHOD("fixed_value", function() {
|
564
|
+
var fixed = this.thedef.fixed;
|
565
|
+
if (!fixed || fixed instanceof AST_Node) return fixed;
|
566
|
+
return fixed();
|
567
|
+
});
|
568
|
+
|
569
|
+
AST_SymbolRef.DEFMETHOD("is_immutable", function() {
|
570
|
+
var orig = this.definition().orig;
|
571
|
+
return orig.length == 1 && orig[0] instanceof AST_SymbolLambda;
|
3719
572
|
});
|
3720
573
|
|
3721
|
-
|
3722
|
-
|
3723
|
-
|
574
|
+
function find_scope(tw) {
|
575
|
+
for (let i = 0;;i++) {
|
576
|
+
const p = tw.parent(i);
|
577
|
+
if (p instanceof AST_Toplevel) return p;
|
578
|
+
if (p instanceof AST_Lambda) return p;
|
579
|
+
if (p.block_scope) return p.block_scope;
|
580
|
+
}
|
3724
581
|
}
|
3725
|
-
|
3726
|
-
|
3727
|
-
|
3728
|
-
|
3729
|
-
|
3730
|
-
|
3731
|
-
|
3732
|
-
|
3733
|
-
}
|
3734
|
-
|
3735
|
-
|
3736
|
-
|
3737
|
-
|
3738
|
-
|
3739
|
-
|
3740
|
-
|
3741
|
-
|
3742
|
-
|
3743
|
-
|
582
|
+
|
583
|
+
function find_variable(compressor, name) {
|
584
|
+
var scope, i = 0;
|
585
|
+
while (scope = compressor.parent(i++)) {
|
586
|
+
if (scope instanceof AST_Scope) break;
|
587
|
+
if (scope instanceof AST_Catch && scope.argname) {
|
588
|
+
scope = scope.argname.definition().scope;
|
589
|
+
break;
|
590
|
+
}
|
591
|
+
}
|
592
|
+
return scope.find_variable(name);
|
593
|
+
}
|
594
|
+
|
595
|
+
function is_empty(thing) {
|
596
|
+
if (thing === null) return true;
|
597
|
+
if (thing instanceof AST_EmptyStatement) return true;
|
598
|
+
if (thing instanceof AST_BlockStatement) return thing.body.length == 0;
|
599
|
+
return false;
|
600
|
+
}
|
601
|
+
|
602
|
+
var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Math Number parseFloat parseInt RangeError ReferenceError RegExp Object setInterval setTimeout String SyntaxError TypeError unescape URIError");
|
603
|
+
AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) {
|
604
|
+
return !this.definition().undeclared
|
605
|
+
|| compressor.option("unsafe") && global_names.has(this.name);
|
3744
606
|
});
|
3745
607
|
|
3746
608
|
/* -----[ optimizers ]----- */
|
@@ -4382,7 +1244,7 @@ AST_Scope.DEFMETHOD("hoist_properties", function(compressor) {
|
|
4382
1244
|
) {
|
4383
1245
|
const defs = defs_by_id.get(node.expression.definition().id);
|
4384
1246
|
if (defs) {
|
4385
|
-
const def = defs.get(String(
|
1247
|
+
const def = defs.get(String(get_simple_key(node.property)));
|
4386
1248
|
const sym = make_node(AST_SymbolRef, node, {
|
4387
1249
|
name: def.name,
|
4388
1250
|
scope: node.expression.scope,
|
@@ -4396,223 +1258,6 @@ AST_Scope.DEFMETHOD("hoist_properties", function(compressor) {
|
|
4396
1258
|
return self.transform(hoister);
|
4397
1259
|
});
|
4398
1260
|
|
4399
|
-
// drop_side_effect_free()
|
4400
|
-
// remove side-effect-free parts which only affects return value
|
4401
|
-
(function(def_drop_side_effect_free) {
|
4402
|
-
// Drop side-effect-free elements from an array of expressions.
|
4403
|
-
// Returns an array of expressions with side-effects or null
|
4404
|
-
// if all elements were dropped. Note: original array may be
|
4405
|
-
// returned if nothing changed.
|
4406
|
-
function trim(nodes, compressor, first_in_statement) {
|
4407
|
-
var len = nodes.length;
|
4408
|
-
if (!len) return null;
|
4409
|
-
var ret = [], changed = false;
|
4410
|
-
for (var i = 0; i < len; i++) {
|
4411
|
-
var node = nodes[i].drop_side_effect_free(compressor, first_in_statement);
|
4412
|
-
changed |= node !== nodes[i];
|
4413
|
-
if (node) {
|
4414
|
-
ret.push(node);
|
4415
|
-
first_in_statement = false;
|
4416
|
-
}
|
4417
|
-
}
|
4418
|
-
return changed ? ret.length ? ret : null : nodes;
|
4419
|
-
}
|
4420
|
-
|
4421
|
-
def_drop_side_effect_free(AST_Node, return_this);
|
4422
|
-
def_drop_side_effect_free(AST_Constant, return_null);
|
4423
|
-
def_drop_side_effect_free(AST_This, return_null);
|
4424
|
-
def_drop_side_effect_free(AST_Call, function(compressor, first_in_statement) {
|
4425
|
-
if (this.optional && is_nullish(this.expression)) {
|
4426
|
-
return make_node(AST_Undefined, this);
|
4427
|
-
}
|
4428
|
-
|
4429
|
-
if (!this.is_callee_pure(compressor)) {
|
4430
|
-
if (this.expression.is_call_pure(compressor)) {
|
4431
|
-
var exprs = this.args.slice();
|
4432
|
-
exprs.unshift(this.expression.expression);
|
4433
|
-
exprs = trim(exprs, compressor, first_in_statement);
|
4434
|
-
return exprs && make_sequence(this, exprs);
|
4435
|
-
}
|
4436
|
-
if (is_func_expr(this.expression)
|
4437
|
-
&& (!this.expression.name || !this.expression.name.definition().references.length)) {
|
4438
|
-
var node = this.clone();
|
4439
|
-
node.expression.process_expression(false, compressor);
|
4440
|
-
return node;
|
4441
|
-
}
|
4442
|
-
return this;
|
4443
|
-
}
|
4444
|
-
var args = trim(this.args, compressor, first_in_statement);
|
4445
|
-
return args && make_sequence(this, args);
|
4446
|
-
});
|
4447
|
-
def_drop_side_effect_free(AST_Accessor, return_null);
|
4448
|
-
def_drop_side_effect_free(AST_Function, return_null);
|
4449
|
-
def_drop_side_effect_free(AST_Arrow, return_null);
|
4450
|
-
def_drop_side_effect_free(AST_Class, function (compressor) {
|
4451
|
-
const with_effects = [];
|
4452
|
-
const trimmed_extends = this.extends && this.extends.drop_side_effect_free(compressor);
|
4453
|
-
if (trimmed_extends) with_effects.push(trimmed_extends);
|
4454
|
-
for (const prop of this.properties) {
|
4455
|
-
const trimmed_prop = prop.drop_side_effect_free(compressor);
|
4456
|
-
if (trimmed_prop) with_effects.push(trimmed_prop);
|
4457
|
-
}
|
4458
|
-
if (!with_effects.length) return null;
|
4459
|
-
return make_sequence(this, with_effects);
|
4460
|
-
});
|
4461
|
-
def_drop_side_effect_free(AST_Binary, function(compressor, first_in_statement) {
|
4462
|
-
var right = this.right.drop_side_effect_free(compressor);
|
4463
|
-
if (!right) return this.left.drop_side_effect_free(compressor, first_in_statement);
|
4464
|
-
if (lazy_op.has(this.operator)) {
|
4465
|
-
if (right === this.right) return this;
|
4466
|
-
var node = this.clone();
|
4467
|
-
node.right = right;
|
4468
|
-
return node;
|
4469
|
-
} else {
|
4470
|
-
var left = this.left.drop_side_effect_free(compressor, first_in_statement);
|
4471
|
-
if (!left) return this.right.drop_side_effect_free(compressor, first_in_statement);
|
4472
|
-
return make_sequence(this, [ left, right ]);
|
4473
|
-
}
|
4474
|
-
});
|
4475
|
-
def_drop_side_effect_free(AST_Assign, function(compressor) {
|
4476
|
-
if (this.logical) return this;
|
4477
|
-
|
4478
|
-
var left = this.left;
|
4479
|
-
if (left.has_side_effects(compressor)
|
4480
|
-
|| compressor.has_directive("use strict")
|
4481
|
-
&& left instanceof AST_PropAccess
|
4482
|
-
&& left.expression.is_constant()) {
|
4483
|
-
return this;
|
4484
|
-
}
|
4485
|
-
set_flag(this, WRITE_ONLY);
|
4486
|
-
while (left instanceof AST_PropAccess) {
|
4487
|
-
left = left.expression;
|
4488
|
-
}
|
4489
|
-
if (left.is_constant_expression(compressor.find_parent(AST_Scope))) {
|
4490
|
-
return this.right.drop_side_effect_free(compressor);
|
4491
|
-
}
|
4492
|
-
return this;
|
4493
|
-
});
|
4494
|
-
def_drop_side_effect_free(AST_Conditional, function(compressor) {
|
4495
|
-
var consequent = this.consequent.drop_side_effect_free(compressor);
|
4496
|
-
var alternative = this.alternative.drop_side_effect_free(compressor);
|
4497
|
-
if (consequent === this.consequent && alternative === this.alternative) return this;
|
4498
|
-
if (!consequent) return alternative ? make_node(AST_Binary, this, {
|
4499
|
-
operator: "||",
|
4500
|
-
left: this.condition,
|
4501
|
-
right: alternative
|
4502
|
-
}) : this.condition.drop_side_effect_free(compressor);
|
4503
|
-
if (!alternative) return make_node(AST_Binary, this, {
|
4504
|
-
operator: "&&",
|
4505
|
-
left: this.condition,
|
4506
|
-
right: consequent
|
4507
|
-
});
|
4508
|
-
var node = this.clone();
|
4509
|
-
node.consequent = consequent;
|
4510
|
-
node.alternative = alternative;
|
4511
|
-
return node;
|
4512
|
-
});
|
4513
|
-
def_drop_side_effect_free(AST_Unary, function(compressor, first_in_statement) {
|
4514
|
-
if (unary_side_effects.has(this.operator)) {
|
4515
|
-
if (!this.expression.has_side_effects(compressor)) {
|
4516
|
-
set_flag(this, WRITE_ONLY);
|
4517
|
-
} else {
|
4518
|
-
clear_flag(this, WRITE_ONLY);
|
4519
|
-
}
|
4520
|
-
return this;
|
4521
|
-
}
|
4522
|
-
if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) return null;
|
4523
|
-
var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
|
4524
|
-
if (first_in_statement && expression && is_iife_call(expression)) {
|
4525
|
-
if (expression === this.expression && this.operator == "!") return this;
|
4526
|
-
return expression.negate(compressor, first_in_statement);
|
4527
|
-
}
|
4528
|
-
return expression;
|
4529
|
-
});
|
4530
|
-
def_drop_side_effect_free(AST_SymbolRef, function(compressor) {
|
4531
|
-
const safe_access = this.is_declared(compressor)
|
4532
|
-
|| pure_prop_access_globals.has(this.name);
|
4533
|
-
return safe_access ? null : this;
|
4534
|
-
});
|
4535
|
-
def_drop_side_effect_free(AST_Object, function(compressor, first_in_statement) {
|
4536
|
-
var values = trim(this.properties, compressor, first_in_statement);
|
4537
|
-
return values && make_sequence(this, values);
|
4538
|
-
});
|
4539
|
-
def_drop_side_effect_free(AST_ObjectProperty, function(compressor, first_in_statement) {
|
4540
|
-
const computed_key = this instanceof AST_ObjectKeyVal && this.key instanceof AST_Node;
|
4541
|
-
const key = computed_key && this.key.drop_side_effect_free(compressor, first_in_statement);
|
4542
|
-
const value = this.value && this.value.drop_side_effect_free(compressor, first_in_statement);
|
4543
|
-
if (key && value) {
|
4544
|
-
return make_sequence(this, [key, value]);
|
4545
|
-
}
|
4546
|
-
return key || value;
|
4547
|
-
});
|
4548
|
-
def_drop_side_effect_free(AST_ClassProperty, function (compressor) {
|
4549
|
-
const key = this.computed_key() && this.key.drop_side_effect_free(compressor);
|
4550
|
-
|
4551
|
-
const value = this.static && this.value
|
4552
|
-
&& this.value.drop_side_effect_free(compressor);
|
4553
|
-
|
4554
|
-
if (key && value) return make_sequence(this, [key, value]);
|
4555
|
-
return key || value || null;
|
4556
|
-
});
|
4557
|
-
def_drop_side_effect_free(AST_ConciseMethod, function () {
|
4558
|
-
return this.computed_key() ? this.key : null;
|
4559
|
-
});
|
4560
|
-
def_drop_side_effect_free(AST_ObjectGetter, function () {
|
4561
|
-
return this.computed_key() ? this.key : null;
|
4562
|
-
});
|
4563
|
-
def_drop_side_effect_free(AST_ObjectSetter, function () {
|
4564
|
-
return this.computed_key() ? this.key : null;
|
4565
|
-
});
|
4566
|
-
def_drop_side_effect_free(AST_Array, function(compressor, first_in_statement) {
|
4567
|
-
var values = trim(this.elements, compressor, first_in_statement);
|
4568
|
-
return values && make_sequence(this, values);
|
4569
|
-
});
|
4570
|
-
def_drop_side_effect_free(AST_Dot, function(compressor, first_in_statement) {
|
4571
|
-
if (this.optional) {
|
4572
|
-
return is_nullish(this.expression) ? make_node(AST_Undefined, this) : this;
|
4573
|
-
}
|
4574
|
-
if (this.expression.may_throw_on_access(compressor)) return this;
|
4575
|
-
|
4576
|
-
return this.expression.drop_side_effect_free(compressor, first_in_statement);
|
4577
|
-
});
|
4578
|
-
def_drop_side_effect_free(AST_Sub, function(compressor, first_in_statement) {
|
4579
|
-
if (this.optional) {
|
4580
|
-
return is_nullish(this.expression) ? make_node(AST_Undefined, this): this;
|
4581
|
-
}
|
4582
|
-
if (this.expression.may_throw_on_access(compressor)) return this;
|
4583
|
-
|
4584
|
-
var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
|
4585
|
-
if (!expression) return this.property.drop_side_effect_free(compressor, first_in_statement);
|
4586
|
-
var property = this.property.drop_side_effect_free(compressor);
|
4587
|
-
if (!property) return expression;
|
4588
|
-
return make_sequence(this, [ expression, property ]);
|
4589
|
-
});
|
4590
|
-
def_drop_side_effect_free(AST_Chain, function (compressor, first_in_statement) {
|
4591
|
-
return this.expression.drop_side_effect_free(compressor, first_in_statement);
|
4592
|
-
});
|
4593
|
-
def_drop_side_effect_free(AST_Sequence, function(compressor) {
|
4594
|
-
var last = this.tail_node();
|
4595
|
-
var expr = last.drop_side_effect_free(compressor);
|
4596
|
-
if (expr === last) return this;
|
4597
|
-
var expressions = this.expressions.slice(0, -1);
|
4598
|
-
if (expr) expressions.push(expr);
|
4599
|
-
if (!expressions.length) {
|
4600
|
-
return make_node(AST_Number, this, { value: 0 });
|
4601
|
-
}
|
4602
|
-
return make_sequence(this, expressions);
|
4603
|
-
});
|
4604
|
-
def_drop_side_effect_free(AST_Expansion, function(compressor, first_in_statement) {
|
4605
|
-
return this.expression.drop_side_effect_free(compressor, first_in_statement);
|
4606
|
-
});
|
4607
|
-
def_drop_side_effect_free(AST_TemplateSegment, return_null);
|
4608
|
-
def_drop_side_effect_free(AST_TemplateString, function(compressor) {
|
4609
|
-
var values = trim(this.segments, compressor, first_in_statement);
|
4610
|
-
return values && make_sequence(this, values);
|
4611
|
-
});
|
4612
|
-
})(function(node, func) {
|
4613
|
-
node.DEFMETHOD("drop_side_effect_free", func);
|
4614
|
-
});
|
4615
|
-
|
4616
1261
|
def_optimize(AST_SimpleStatement, function(self, compressor) {
|
4617
1262
|
if (compressor.option("side_effects")) {
|
4618
1263
|
var body = self.body;
|
@@ -4631,20 +1276,6 @@ def_optimize(AST_While, function(self, compressor) {
|
|
4631
1276
|
return compressor.option("loops") ? make_node(AST_For, self, self).optimize(compressor) : self;
|
4632
1277
|
});
|
4633
1278
|
|
4634
|
-
function has_break_or_continue(loop, parent) {
|
4635
|
-
var found = false;
|
4636
|
-
var tw = new TreeWalker(function(node) {
|
4637
|
-
if (found || node instanceof AST_Scope) return true;
|
4638
|
-
if (node instanceof AST_LoopControl && tw.loopcontrol_target(node) === loop) {
|
4639
|
-
return found = true;
|
4640
|
-
}
|
4641
|
-
});
|
4642
|
-
if (parent instanceof AST_LabeledStatement) tw.push(parent);
|
4643
|
-
tw.push(loop);
|
4644
|
-
loop.body.walk(tw);
|
4645
|
-
return found;
|
4646
|
-
}
|
4647
|
-
|
4648
1279
|
def_optimize(AST_Do, function(self, compressor) {
|
4649
1280
|
if (!compressor.option("loops")) return self;
|
4650
1281
|
var cond = self.condition.tail_node().evaluate(compressor);
|
@@ -4962,58 +1593,290 @@ def_optimize(AST_Switch, function(self, compressor) {
|
|
4962
1593
|
}
|
4963
1594
|
}
|
4964
1595
|
}
|
4965
|
-
if (aborts(branch)) {
|
4966
|
-
var prev = body[body.length - 1];
|
4967
|
-
if (aborts(prev) && prev.body.length == branch.body.length
|
4968
|
-
&& make_node(AST_BlockStatement, prev, prev).equivalent_to(make_node(AST_BlockStatement, branch, branch))) {
|
4969
|
-
prev.body = [];
|
4970
|
-
}
|
4971
|
-
}
|
4972
1596
|
body.push(branch);
|
4973
1597
|
}
|
4974
1598
|
while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
|
1599
|
+
self.body = body;
|
1600
|
+
|
1601
|
+
let default_or_exact = default_branch || exact_match;
|
1602
|
+
default_branch = null;
|
1603
|
+
exact_match = null;
|
1604
|
+
|
1605
|
+
// group equivalent branches so they will be located next to each other,
|
1606
|
+
// that way the next micro-optimization will merge them.
|
1607
|
+
// ** bail micro-optimization if not a simple switch case with breaks
|
1608
|
+
if (body.every((branch, i) =>
|
1609
|
+
(branch === default_or_exact || !branch.expression.has_side_effects(compressor))
|
1610
|
+
&& (branch.body.length === 0 || aborts(branch) || body.length - 1 === i))
|
1611
|
+
) {
|
1612
|
+
for (let i = 0; i < body.length; i++) {
|
1613
|
+
const branch = body[i];
|
1614
|
+
for (let j = i + 1; j < body.length; j++) {
|
1615
|
+
const next = body[j];
|
1616
|
+
if (next.body.length === 0) continue;
|
1617
|
+
const last_branch = j === (body.length - 1);
|
1618
|
+
const equivalentBranch = branches_equivalent(next, branch, false);
|
1619
|
+
if (equivalentBranch || (last_branch && branches_equivalent(next, branch, true))) {
|
1620
|
+
if (!equivalentBranch && last_branch) {
|
1621
|
+
next.body.push(make_node(AST_Break));
|
1622
|
+
}
|
1623
|
+
|
1624
|
+
// let's find previous siblings with inert fallthrough...
|
1625
|
+
let x = j - 1;
|
1626
|
+
let fallthroughDepth = 0;
|
1627
|
+
while (x > i) {
|
1628
|
+
if (is_inert_body(body[x--])) {
|
1629
|
+
fallthroughDepth++;
|
1630
|
+
} else {
|
1631
|
+
break;
|
1632
|
+
}
|
1633
|
+
}
|
1634
|
+
|
1635
|
+
const plucked = body.splice(j - fallthroughDepth, 1 + fallthroughDepth);
|
1636
|
+
body.splice(i + 1, 0, ...plucked);
|
1637
|
+
i += plucked.length;
|
1638
|
+
}
|
1639
|
+
}
|
1640
|
+
}
|
1641
|
+
}
|
1642
|
+
|
1643
|
+
// merge equivalent branches in a row
|
1644
|
+
for (let i = 0; i < body.length; i++) {
|
1645
|
+
let branch = body[i];
|
1646
|
+
if (branch.body.length === 0) continue;
|
1647
|
+
if (!aborts(branch)) continue;
|
1648
|
+
|
1649
|
+
for (let j = i + 1; j < body.length; i++, j++) {
|
1650
|
+
let next = body[j];
|
1651
|
+
if (next.body.length === 0) continue;
|
1652
|
+
if (
|
1653
|
+
branches_equivalent(next, branch, false)
|
1654
|
+
|| (j === body.length - 1 && branches_equivalent(next, branch, true))
|
1655
|
+
) {
|
1656
|
+
branch.body = [];
|
1657
|
+
branch = next;
|
1658
|
+
continue;
|
1659
|
+
}
|
1660
|
+
break;
|
1661
|
+
}
|
1662
|
+
}
|
1663
|
+
|
1664
|
+
// Prune any empty branches at the end of the switch statement.
|
1665
|
+
{
|
1666
|
+
let i = body.length - 1;
|
1667
|
+
for (; i >= 0; i--) {
|
1668
|
+
let bbody = body[i].body;
|
1669
|
+
if (is_break(bbody[bbody.length - 1], compressor)) bbody.pop();
|
1670
|
+
if (!is_inert_body(body[i])) break;
|
1671
|
+
}
|
1672
|
+
// i now points to the index of a branch that contains a body. By incrementing, it's
|
1673
|
+
// pointing to the first branch that's empty.
|
1674
|
+
i++;
|
1675
|
+
if (!default_or_exact || body.indexOf(default_or_exact) >= i) {
|
1676
|
+
// The default behavior is to do nothing. We can take advantage of that to
|
1677
|
+
// remove all case expressions that are side-effect free that also do
|
1678
|
+
// nothing, since they'll default to doing nothing. But we can't remove any
|
1679
|
+
// case expressions before one that would side-effect, since they may cause
|
1680
|
+
// the side-effect to be skipped.
|
1681
|
+
for (let j = body.length - 1; j >= i; j--) {
|
1682
|
+
let branch = body[j];
|
1683
|
+
if (branch === default_or_exact) {
|
1684
|
+
default_or_exact = null;
|
1685
|
+
body.pop();
|
1686
|
+
} else if (!branch.expression.has_side_effects(compressor)) {
|
1687
|
+
body.pop();
|
1688
|
+
} else {
|
1689
|
+
break;
|
1690
|
+
}
|
1691
|
+
}
|
1692
|
+
}
|
1693
|
+
}
|
1694
|
+
|
1695
|
+
|
1696
|
+
// Prune side-effect free branches that fall into default.
|
1697
|
+
if (default_or_exact) {
|
1698
|
+
let default_index = body.indexOf(default_or_exact);
|
1699
|
+
let default_body_index = default_index;
|
1700
|
+
for (; default_body_index < body.length - 1; default_body_index++) {
|
1701
|
+
if (!is_inert_body(body[default_body_index])) break;
|
1702
|
+
}
|
1703
|
+
let side_effect_index = body.length - 1;
|
1704
|
+
for (; side_effect_index >= 0; side_effect_index--) {
|
1705
|
+
let branch = body[side_effect_index];
|
1706
|
+
if (branch === default_or_exact) continue;
|
1707
|
+
if (branch.expression.has_side_effects(compressor)) break;
|
1708
|
+
}
|
1709
|
+
// If the default behavior comes after any side-effect case expressions,
|
1710
|
+
// then we can fold all side-effect free cases into the default branch.
|
1711
|
+
// If the side-effect case is after the default, then any side-effect
|
1712
|
+
// free cases could prevent the side-effect from occurring.
|
1713
|
+
if (default_body_index > side_effect_index) {
|
1714
|
+
let prev_body_index = default_index - 1;
|
1715
|
+
for (; prev_body_index >= 0; prev_body_index--) {
|
1716
|
+
if (!is_inert_body(body[prev_body_index])) break;
|
1717
|
+
}
|
1718
|
+
let before = Math.max(side_effect_index, prev_body_index) + 1;
|
1719
|
+
let after = default_index;
|
1720
|
+
if (side_effect_index > default_index) {
|
1721
|
+
// If the default falls into the same body as a side-effect
|
1722
|
+
// case, then we need preserve that case and only prune the
|
1723
|
+
// cases after it.
|
1724
|
+
after = side_effect_index;
|
1725
|
+
body[side_effect_index].body = body[default_body_index].body;
|
1726
|
+
} else {
|
1727
|
+
// The default will be the last branch.
|
1728
|
+
default_or_exact.body = body[default_body_index].body;
|
1729
|
+
}
|
1730
|
+
|
1731
|
+
// Prune everything after the default (or last side-effect case)
|
1732
|
+
// until the next case with a body.
|
1733
|
+
body.splice(after + 1, default_body_index - after);
|
1734
|
+
// Prune everything before the default that falls into it.
|
1735
|
+
body.splice(before, default_index - before);
|
1736
|
+
}
|
1737
|
+
}
|
1738
|
+
|
1739
|
+
// See if we can remove the switch entirely if all cases (the default) fall into the same case body.
|
1740
|
+
DEFAULT: if (default_or_exact) {
|
1741
|
+
let i = body.findIndex(branch => !is_inert_body(branch));
|
1742
|
+
let caseBody;
|
1743
|
+
// `i` is equal to one of the following:
|
1744
|
+
// - `-1`, there is no body in the switch statement.
|
1745
|
+
// - `body.length - 1`, all cases fall into the same body.
|
1746
|
+
// - anything else, there are multiple bodies in the switch.
|
1747
|
+
if (i === body.length - 1) {
|
1748
|
+
// All cases fall into the case body.
|
1749
|
+
let branch = body[i];
|
1750
|
+
if (has_nested_break(self)) break DEFAULT;
|
1751
|
+
|
1752
|
+
// This is the last case body, and we've already pruned any breaks, so it's
|
1753
|
+
// safe to hoist.
|
1754
|
+
caseBody = make_node(AST_BlockStatement, branch, {
|
1755
|
+
body: branch.body
|
1756
|
+
});
|
1757
|
+
branch.body = [];
|
1758
|
+
} else if (i !== -1) {
|
1759
|
+
// If there are multiple bodies, then we cannot optimize anything.
|
1760
|
+
break DEFAULT;
|
1761
|
+
}
|
1762
|
+
|
1763
|
+
let sideEffect = body.find(branch => {
|
1764
|
+
return (
|
1765
|
+
branch !== default_or_exact
|
1766
|
+
&& branch.expression.has_side_effects(compressor)
|
1767
|
+
);
|
1768
|
+
});
|
1769
|
+
// If no cases cause a side-effect, we can eliminate the switch entirely.
|
1770
|
+
if (!sideEffect) {
|
1771
|
+
return make_node(AST_BlockStatement, self, {
|
1772
|
+
body: decl.concat(
|
1773
|
+
statement(self.expression),
|
1774
|
+
default_or_exact.expression ? statement(default_or_exact.expression) : [],
|
1775
|
+
caseBody || []
|
1776
|
+
)
|
1777
|
+
}).optimize(compressor);
|
1778
|
+
}
|
1779
|
+
|
1780
|
+
// If we're this far, either there was no body or all cases fell into the same body.
|
1781
|
+
// If there was no body, then we don't need a default branch (because the default is
|
1782
|
+
// do nothing). If there was a body, we'll extract it to after the switch, so the
|
1783
|
+
// switch's new default is to do nothing and we can still prune it.
|
1784
|
+
const default_index = body.indexOf(default_or_exact);
|
1785
|
+
body.splice(default_index, 1);
|
1786
|
+
default_or_exact = null;
|
1787
|
+
|
1788
|
+
if (caseBody) {
|
1789
|
+
// Recurse into switch statement one more time so that we can append the case body
|
1790
|
+
// outside of the switch. This recursion will only happen once since we've pruned
|
1791
|
+
// the default case.
|
1792
|
+
return make_node(AST_BlockStatement, self, {
|
1793
|
+
body: decl.concat(self, caseBody)
|
1794
|
+
}).optimize(compressor);
|
1795
|
+
}
|
1796
|
+
// If we fall here, there is a default branch somewhere, there are no case bodies,
|
1797
|
+
// and there's a side-effect somewhere. Just let the below paths take care of it.
|
1798
|
+
}
|
1799
|
+
|
4975
1800
|
if (body.length > 0) {
|
4976
1801
|
body[0].body = decl.concat(body[0].body);
|
4977
1802
|
}
|
4978
|
-
|
4979
|
-
while (branch = body[body.length - 1]) {
|
4980
|
-
var stat = branch.body[branch.body.length - 1];
|
4981
|
-
if (stat instanceof AST_Break && compressor.loopcontrol_target(stat) === self)
|
4982
|
-
branch.body.pop();
|
4983
|
-
if (branch.body.length || branch instanceof AST_Case
|
4984
|
-
&& (default_branch || branch.expression.has_side_effects(compressor))) break;
|
4985
|
-
if (body.pop() === default_branch) default_branch = null;
|
4986
|
-
}
|
1803
|
+
|
4987
1804
|
if (body.length == 0) {
|
4988
1805
|
return make_node(AST_BlockStatement, self, {
|
4989
|
-
body: decl.concat(
|
4990
|
-
body: self.expression
|
4991
|
-
}))
|
1806
|
+
body: decl.concat(statement(self.expression))
|
4992
1807
|
}).optimize(compressor);
|
4993
1808
|
}
|
4994
|
-
if (body.length == 1 && (
|
4995
|
-
|
4996
|
-
|
4997
|
-
|
4998
|
-
|
4999
|
-
|
5000
|
-
|
5001
|
-
|
5002
|
-
|
5003
|
-
|
5004
|
-
|
5005
|
-
|
5006
|
-
|
5007
|
-
|
5008
|
-
|
5009
|
-
|
5010
|
-
|
5011
|
-
|
5012
|
-
|
5013
|
-
|
5014
|
-
|
1809
|
+
if (body.length == 1 && !has_nested_break(self)) {
|
1810
|
+
// This is the last case body, and we've already pruned any breaks, so it's
|
1811
|
+
// safe to hoist.
|
1812
|
+
let branch = body[0];
|
1813
|
+
return make_node(AST_If, self, {
|
1814
|
+
condition: make_node(AST_Binary, self, {
|
1815
|
+
operator: "===",
|
1816
|
+
left: self.expression,
|
1817
|
+
right: branch.expression,
|
1818
|
+
}),
|
1819
|
+
body: make_node(AST_BlockStatement, branch, {
|
1820
|
+
body: branch.body
|
1821
|
+
}),
|
1822
|
+
alternative: null
|
1823
|
+
}).optimize(compressor);
|
1824
|
+
}
|
1825
|
+
if (body.length === 2 && default_or_exact && !has_nested_break(self)) {
|
1826
|
+
let branch = body[0] === default_or_exact ? body[1] : body[0];
|
1827
|
+
let exact_exp = default_or_exact.expression && statement(default_or_exact.expression);
|
1828
|
+
if (aborts(body[0])) {
|
1829
|
+
// Only the first branch body could have a break (at the last statement)
|
1830
|
+
let first = body[0];
|
1831
|
+
if (is_break(first.body[first.body.length - 1], compressor)) {
|
1832
|
+
first.body.pop();
|
1833
|
+
}
|
1834
|
+
return make_node(AST_If, self, {
|
1835
|
+
condition: make_node(AST_Binary, self, {
|
1836
|
+
operator: "===",
|
1837
|
+
left: self.expression,
|
1838
|
+
right: branch.expression,
|
1839
|
+
}),
|
1840
|
+
body: make_node(AST_BlockStatement, branch, {
|
1841
|
+
body: branch.body
|
1842
|
+
}),
|
1843
|
+
alternative: make_node(AST_BlockStatement, default_or_exact, {
|
1844
|
+
body: [].concat(
|
1845
|
+
exact_exp || [],
|
1846
|
+
default_or_exact.body
|
1847
|
+
)
|
1848
|
+
})
|
5015
1849
|
}).optimize(compressor);
|
5016
1850
|
}
|
1851
|
+
let operator = "===";
|
1852
|
+
let consequent = make_node(AST_BlockStatement, branch, {
|
1853
|
+
body: branch.body,
|
1854
|
+
});
|
1855
|
+
let always = make_node(AST_BlockStatement, default_or_exact, {
|
1856
|
+
body: [].concat(
|
1857
|
+
exact_exp || [],
|
1858
|
+
default_or_exact.body
|
1859
|
+
)
|
1860
|
+
});
|
1861
|
+
if (body[0] === default_or_exact) {
|
1862
|
+
operator = "!==";
|
1863
|
+
let tmp = always;
|
1864
|
+
always = consequent;
|
1865
|
+
consequent = tmp;
|
1866
|
+
}
|
1867
|
+
return make_node(AST_BlockStatement, self, {
|
1868
|
+
body: [
|
1869
|
+
make_node(AST_If, self, {
|
1870
|
+
condition: make_node(AST_Binary, self, {
|
1871
|
+
operator: operator,
|
1872
|
+
left: self.expression,
|
1873
|
+
right: branch.expression,
|
1874
|
+
}),
|
1875
|
+
body: consequent,
|
1876
|
+
alternative: null
|
1877
|
+
})
|
1878
|
+
].concat(always)
|
1879
|
+
}).optimize(compressor);
|
5017
1880
|
}
|
5018
1881
|
return self;
|
5019
1882
|
|
@@ -5024,6 +1887,50 @@ def_optimize(AST_Switch, function(self, compressor) {
|
|
5024
1887
|
trim_unreachable_code(compressor, branch, decl);
|
5025
1888
|
}
|
5026
1889
|
}
|
1890
|
+
function branches_equivalent(branch, prev, insertBreak) {
|
1891
|
+
let bbody = branch.body;
|
1892
|
+
let pbody = prev.body;
|
1893
|
+
if (insertBreak) {
|
1894
|
+
bbody = bbody.concat(make_node(AST_Break));
|
1895
|
+
}
|
1896
|
+
if (bbody.length !== pbody.length) return false;
|
1897
|
+
let bblock = make_node(AST_BlockStatement, branch, { body: bbody });
|
1898
|
+
let pblock = make_node(AST_BlockStatement, prev, { body: pbody });
|
1899
|
+
return bblock.equivalent_to(pblock);
|
1900
|
+
}
|
1901
|
+
function statement(expression) {
|
1902
|
+
return make_node(AST_SimpleStatement, expression, {
|
1903
|
+
body: expression
|
1904
|
+
});
|
1905
|
+
}
|
1906
|
+
function has_nested_break(root) {
|
1907
|
+
let has_break = false;
|
1908
|
+
let tw = new TreeWalker(node => {
|
1909
|
+
if (has_break) return true;
|
1910
|
+
if (node instanceof AST_Lambda) return true;
|
1911
|
+
if (node instanceof AST_SimpleStatement) return true;
|
1912
|
+
if (!is_break(node, tw)) return;
|
1913
|
+
let parent = tw.parent();
|
1914
|
+
if (
|
1915
|
+
parent instanceof AST_SwitchBranch
|
1916
|
+
&& parent.body[parent.body.length - 1] === node
|
1917
|
+
) {
|
1918
|
+
return;
|
1919
|
+
}
|
1920
|
+
has_break = true;
|
1921
|
+
});
|
1922
|
+
root.walk(tw);
|
1923
|
+
return has_break;
|
1924
|
+
}
|
1925
|
+
function is_break(node, stack) {
|
1926
|
+
return node instanceof AST_Break
|
1927
|
+
&& stack.loopcontrol_target(node) === self;
|
1928
|
+
}
|
1929
|
+
function is_inert_body(branch) {
|
1930
|
+
return !aborts(branch) && !make_node(AST_BlockStatement, branch, {
|
1931
|
+
body: branch.body
|
1932
|
+
}).has_side_effects(compressor);
|
1933
|
+
}
|
5027
1934
|
});
|
5028
1935
|
|
5029
1936
|
def_optimize(AST_Try, function(self, compressor) {
|
@@ -5102,11 +2009,11 @@ def_optimize(AST_Definitions, function(self) {
|
|
5102
2009
|
return self;
|
5103
2010
|
});
|
5104
2011
|
|
5105
|
-
def_optimize(AST_VarDef, function(self) {
|
2012
|
+
def_optimize(AST_VarDef, function(self, compressor) {
|
5106
2013
|
if (
|
5107
2014
|
self.name instanceof AST_SymbolLet
|
5108
2015
|
&& self.value != null
|
5109
|
-
&& is_undefined(self.value)
|
2016
|
+
&& is_undefined(self.value, compressor)
|
5110
2017
|
) {
|
5111
2018
|
self.value = null;
|
5112
2019
|
}
|
@@ -5144,10 +2051,6 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
5144
2051
|
}
|
5145
2052
|
}
|
5146
2053
|
|
5147
|
-
if (self.optional && is_nullish(fn)) {
|
5148
|
-
return make_node(AST_Undefined, self);
|
5149
|
-
}
|
5150
|
-
|
5151
2054
|
var is_func = fn instanceof AST_Lambda;
|
5152
2055
|
|
5153
2056
|
if (is_func && fn.pinned()) return self;
|
@@ -5190,6 +2093,14 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
5190
2093
|
}
|
5191
2094
|
|
5192
2095
|
if (compressor.option("unsafe")) {
|
2096
|
+
if (exp instanceof AST_Dot && exp.start.value === "Array" && exp.property === "from" && self.args.length === 1) {
|
2097
|
+
const [argument] = self.args;
|
2098
|
+
if (argument instanceof AST_Array) {
|
2099
|
+
return make_node(AST_Array, argument, {
|
2100
|
+
elements: argument.elements
|
2101
|
+
}).optimize(compressor);
|
2102
|
+
}
|
2103
|
+
}
|
5193
2104
|
if (is_undeclared_ref(exp)) switch (exp.name) {
|
5194
2105
|
case "Array":
|
5195
2106
|
if (self.args.length != 1) {
|
@@ -5395,6 +2306,7 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
5395
2306
|
argnames: [],
|
5396
2307
|
body: []
|
5397
2308
|
}).optimize(compressor);
|
2309
|
+
var nth_identifier = compressor.mangle_options && compressor.mangle_options.nth_identifier || base54;
|
5398
2310
|
if (self.args.every((x) => x instanceof AST_String)) {
|
5399
2311
|
// quite a corner-case, but we can handle it:
|
5400
2312
|
// https://github.com/mishoo/UglifyJS2/issues/203
|
@@ -5404,14 +2316,13 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
5404
2316
|
return arg.value;
|
5405
2317
|
}).join(",") + "){" + self.args[self.args.length - 1].value + "})";
|
5406
2318
|
var ast = parse(code);
|
5407
|
-
var mangle = { ie8: compressor.option("ie8") };
|
2319
|
+
var mangle = { ie8: compressor.option("ie8"), nth_identifier: nth_identifier };
|
5408
2320
|
ast.figure_out_scope(mangle);
|
5409
2321
|
var comp = new Compressor(compressor.options, {
|
5410
2322
|
mangle_options: compressor.mangle_options
|
5411
2323
|
});
|
5412
2324
|
ast = ast.transform(comp);
|
5413
2325
|
ast.figure_out_scope(mangle);
|
5414
|
-
base54.reset();
|
5415
2326
|
ast.compute_char_frequency(mangle);
|
5416
2327
|
ast.mangle_names(mangle);
|
5417
2328
|
var fun;
|
@@ -5506,7 +2417,7 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
5506
2417
|
|| has_annotation(self, _INLINE)
|
5507
2418
|
|| compressor.option("unused")
|
5508
2419
|
&& (def = exp.definition()).references.length == 1
|
5509
|
-
&& !
|
2420
|
+
&& !is_recursive_ref(compressor, def)
|
5510
2421
|
&& fn.is_constant_expression(exp.scope))
|
5511
2422
|
&& !has_annotation(self, _PURE | _NOINLINE)
|
5512
2423
|
&& !fn.contains_this()
|
@@ -5536,6 +2447,7 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
5536
2447
|
if (can_inline && has_annotation(self, _INLINE)) {
|
5537
2448
|
set_flag(fn, SQUEEZED);
|
5538
2449
|
fn = make_node(fn.CTOR === AST_Defun ? AST_Function : fn.CTOR, fn, fn);
|
2450
|
+
fn = fn.clone(true);
|
5539
2451
|
fn.figure_out_scope({}, {
|
5540
2452
|
parent_scope: find_scope(compressor),
|
5541
2453
|
toplevel: compressor.get_toplevel()
|
@@ -5756,12 +2668,14 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
5756
2668
|
flatten_args(decls, expressions);
|
5757
2669
|
flatten_vars(decls, expressions);
|
5758
2670
|
expressions.push(returned_value);
|
2671
|
+
|
5759
2672
|
if (decls.length) {
|
5760
2673
|
const i = scope.body.indexOf(compressor.parent(level - 1)) + 1;
|
5761
2674
|
scope.body.splice(i, 0, make_node(AST_Var, fn, {
|
5762
2675
|
definitions: decls
|
5763
2676
|
}));
|
5764
2677
|
}
|
2678
|
+
|
5765
2679
|
return expressions.map(exp => exp.clone(true));
|
5766
2680
|
}
|
5767
2681
|
});
|
@@ -6170,7 +3084,7 @@ def_optimize(AST_Binary, function(self, compressor) {
|
|
6170
3084
|
}
|
6171
3085
|
break;
|
6172
3086
|
case "??":
|
6173
|
-
if (is_nullish(self.left)) {
|
3087
|
+
if (is_nullish(self.left, compressor)) {
|
6174
3088
|
return self.right;
|
6175
3089
|
}
|
6176
3090
|
|
@@ -6416,20 +3330,6 @@ def_optimize(AST_SymbolExport, function(self) {
|
|
6416
3330
|
return self;
|
6417
3331
|
});
|
6418
3332
|
|
6419
|
-
function recursive_ref(compressor, def) {
|
6420
|
-
var node;
|
6421
|
-
for (var i = 0; node = compressor.parent(i); i++) {
|
6422
|
-
if (
|
6423
|
-
node instanceof AST_Lambda
|
6424
|
-
|| node instanceof AST_Class
|
6425
|
-
) {
|
6426
|
-
var name = node.name;
|
6427
|
-
if (name && name.definition() === def) break;
|
6428
|
-
}
|
6429
|
-
}
|
6430
|
-
return node;
|
6431
|
-
}
|
6432
|
-
|
6433
3333
|
function within_array_or_object_literal(compressor) {
|
6434
3334
|
var node, level = 0;
|
6435
3335
|
while (node = compressor.parent(level++)) {
|
@@ -6493,7 +3393,7 @@ def_optimize(AST_SymbolRef, function(self, compressor) {
|
|
6493
3393
|
|| within_array_or_object_literal(compressor)
|
6494
3394
|
|| !compressor.option("reduce_funcs"))) {
|
6495
3395
|
single_use = false;
|
6496
|
-
} else if (
|
3396
|
+
} else if (is_recursive_ref(compressor, def)) {
|
6497
3397
|
single_use = false;
|
6498
3398
|
} else if (def.scope !== self.scope || def.orig[0] instanceof AST_SymbolFunarg) {
|
6499
3399
|
single_use = fixed.is_constant_expression(self.scope);
|
@@ -6778,23 +3678,6 @@ def_optimize(AST_DefaultAssign, function(self, compressor) {
|
|
6778
3678
|
return self;
|
6779
3679
|
});
|
6780
3680
|
|
6781
|
-
function is_nullish(node) {
|
6782
|
-
let fixed;
|
6783
|
-
return (
|
6784
|
-
node instanceof AST_Null
|
6785
|
-
|| is_undefined(node)
|
6786
|
-
|| (
|
6787
|
-
node instanceof AST_SymbolRef
|
6788
|
-
&& (fixed = node.definition().fixed) instanceof AST_Node
|
6789
|
-
&& is_nullish(fixed)
|
6790
|
-
)
|
6791
|
-
// Recurse into those optional chains!
|
6792
|
-
|| node instanceof AST_PropAccess && node.optional && is_nullish(node.expression)
|
6793
|
-
|| node instanceof AST_Call && node.optional && is_nullish(node.expression)
|
6794
|
-
|| node instanceof AST_Chain && is_nullish(node.expression)
|
6795
|
-
);
|
6796
|
-
}
|
6797
|
-
|
6798
3681
|
function is_nullish_check(check, check_subject, compressor) {
|
6799
3682
|
if (check_subject.may_throw(compressor)) return false;
|
6800
3683
|
|
@@ -6806,8 +3689,8 @@ function is_nullish_check(check, check_subject, compressor) {
|
|
6806
3689
|
&& check.operator === "=="
|
6807
3690
|
// which side is nullish?
|
6808
3691
|
&& (
|
6809
|
-
(nullish_side = is_nullish(check.left) && check.left)
|
6810
|
-
|| (nullish_side = is_nullish(check.right) && check.right)
|
3692
|
+
(nullish_side = is_nullish(check.left, compressor) && check.left)
|
3693
|
+
|| (nullish_side = is_nullish(check.right, compressor) && check.right)
|
6811
3694
|
)
|
6812
3695
|
// is the other side the same as the check_subject
|
6813
3696
|
&& (
|
@@ -6845,12 +3728,12 @@ function is_nullish_check(check, check_subject, compressor) {
|
|
6845
3728
|
null_cmp = cmp;
|
6846
3729
|
defined_side = cmp.left;
|
6847
3730
|
}
|
6848
|
-
if (is_undefined(cmp.left)) {
|
3731
|
+
if (is_undefined(cmp.left, compressor)) {
|
6849
3732
|
found++;
|
6850
3733
|
undefined_cmp = cmp;
|
6851
3734
|
defined_side = cmp.right;
|
6852
3735
|
}
|
6853
|
-
if (is_undefined(cmp.right)) {
|
3736
|
+
if (is_undefined(cmp.right, compressor)) {
|
6854
3737
|
found++;
|
6855
3738
|
undefined_cmp = cmp;
|
6856
3739
|
defined_side = cmp.left;
|
@@ -7045,7 +3928,8 @@ def_optimize(AST_Conditional, function(self, compressor) {
|
|
7045
3928
|
right: alternative
|
7046
3929
|
}).optimize(compressor);
|
7047
3930
|
}
|
7048
|
-
|
3931
|
+
|
3932
|
+
const in_bool = compressor.in_boolean_context();
|
7049
3933
|
if (is_true(self.consequent)) {
|
7050
3934
|
if (is_false(self.alternative)) {
|
7051
3935
|
// c ? true : false ---> !!c
|
@@ -7351,14 +4235,11 @@ def_optimize(AST_Sub, function(self, compressor) {
|
|
7351
4235
|
ev = make_node_from_constant(ev, self).optimize(compressor);
|
7352
4236
|
return best_of(compressor, ev, self);
|
7353
4237
|
}
|
7354
|
-
if (self.optional && is_nullish(self.expression)) {
|
7355
|
-
return make_node(AST_Undefined, self);
|
7356
|
-
}
|
7357
4238
|
return self;
|
7358
4239
|
});
|
7359
4240
|
|
7360
4241
|
def_optimize(AST_Chain, function (self, compressor) {
|
7361
|
-
self.expression
|
4242
|
+
if (is_nullish(self.expression, compressor)) return make_node(AST_Undefined, self);
|
7362
4243
|
return self;
|
7363
4244
|
});
|
7364
4245
|
|
@@ -7425,9 +4306,6 @@ def_optimize(AST_Dot, function(self, compressor) {
|
|
7425
4306
|
ev = make_node_from_constant(ev, self).optimize(compressor);
|
7426
4307
|
return best_of(compressor, ev, self);
|
7427
4308
|
}
|
7428
|
-
if (self.optional && is_nullish(self.expression)) {
|
7429
|
-
return make_node(AST_Undefined, self);
|
7430
|
-
}
|
7431
4309
|
return self;
|
7432
4310
|
});
|
7433
4311
|
|
@@ -7469,7 +4347,7 @@ def_optimize(AST_Array, function(self, compressor) {
|
|
7469
4347
|
return self;
|
7470
4348
|
});
|
7471
4349
|
|
7472
|
-
function inline_object_prop_spread(props) {
|
4350
|
+
function inline_object_prop_spread(props, compressor) {
|
7473
4351
|
for (var i = 0; i < props.length; i++) {
|
7474
4352
|
var prop = props[i];
|
7475
4353
|
if (prop instanceof AST_Expansion) {
|
@@ -7487,6 +4365,11 @@ function inline_object_prop_spread(props) {
|
|
7487
4365
|
// non-iterable value silently does nothing; it is thus safe
|
7488
4366
|
// to remove. AST_String is the only iterable AST_Constant.
|
7489
4367
|
props.splice(i, 1);
|
4368
|
+
i--;
|
4369
|
+
} else if (is_nullish(expr, compressor)) {
|
4370
|
+
// Likewise, null and undefined can be silently removed.
|
4371
|
+
props.splice(i, 1);
|
4372
|
+
i--;
|
7490
4373
|
}
|
7491
4374
|
}
|
7492
4375
|
}
|
@@ -7497,7 +4380,7 @@ def_optimize(AST_Object, function(self, compressor) {
|
|
7497
4380
|
if (optimized !== self) {
|
7498
4381
|
return optimized;
|
7499
4382
|
}
|
7500
|
-
inline_object_prop_spread(self.properties);
|
4383
|
+
inline_object_prop_spread(self.properties, compressor);
|
7501
4384
|
return self;
|
7502
4385
|
});
|
7503
4386
|
|
@@ -7590,7 +4473,7 @@ def_optimize(AST_TemplateString, function(self, compressor) {
|
|
7590
4473
|
&& (
|
7591
4474
|
segments[1].is_string(compressor)
|
7592
4475
|
|| segments[1].is_number(compressor)
|
7593
|
-
|| is_nullish(segments[1])
|
4476
|
+
|| is_nullish(segments[1], compressor)
|
7594
4477
|
|| compressor.option("unsafe")
|
7595
4478
|
)
|
7596
4479
|
) {
|
@@ -7634,12 +4517,15 @@ function lift_key(self, compressor) {
|
|
7634
4517
|
if (self.key.value == "constructor"
|
7635
4518
|
&& compressor.parent() instanceof AST_Class) return self;
|
7636
4519
|
if (self instanceof AST_ObjectKeyVal) {
|
4520
|
+
self.quote = self.key.quote;
|
7637
4521
|
self.key = self.key.value;
|
7638
4522
|
} else if (self instanceof AST_ClassProperty) {
|
4523
|
+
self.quote = self.key.quote;
|
7639
4524
|
self.key = make_node(AST_SymbolClassProperty, self.key, {
|
7640
4525
|
name: self.key.value
|
7641
4526
|
});
|
7642
4527
|
} else {
|
4528
|
+
self.quote = self.key.quote;
|
7643
4529
|
self.key = make_node(AST_SymbolMethod, self.key, {
|
7644
4530
|
name: self.key.value
|
7645
4531
|
});
|