fix corner case in `evaluate` (#5139)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 30 Sep 2021 18:52:21 +0000 (19:52 +0100)
committerGitHub <noreply@github.com>
Thu, 30 Sep 2021 18:52:21 +0000 (02:52 +0800)
fixes #5138

lib/compress.js
test/compress/default-values.js

index 4657de2..2aa2bca 100644 (file)
@@ -4805,10 +4805,11 @@ merge(Compressor.prototype, {
                 if (!all(fn.argnames, function(sym, index) {
                     if (sym instanceof AST_DefaultValue) {
                         if (!args) return false;
-                        if (args[index] !== undefined) return false;
-                        var value = sym.value._eval(compressor, ignore_side_effects, cached, depth);
-                        if (value === sym.value) return false;
-                        args[index] = value;
+                        if (args[index] === undefined) {
+                            var value = sym.value._eval(compressor, ignore_side_effects, cached, depth);
+                            if (value === sym.value) return false;
+                            args[index] = value;
+                        }
                         sym = sym.name;
                     }
                     return !(sym instanceof AST_Destructured);
@@ -4842,6 +4843,9 @@ merge(Compressor.prototype, {
                 if (!args || all(fn.argnames, function(sym, i) {
                     return assign(sym, args[i]);
                 }) && !(fn.rest && !assign(fn.rest, args.slice(fn.argnames.length))) || ignore_side_effects) {
+                    if (ignore_side_effects) fn.argnames.forEach(function(sym) {
+                        if (sym instanceof AST_DefaultValue) sym.value.walk(scan_modified);
+                    });
                     fn.evaluating = true;
                     val = val._eval(compressor, ignore_side_effects, cached, depth);
                     delete fn.evaluating;
index 13d4a18..3a4d164 100644 (file)
@@ -1904,3 +1904,37 @@ issue_5065: {
     expect_stdout: "PASS"
     node_version: ">=6"
 }
+
+issue_5138_1: {
+    options = {
+        evaluate: true,
+    }
+    input: {
+        console.log(function(a, b = a = "FAIL") {
+            return a;
+        }() && "PASS");
+    }
+    expect: {
+        console.log(function(a, b = a = "FAIL") {
+            return a;
+        }() && "PASS");
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}
+
+issue_5138_2: {
+    options = {
+        evaluate: true,
+    }
+    input: {
+        console.log(function(a, b = a = "FAIL 1") {
+            return a;
+        }(null, "FAIL 2") || "PASS");
+    }
+    expect: {
+        console.log((null, "PASS"));
+    }
+    expect_stdout: "PASS"
+    node_version: ">=6"
+}