fix argument/atom collision by `collapse_vars` (#2507)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 23 Nov 2017 23:26:22 +0000 (07:26 +0800)
committerGitHub <noreply@github.com>
Thu, 23 Nov 2017 23:26:22 +0000 (07:26 +0800)
fixes #2506

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

index ce881c0..d8a0bd7 100644 (file)
@@ -806,6 +806,12 @@ merge(Compressor.prototype, {
         }
     }
 
+    function is_identifier_atom(node) {
+        return node instanceof AST_Infinity
+            || node instanceof AST_NaN
+            || node instanceof AST_Undefined;
+    }
+
     function tighten_body(statements, compressor) {
         var CHANGED, max_iter = 10;
         do {
@@ -890,14 +896,19 @@ merge(Compressor.prototype, {
                             return node;
                         }
                         var def = candidate.name.definition();
+                        var value = candidate.value;
                         if (def.references.length - def.replaced == 1 && !compressor.exposed(def)) {
                             def.replaced++;
-                            return maintain_this_binding(parent, node, candidate.value);
+                            if (funarg && is_identifier_atom(value)) {
+                                return value.transform(compressor);
+                            } else {
+                                return maintain_this_binding(parent, node, value);
+                            }
                         }
                         return make_node(AST_Assign, candidate, {
                             operator: "=",
                             left: make_node(AST_SymbolRef, candidate.name, candidate.name),
-                            right: candidate.value
+                            right: value
                         });
                     }
                     candidate.write_only = false;
@@ -971,7 +982,8 @@ merge(Compressor.prototype, {
                         replace_all = def.references.length - def.replaced == 1;
                     }
                     var side_effects = value_has_side_effects(candidate);
-                    var hit = candidate.name instanceof AST_SymbolFunarg;
+                    var funarg = candidate.name instanceof AST_SymbolFunarg;
+                    var hit = funarg;
                     var abort = false, replaced = 0, can_replace = !args || !hit;
                     if (!can_replace) {
                         for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; !abort && j < args.length; j++) {
@@ -987,7 +999,7 @@ merge(Compressor.prototype, {
                         if (abort && def.references.length - def.replaced > replaced) replaced = false;
                         else {
                             abort = false;
-                            hit = candidate.name instanceof AST_SymbolFunarg;
+                            hit = funarg;
                             for (var i = stat_index; !abort && i < statements.length; i++) {
                                 statements[i].transform(multi_replacer);
                             }
@@ -3810,9 +3822,7 @@ merge(Compressor.prototype, {
         if (self.operator == "delete"
             && !(e instanceof AST_SymbolRef
                 || e instanceof AST_PropAccess
-                || e instanceof AST_NaN
-                || e instanceof AST_Infinity
-                || e instanceof AST_Undefined)) {
+                || is_identifier_atom(e))) {
             if (e instanceof AST_Sequence) {
                 e = e.expressions.slice();
                 e.push(make_node(AST_True, self));
index e89d44d..844d5b0 100644 (file)
@@ -3618,3 +3618,41 @@ issue_2497: {
         }
     }
 }
+
+issue_2506: {
+    options = {
+        collapse_vars: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        var c = 0;
+        function f0(bar) {
+            function f1(Infinity_2) {
+                function f13(NaN) {
+                    if (false <= NaN & this >> 1 >= 0) {
+                        c++;
+                    }
+                }
+                var b_2 = f13(NaN, c++);
+            }
+            var bar = f1(-3, -1);
+        }
+        f0(false);
+        console.log(c);
+    }
+    expect: {
+        var c = 0;
+        function f0(bar) {
+            (function(Infinity_2) {
+                (function(NaN) {
+                    if (false <= 0/0 & this >> 1 >= 0)
+                        c++;
+                })(0, c++);
+            })();
+        }
+        f0(false);
+        console.log(c);
+    }
+    expect_stdout: "1"
+}