fix corner case in `side_effects` (#4009)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 1 Jul 2020 03:33:48 +0000 (04:33 +0100)
committerGitHub <noreply@github.com>
Wed, 1 Jul 2020 03:33:48 +0000 (11:33 +0800)
fixes #4008

lib/compress.js
test/compress/side_effects.js

index 10a84c6..2c8c776 100644 (file)
@@ -5303,10 +5303,7 @@ merge(Compressor.prototype, {
             return make_sequence(this, [ expression, property ]);
         });
         def(AST_SymbolRef, function(compressor) {
-            if (!this.is_declared(compressor)) return this;
-            var def = this.definition();
-            if (member(this, def.references)) def.replaced++;
-            return null;
+            return this.is_declared(compressor) ? null : this;
         });
         def(AST_This, return_null);
         def(AST_Unary, function(compressor, first_in_statement) {
@@ -5314,10 +5311,7 @@ merge(Compressor.prototype, {
                 this.write_only = !this.expression.has_side_effects(compressor);
                 return this;
             }
-            if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) {
-                this.expression.definition().replaced++;
-                return null;
-            }
+            if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) return null;
             var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
             if (first_in_statement && expression && is_iife_call(expression)) {
                 if (expression === this.expression && this.operator == "!") return this;
index 412a38c..244e388 100644 (file)
@@ -298,7 +298,7 @@ operator_in: {
     expect_stdout: "PASS"
 }
 
-issue_3983: {
+issue_3983_1: {
     options = {
         collapse_vars: true,
         conditionals: true,
@@ -323,7 +323,71 @@ issue_3983: {
     }
     expect: {
         var a = "PASS";
+        g();
+        function g() {}
         console.log(a);
     }
     expect_stdout: "PASS"
 }
+
+issue_3983_2: {
+    options = {
+        collapse_vars: true,
+        conditionals: true,
+        evaluate: true,
+        inline: true,
+        passes: 2,
+        reduce_vars: true,
+        side_effects: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var a = "PASS";
+        function f() {
+            g && g();
+        }
+        f();
+        function g() {
+            0 ? a : 0;
+        }
+        var b = a;
+        console.log(a);
+    }
+    expect: {
+        console.log("PASS");
+    }
+    expect_stdout: "PASS"
+}
+
+issue_4008: {
+    options = {
+        collapse_vars: true,
+        evaluate: true,
+        inline: true,
+        pure_getters: "strict",
+        reduce_vars: true,
+        side_effects: true,
+        toplevel: true,
+    }
+    input: {
+        var a = "PASS";
+        function f(b, b) {
+            console.log(b);
+        }
+        f && f(a && a[a]);
+        console.log(a);
+    }
+    expect: {
+        var a = "PASS";
+        function f(b, b) {
+            console.log(b);
+        }
+        f(a[a]);
+        console.log(a);
+    }
+    expect_stdout: [
+        "undefined",
+        "PASS",
+    ]
+}