terser 5.7.1 → 5.7.2

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