fix `collapse_vars` on default function argument (#2299)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 3 Sep 2017 18:32:33 +0000 (02:32 +0800)
committerGitHub <noreply@github.com>
Sun, 3 Sep 2017 18:32:33 +0000 (02:32 +0800)
Avoid collision with local variable `undefined` under certain corner cases.

fixes #2298

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

index b6a984c..6e766fb 100644 (file)
@@ -847,7 +847,7 @@ merge(Compressor.prototype, {
                         if (sym.name in names) continue;
                         names[sym.name] = true;
                         var arg = iife.args[i];
-                        if (!arg) arg = make_node(AST_Undefined, sym);
+                        if (!arg) arg = make_node(AST_Undefined, sym).transform(compressor);
                         else {
                             var tw = new TreeWalker(function(node) {
                                 if (!arg) return true;
index 7686add..72123d4 100644 (file)
@@ -2342,3 +2342,45 @@ duplicate_argname: {
     }
     expect_stdout: "PASS"
 }
+
+issue_2298: {
+    options = {
+        collapse_vars: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        !function() {
+            function f() {
+                var a = undefined;
+                var undefined = a++;
+                try {
+                    !function g(b) {
+                        b[1] = "foo";
+                    }();
+                    console.log("FAIL");
+                } catch (e) {
+                    console.log("PASS");
+                }
+            }
+            f();
+        }();
+    }
+    expect: {
+        !function() {
+            (function() {
+                var a = undefined;
+                var undefined = a++;
+                try {
+                    !function(b) {
+                        (void 0)[1] = "foo";
+                    }();
+                    console.log("FAIL");
+                } catch (e) {
+                    console.log("PASS");
+                }
+            })();
+        }();
+    }
+    expect_stdout: "PASS"
+}