fix corner case in `collapse_vars` (#5274)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 7 Jan 2022 07:00:23 +0000 (07:00 +0000)
committerGitHub <noreply@github.com>
Fri, 7 Jan 2022 07:00:23 +0000 (15:00 +0800)
fixes #5273

lib/compress.js
test/compress/collapse_vars.js

index 32cd445..2784bbe 100644 (file)
@@ -1962,21 +1962,27 @@ Compressor.prototype.compress = function(node) {
                     can_replace = false;
                     node.right.transform(scanner);
                     clear_write_only(candidate);
-                    var assign = make_node(AST_Assign, node, {
+                    var folded;
+                    if (abort) {
+                        folded = candidate;
+                    } else {
+                        abort = true;
+                        lhs.definition().fixed = false;
+                        folded = make_node(AST_Binary, candidate, {
+                            operator: compound,
+                            left: lhs,
+                            right: rvalue,
+                        });
+                    }
+                    return make_node(AST_Assign, node, {
                         operator: "=",
                         left: node.left,
                         right: make_node(AST_Binary, node, {
                             operator: node.operator.slice(0, -1),
-                            left: abort ? candidate : make_node(AST_Binary, candidate, {
-                                operator: compound,
-                                left: lhs,
-                                right: rvalue,
-                            }),
+                            left: folded,
                             right: node.right,
                         }),
                     });
-                    abort = true;
-                    return assign;
                 }
                 // Stop immediately if these node types are encountered
                 if (should_stop(node, parent)) {
index e05b3d4..e3c6468 100644 (file)
@@ -9692,3 +9692,32 @@ issue_5182: {
     ]
     node_version: ">=4"
 }
+
+issue_5273: {
+    options = {
+        collapse_vars: true,
+        evaluate: true,
+        inline: true,
+        reduce_vars: true,
+        sequences: true,
+        toplevel: true,
+    }
+    input: {
+        var a = "10", b = 1;
+        function f(c, d) {
+            return d;
+        }
+        f((b += a, b *= a), f);
+        console.log(b);
+    }
+    expect: {
+        var a = "10", b = 1;
+        function f(c, d) {
+            return d;
+        }
+        b = (b + a) * a,
+        f,
+        console.log(b);
+    }
+    expect_stdout: "1100"
+}