fix corner case in `inline` (#3017)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 22 Mar 2018 15:46:26 +0000 (23:46 +0800)
committerGitHub <noreply@github.com>
Thu, 22 Mar 2018 15:46:26 +0000 (23:46 +0800)
fixes #3016

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

index f9fe3c6..fba88f4 100644 (file)
@@ -4774,7 +4774,8 @@ merge(Compressor.prototype, {
                     var name = var_def.name;
                     append_var(decls, expressions, name, var_def.value);
                     if (in_loop) {
-                        var def = name.definition();
+                        var def = fn.variables.get(name.name);
+                        if (def.orig[0] instanceof AST_SymbolFunarg) continue;
                         var sym = make_node(AST_SymbolRef, name, name);
                         def.references.push(sym);
                         expressions.splice(pos++, 0, make_node(AST_Assign, var_def, {
index ddf5fe0..bbe94eb 100644 (file)
@@ -2051,3 +2051,160 @@ drop_lone_use_strict: {
         }
     }
 }
+
+issue_3016_1: {
+    options = {
+        inline: true,
+        toplevel: true,
+    }
+    input: {
+        var b = 1;
+        do {
+            (function(a) {
+                return a[b];
+                var a;
+            })(3);
+        } while (0);
+        console.log(b);
+    }
+    expect: {
+        var b = 1;
+        do {
+            a = 3,
+            a[b];
+        } while(0);
+        var a;
+        console.log(b);
+    }
+    expect_stdout: "1"
+}
+
+issue_3016_2: {
+    options = {
+        dead_code: true,
+        inline: true,
+        toplevel: true,
+    }
+    input: {
+        var b = 1;
+        do {
+            (function(a) {
+                return a[b];
+                try {
+                    a = 2;
+                } catch (a) {
+                    var a;
+                }
+            })(3);
+        } while (0);
+        console.log(b);
+    }
+    expect: {
+        var b = 1;
+        do {
+            a = 3,
+            a[b];
+        } while(0);
+        var a;
+        console.log(b);
+    }
+    expect_stdout: "1"
+}
+
+issue_3016_2_ie8: {
+    options = {
+        dead_code: true,
+        ie8: true,
+        inline: true,
+        toplevel: true,
+    }
+    input: {
+        var b = 1;
+        do {
+            (function(a) {
+                return a[b];
+                try {
+                    a = 2;
+                } catch (a) {
+                    var a;
+                }
+            })(3);
+        } while (0);
+        console.log(b);
+    }
+    expect: {
+        var b = 1;
+        do {
+            a = 3,
+            a[b];
+        } while(0);
+        var a;
+        console.log(b);
+    }
+    expect_stdout: "1"
+}
+
+issue_3016_3: {
+    options = {
+        dead_code: true,
+        inline: true,
+        toplevel: true,
+    }
+    input: {
+        var b = 1;
+        do {
+            console.log(function() {
+                return a ? "FAIL" : a = "PASS";
+                try {
+                    a = 2;
+                } catch (a) {
+                    var a;
+                }
+            }());
+        } while (b--);
+    }
+    expect: {
+        var b = 1;
+        do {
+            console.log((a = void 0, a ? "FAIL" : a = "PASS"));
+        } while(b--);
+        var a;
+    }
+    expect_stdout: [
+        "PASS",
+        "PASS",
+    ]
+}
+
+issue_3016_3_ie8: {
+    options = {
+        dead_code: true,
+        ie8: true,
+        inline: true,
+        toplevel: true,
+    }
+    input: {
+        var b = 1;
+        do {
+            console.log(function() {
+                return a ? "FAIL" : a = "PASS";
+                try {
+                    a = 2;
+                } catch (a) {
+                    var a;
+                }
+            }());
+        } while (b--);
+    }
+    expect: {
+        var b = 1;
+        do {
+            console.log((a = void 0, a ? "FAIL" : a = "PASS"));
+        } while(b--);
+        var a;
+    }
+    expect_stdout: [
+        "PASS",
+        "PASS",
+    ]
+}