fix corner case in `assignments` (#3430)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 29 May 2019 21:01:53 +0000 (05:01 +0800)
committerGitHub <noreply@github.com>
Wed, 29 May 2019 21:01:53 +0000 (05:01 +0800)
fixes #3429

lib/compress.js
test/compress/assignment.js

index 7bbb5a2..09d416c 100644 (file)
@@ -3856,7 +3856,7 @@ merge(Compressor.prototype, {
             if (!(rhs instanceof AST_Binary && lazy_op[rhs.operator])) return rhs;
             var sym = assign.left;
             if (!(sym instanceof AST_SymbolRef) || sym.name != rhs.left.name) return rhs;
-            return rhs.right;
+            return rhs.right.has_side_effects(compressor) ? rhs : rhs.right;
         }
 
         function scan_ref_scoped(node, descend) {
index ecd0ac4..5ca7440 100644 (file)
@@ -327,3 +327,49 @@ issue_3427: {
     }
     expect: {}
 }
+
+issue_3429_1: {
+    options = {
+        assignments: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        var a = "PASS";
+        (function(b) {
+            b && (b = a = "FAIL");
+        })();
+        console.log(a);
+    }
+    expect: {
+        var a = "PASS";
+        (function(b) {
+            b = b && (a = "FAIL");
+        })();
+        console.log(a);
+    }
+    expect_stdout: "PASS"
+}
+
+issue_3429_2: {
+    options = {
+        assignments: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        var a;
+        (function(b) {
+            b || (b = a = "FAIL");
+        })(42);
+        console.log(a);
+    }
+    expect: {
+        var a;
+        (function(b) {
+            b = b || (a = "FAIL");
+        })(42);
+        console.log(a);
+    }
+    expect_stdout: "undefined"
+}