better fix for #3113 (#3115)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 3 May 2018 07:51:51 +0000 (15:51 +0800)
committerGitHub <noreply@github.com>
Thu, 3 May 2018 07:51:51 +0000 (15:51 +0800)
lib/compress.js
test/compress/reduce_vars.js

index 99b7782..6029318 100644 (file)
@@ -361,8 +361,8 @@ merge(Compressor.prototype, {
             def.single_use = undefined;
         }
 
-        function reset_variables(tw, compressor, node) {
-            node.variables.each(function(def) {
+        function reset_variables(tw, compressor, scope) {
+            scope.variables.each(function(def) {
                 reset_def(compressor, def);
                 if (def.fixed === null) {
                     def.safe_ids = tw.safe_ids;
@@ -374,9 +374,27 @@ merge(Compressor.prototype, {
             });
         }
 
-        function walk_defuns(tw, compressor, node) {
-            node.functions.each(function(def) {
-                if (def.init instanceof AST_Defun && !(def.id in tw.defun_ids)) {
+        function same_defun_scope(def, ref) {
+            var scope = ref.scope;
+            do {
+                if (def.scope === scope) return true;
+            } while (scope instanceof AST_Function && (scope = scope.parent_scope));
+        }
+
+        function walk_defun(tw, ref) {
+            var def = ref.definition();
+            if (tw.in_loop || !same_defun_scope(def, ref)) {
+                if (tw.defun_ids[def.id] !== false) tw.defun_ids[def.id] = undefined;
+                return;
+            }
+            if (def.id in tw.defun_ids) return;
+            tw.defun_ids[def.id] = true;
+            def.fixed.walk(tw);
+        }
+
+        function walk_defuns(tw, scope) {
+            scope.functions.each(function(def) {
+                if (def.init instanceof AST_Defun && tw.defun_ids[def.id] === undefined) {
                     tw.defun_ids[def.id] = true;
                     def.init.walk(tw);
                 }
@@ -480,7 +498,7 @@ merge(Compressor.prototype, {
             reset_variables(tw, compressor, this);
             descend();
             pop(tw);
-            walk_defuns(tw, compressor, this);
+            walk_defuns(tw, this);
             return true;
         });
         def(AST_Assign, function(tw) {
@@ -515,6 +533,20 @@ merge(Compressor.prototype, {
             pop(tw);
             return true;
         });
+        def(AST_Call, function(tw, descend) {
+            var exp = this.expression;
+            if (!(exp instanceof AST_SymbolRef)) return;
+            var def = exp.definition();
+            if (!(def.fixed instanceof AST_Defun)) return;
+            if (def.id in tw.defun_ids) return;
+            tw.defun_ids[def.id] = 0;
+            descend();
+            if (tw.defun_ids[def.id] === 0) {
+                delete tw.defun_ids[def.id];
+                walk_defun(tw, exp);
+            }
+            return true;
+        });
         def(AST_Case, function(tw) {
             push(tw);
             this.expression.walk(tw);
@@ -549,7 +581,7 @@ merge(Compressor.prototype, {
             reset_variables(tw, compressor, this);
             descend();
             pop(tw);
-            walk_defuns(tw, compressor, this);
+            walk_defuns(tw, this);
             return true;
         });
         def(AST_Do, function(tw) {
@@ -622,7 +654,7 @@ merge(Compressor.prototype, {
             }
             descend();
             pop(tw);
-            walk_defuns(tw, compressor, node);
+            walk_defuns(tw, node);
             return true;
         });
         def(AST_If, function(tw) {
@@ -676,10 +708,7 @@ merge(Compressor.prototype, {
                 }
             }
             mark_escaped(tw, d, this.scope, this, value, 0, 1);
-            if (d.scope === this.scope && !tw.in_loop && d.fixed instanceof AST_Defun && !(d.id in tw.defun_ids)) {
-                tw.defun_ids[d.id] = true;
-                d.fixed.walk(tw);
-            }
+            if (d.fixed instanceof AST_Defun) walk_defun(tw, this);
         });
         def(AST_Toplevel, function(tw, descend, compressor) {
             this.globals.each(function(def) {
@@ -689,7 +718,7 @@ merge(Compressor.prototype, {
             reset_variables(tw, compressor, this);
             descend();
             pop(tw);
-            walk_defuns(tw, compressor, this);
+            walk_defuns(tw, this);
             return true;
         });
         def(AST_Try, function(tw) {
index b21b760..c4308d3 100644 (file)
@@ -5834,7 +5834,7 @@ issue_3110_3: {
     ]
 }
 
-issue_3113: {
+issue_3113_1: {
     options = {
         evaluate: true,
         reduce_vars: true,
@@ -5869,3 +5869,73 @@ issue_3113: {
     }
     expect_stdout: "1"
 }
+
+issue_3113_2: {
+    options = {
+        evaluate: true,
+        reduce_vars: true,
+    }
+    input: {
+        var c = 0;
+        (function() {
+            function f() {
+                while (g());
+            }
+            var a = f();
+            function g() {
+                a && a[c++];
+            }
+            a = 1;
+            g();
+        })();
+        console.log(c);
+    }
+    expect: {
+        var c = 0;
+        (function() {
+            function f() {
+                while (g());
+            }
+            var a = f();
+            function g() {
+                a && a[c++];
+            }
+            a = 1;
+            g();
+        })();
+        console.log(c);
+    }
+    expect_stdout: "1"
+}
+
+issue_3113_3: {
+    options = {
+        evaluate: true,
+        inline: true,
+        passes: 2,
+        pure_getters: "strict",
+        reduce_vars: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        var c = 0;
+        (function() {
+            function f() {
+                while (g());
+            }
+            var a;
+            function g() {
+                a && a[c++];
+            }
+            g(a = 1);
+        })();
+        console.log(c);
+    }
+    expect: {
+        var c = 0;
+        c++;
+        console.log(c);
+    }
+    expect_stdout: "1"
+}