fix corner case in `reduce_vars` (#3775)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 10 Apr 2020 18:19:38 +0000 (19:19 +0100)
committerGitHub <noreply@github.com>
Fri, 10 Apr 2020 18:19:38 +0000 (02:19 +0800)
fixes #3774

lib/compress.js
test/compress/reduce_vars.js

index 94b7667..e9f8373 100644 (file)
@@ -318,7 +318,7 @@ merge(Compressor.prototype, {
         if (value instanceof AST_RegExp) return native_fns.RegExp[name] && !value.value.global;
     }
 
-    function is_modified(compressor, tw, node, value, level, immutable) {
+    function is_modified(compressor, tw, node, value, level, immutable, recursive) {
         var parent = tw.parent(level);
         if (compressor.option("unsafe") && parent instanceof AST_Dot && is_read_only_fn(value, parent.property)) {
             return;
@@ -342,7 +342,7 @@ merge(Compressor.prototype, {
         }
         if (parent instanceof AST_PropAccess && parent.expression === node) {
             var prop = read_property(value, parent);
-            return !immutable && is_modified(compressor, tw, parent, prop, level + 1);
+            return (!immutable || recursive) && is_modified(compressor, tw, parent, prop, level + 1);
         }
     }
 
@@ -759,7 +759,8 @@ merge(Compressor.prototype, {
                 d.fixed = false;
             } else if (d.fixed) {
                 value = this.fixed_value();
-                if (recursive_ref(tw, d)) {
+                var recursive = recursive_ref(tw, d);
+                if (recursive) {
                     d.recursive_refs++;
                 } else if (value && ref_once(tw, compressor, d)) {
                     d.single_use = value instanceof AST_Lambda && !value.pinned()
@@ -767,7 +768,7 @@ merge(Compressor.prototype, {
                 } else {
                     d.single_use = false;
                 }
-                if (is_modified(compressor, tw, this, value, 0, is_immutable(value))) {
+                if (is_modified(compressor, tw, this, value, 0, is_immutable(value), recursive)) {
                     if (d.single_use) {
                         d.single_use = "m";
                     } else {
index 85ca6bd..4e11de4 100644 (file)
@@ -6510,17 +6510,17 @@ issue_3240_3: {
     }
     expect: {
         (function() {
-            (function f(b) {
+            f();
+            function f(b) {
                 if (!f.a) f.a = 0;
                 console.log(f.a.toString());
-                var g = function() {
+                (function() {
                     (b ? function() {} : function() {
                         f.a++;
                         f(1);
                     })();
-                };
-                g();
-            })();
+                })();
+            }
         })();
     }
     expect_stdout: [
@@ -6554,7 +6554,8 @@ issue_3240_4: {
     }
     expect: {
         (function() {
-            (function f(b) {
+            f();
+            function f(b) {
                 if (!f.a) f.a = 0;
                 console.log(f.a.toString());
                 (function() {
@@ -6563,7 +6564,7 @@ issue_3240_4: {
                         f(1);
                     })();
                 })();
-            })();
+            }
         })();
     }
     expect_stdout: [
@@ -6872,3 +6873,42 @@ issue_3666: {
     }
     expect_stdout: "PASS PASS"
 }
+
+issue_3774: {
+    options = {
+        reduce_funcs: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        var f = function() {
+            function g() {
+                if (!g.p) {
+                    g.p = 1;
+                    console.log("PASS");
+                }
+            }
+            return function() {
+                g();
+            };
+        }();
+        f();
+        f();
+    }
+    expect: {
+        var f = function() {
+            function g() {
+                if (!g.p) {
+                    g.p = 1;
+                    console.log("PASS");
+                }
+            }
+            return function() {
+                g();
+            };
+        }();
+        f();
+        f();
+    }
+    expect_stdout: "PASS"
+}