fix corner case in `rests` (#4576)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 20 Jan 2021 23:23:06 +0000 (23:23 +0000)
committerGitHub <noreply@github.com>
Wed, 20 Jan 2021 23:23:06 +0000 (07:23 +0800)
fixes #4575

lib/compress.js
test/compress/rests.js

index 4710b69..fc1bc11 100644 (file)
@@ -7845,11 +7845,11 @@ merge(Compressor.prototype, {
         if (fn.rest) {
             if (!(is_iife && compressor.option("rests"))) return;
             var insert = fn.argnames.length;
-            for (var i = args.length; i < insert; i++) {
-                args[i] = make_node(AST_Undefined, call).optimize(compressor);
-            }
-            args[insert] = make_node(AST_Array, call, { elements: args.splice(insert) });
-            fn.argnames.push(fn.rest);
+            args = args.slice(0, insert);
+            while (args.length < insert) args.push(make_node(AST_Undefined, call).optimize(compressor));
+            args.push(make_node(AST_Array, call, { elements: call.args.slice(insert) }));
+            call.args = args;
+            fn.argnames = fn.argnames.concat(fn.rest);
             fn.rest = null;
         }
         var pos = 0, last = 0;
index 017f871..42e6295 100644 (file)
@@ -663,3 +663,36 @@ issue_4562: {
     expect_stdout: "f"
     node_version: ">=6"
 }
+
+issue_4575: {
+    options = {
+        collapse_vars: true,
+        ie8: true,
+        reduce_vars: true,
+        rests: true,
+        unused: true,
+    }
+    input: {
+        A = "PASS";
+        (function() {
+            var a = 0, b = a;
+            var c = function a(...b) {
+                A;
+                var d = A;
+                console.log(d, b.length);
+            }();
+        })();
+    }
+    expect: {
+        A = "PASS";
+        (function() {
+            (function(b) {
+                A;
+                var d = A;
+                console.log(d, b.length);
+            })([]);
+        })();
+    }
+    expect_stdout: "PASS 0"
+    node_version: ">=6"
+}