fix corner cases in `inline` (#5265)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 4 Jan 2022 14:05:03 +0000 (14:05 +0000)
committerGitHub <noreply@github.com>
Tue, 4 Jan 2022 14:05:03 +0000 (22:05 +0800)
fixes #5263
fixes #5264

lib/compress.js
test/compress/functions.js

index 64ec5c4..87e0f96 100644 (file)
@@ -12999,6 +12999,7 @@ Compressor.prototype.compress = function(node) {
             });
             var body = [];
             fn.variables.each(function(def, name) {
+                if (name == "arguments") return;
                 names.set(name, true);
                 scope.enclosed.push(def);
                 scope.variables.set(name, def);
@@ -13007,9 +13008,10 @@ Compressor.prototype.compress = function(node) {
                 if (fn.functions.has(name) && def.orig.length == 1) return;
                 if (def.references.length == def.replaced) return;
                 if (!all(def.orig, function(sym) {
-                    return !(sym instanceof AST_SymbolConst
-                        || sym instanceof AST_SymbolFunarg
-                        || sym instanceof AST_SymbolLet);
+                    if (sym instanceof AST_SymbolConst) return false;
+                    if (sym instanceof AST_SymbolFunarg) return def.scope.resolve() !== fn;
+                    if (sym instanceof AST_SymbolLet) return false;
+                    return true;
                 })) return;
                 var sym = def.orig[0];
                 var ref = make_node(AST_SymbolRef, sym, flatten_var(sym));
index a56e107..a196f79 100644 (file)
@@ -7875,3 +7875,105 @@ issue_5254_2: {
         "undefined",
     ]
 }
+
+issue_5263: {
+    options = {
+        inline: true,
+        side_effects: true,
+        toplevel: true,
+    }
+    input: {
+        for (var i = 0; i < 2; i++) (function() {
+            while (console.log(i));
+            (function(a) {
+                console.log(a) && a,
+                a++;
+            })();
+        })();
+    }
+    expect: {
+        for (var i = 0; i < 2; i++) {
+            a = void 0;
+            while (console.log(i));
+            console.log(a),
+            a++;
+            var a;
+        }
+    }
+    expect_stdout: [
+        "0",
+        "undefined",
+        "1",
+        "undefined",
+    ]
+}
+
+issue_5264_1: {
+    options = {
+        if_return: true,
+        inline: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        console.log(function() {
+            function f(arguments) {
+                console.log(arguments);
+                (function() {
+                    while (console.log("foo"));
+                })();
+            }
+            f("bar");
+            return arguments;
+        }("baz")[0]);
+    }
+    expect: {
+        console.log(function() {
+            (function(arguments) {
+                console.log(arguments);
+                while (console.log("foo"));
+            })("bar");
+            return arguments;
+        }("baz")[0]);
+    }
+    expect_stdout: [
+        "bar",
+        "foo",
+        "baz",
+    ]
+}
+
+issue_5264_2: {
+    options = {
+        inline: true,
+        reduce_vars: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        console.log(function() {
+            function f(arguments) {
+                console.log(arguments);
+                (function() {
+                    while (console.log("foo"));
+                })();
+            }
+            f("bar");
+            return arguments;
+        }("baz")[0]);
+    }
+    expect: {
+        console.log(function() {
+            (function(arguments) {
+                console.log(arguments);
+                while (console.log("foo"));
+            })("bar");
+            return arguments;
+        }("baz")[0]);
+    }
+    expect_stdout: [
+        "bar",
+        "foo",
+        "baz",
+    ]
+}