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