[Pkg-javascript-commits] [uglifyjs] 357/491: fix corner cases with `collapse_vars`, `inline` & `reduce_vars` (#2637)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:52 UTC 2018


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.

commit 202f90ef8f2b282fbd5c063a7e5a34f79551099e
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Sun Dec 24 01:24:12 2017 +0800

    fix corner cases with `collapse_vars`, `inline` & `reduce_vars` (#2637)
    
    fixes #2630
---
 lib/compress.js            |   6 +-
 test/compress/functions.js | 165 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 169 insertions(+), 2 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 5328b51..6b2c936 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1089,6 +1089,7 @@ merge(Compressor.prototype, {
                             for (var i = stat_index; !abort && i < statements.length; i++) {
                                 statements[i].transform(multi_replacer);
                             }
+                            value_def.single_use = false;
                         }
                     }
                     if (replaced && !remove_candidate(candidate)) statements.splice(stat_index, 1);
@@ -3958,13 +3959,14 @@ merge(Compressor.prototype, {
                 && (exp === fn ? !fn.name
                     : compressor.option("unused")
                         && (def = exp.definition()).references.length == 1
-                        && !recursive_ref(compressor, def))
+                        && !recursive_ref(compressor, def)
+                        && fn.is_constant_expression(exp.scope))
                 && !self.has_pure_annotation(compressor)
                 && !fn.contains_this()
                 && (scope = can_flatten_args(fn))
                 && (value = flatten_body(stat))) {
                 var expressions = flatten_args(fn, scope);
-                expressions.push(value);
+                expressions.push(value.clone(true));
                 return make_sequence(self, expressions).optimize(compressor);
             }
             if (compressor.option("side_effects") && all(fn.body, is_empty)) {
diff --git a/test/compress/functions.js b/test/compress/functions.js
index 4c22652..0714766 100644
--- a/test/compress/functions.js
+++ b/test/compress/functions.js
@@ -1258,3 +1258,168 @@ issue_2620_4: {
     }
     expect_stdout: "PASS"
 }
+
+issue_2630_1: {
+    options = {
+        collapse_vars: true,
+        inline: true,
+        passes: 2,
+        reduce_funcs: true,
+        reduce_vars: true,
+        sequences: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        var c = 0;
+        (function() {
+            while (f());
+            function f() {
+                var a = function() {
+                    var b = c++, d = c = 1 + c;
+                }();
+            }
+        })();
+        console.log(c);
+    }
+    expect: {
+        var c = 0;
+        (function() {
+            while (c++, void (c = 1 + c));
+        })(),
+        console.log(c);
+    }
+    expect_stdout: "2"
+}
+
+issue_2630_2: {
+    options = {
+        collapse_vars: true,
+        inline: true,
+        passes: 2,
+        reduce_vars: true,
+        sequences: true,
+        unused: true,
+    }
+    input: {
+        var c = 0;
+        !function() {
+            while (f()) {}
+            function f() {
+                var not_used = function() {
+                    c = 1 + c;
+                }(c = c + 1);
+            }
+        }();
+        console.log(c);
+    }
+    expect: {
+        var c = 0;
+        !function() {
+            while (c += 1, void (c = 1 + c));
+        }(), console.log(c);
+    }
+    expect_stdout: "2"
+}
+
+issue_2630_3: {
+    options = {
+        inline: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        var x = 2, a = 1;
+        (function() {
+            function f1(a) {
+                f2();
+                --x >= 0 && f1({});
+            }
+            f1(a++);
+            function f2() {
+                a++;
+            }
+        })();
+        console.log(a);
+    }
+    expect: {
+        var x = 2, a = 1;
+        (function() {
+            function f1(a) {
+                f2();
+                --x >= 0 && f1({});
+            }
+            f1(a++);
+            function f2() {
+                a++;
+            }
+        })();
+        console.log(a);
+    }
+    expect_stdout: "5"
+}
+
+issue_2630_4: {
+    options = {
+        collapse_vars: true,
+        inline: true,
+        reduce_vars: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        var x = 3, a = 1, b = 2;
+        (function() {
+            (function f1() {
+                while (--x >= 0 && f2());
+            }());
+            function f2() {
+                a++ + (b += a);
+            }
+        })();
+        console.log(a);
+    }
+    expect: {
+        var x = 3, a = 1, b = 2;
+        (function() {
+            (function() {
+                while (--x >= 0 && void (a++, b += a));
+            })();
+        })();
+        console.log(a);
+    }
+    expect_stdout: "2"
+}
+
+issue_2630_5: {
+    options = {
+        collapse_vars: true,
+        inline: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        var c = 1;
+        !function() {
+            do {
+                c *= 10;
+            } while (f());
+            function f() {
+                return function() {
+                    return (c = 2 + c) < 100;
+                }(c = c + 3);
+            }
+        }();
+        console.log(c);
+    }
+    expect: {
+        var c = 1;
+        !function() {
+            do {
+                c *= 10;
+            } while (c += 3, (c = 2 + c) < 100);
+        }();
+        console.log(c);
+    }
+    expect_stdout: "155"
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list