[Pkg-javascript-commits] [uglifyjs] 01/50: fix variable substitution (#1816)

Jonas Smedegaard dr at jones.dk
Thu Aug 17 23:06:42 UTC 2017


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

js pushed a commit to branch master
in repository uglifyjs.

commit 43ad4e9775c6d0e4e9057c65cab302ef5aad638a
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Sun Apr 16 17:25:39 2017 +0800

    fix variable substitution (#1816)
    
    - let `collapse_vars` take care of value containing any symbols
    - improve overhead accounting
---
 lib/compress.js              | 38 +++++++++++++++++++++++++++-----
 test/compress/issue-1656.js  |  6 ++---
 test/compress/reduce_vars.js | 52 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 87 insertions(+), 9 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 1d9258c..d1a5242 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3595,7 +3595,9 @@ merge(Compressor.prototype, {
                 return make_node(AST_Infinity, self).optimize(compressor);
             }
         }
-        if (compressor.option("evaluate") && compressor.option("reduce_vars")) {
+        if (compressor.option("evaluate")
+            && compressor.option("reduce_vars")
+            && is_lhs(self, compressor.parent()) !== self) {
             var d = self.definition();
             var fixed = self.fixed_value();
             if (fixed) {
@@ -3603,21 +3605,45 @@ merge(Compressor.prototype, {
                     var init = fixed.evaluate(compressor);
                     if (init !== fixed) {
                         init = make_node_from_constant(init, fixed);
-                        var value = best_of_expression(init.optimize(compressor), fixed).print_to_string().length;
+                        var value = init.optimize(compressor).print_to_string().length;
+                        var fn;
+                        if (has_symbol_ref(fixed)) {
+                            fn = function() {
+                                var result = init.optimize(compressor);
+                                return result === init ? result.clone(true) : result;
+                            };
+                        } else {
+                            value = Math.min(value, fixed.print_to_string().length);
+                            fn = function() {
+                                var result = best_of_expression(init.optimize(compressor), fixed);
+                                return result === init || result === fixed ? result.clone(true) : result;
+                            };
+                        }
                         var name = d.name.length;
-                        var freq = d.references.length;
-                        var overhead = d.global || !freq ? 0 : (name + 2 + value) / freq;
-                        d.should_replace = value <= name + overhead ? init : false;
+                        var overhead = 0;
+                        if (compressor.option("unused") && (!d.global || compressor.option("toplevel"))) {
+                            overhead = (name + 2 + value) / d.references.length;
+                        }
+                        d.should_replace = value <= name + overhead ? fn : false;
                     } else {
                         d.should_replace = false;
                     }
                 }
                 if (d.should_replace) {
-                    return best_of_expression(d.should_replace.optimize(compressor), fixed).clone(true);
+                    return d.should_replace();
                 }
             }
         }
         return self;
+
+        function has_symbol_ref(value) {
+            var found;
+            value.walk(new TreeWalker(function(node) {
+                if (node instanceof AST_SymbolRef) found = true;
+                if (found) return true;
+            }));
+            return found;
+        }
     });
 
     function is_atomic(lhs, self) {
diff --git a/test/compress/issue-1656.js b/test/compress/issue-1656.js
index 8b683a2..c4c8f86 100644
--- a/test/compress/issue-1656.js
+++ b/test/compress/issue-1656.js
@@ -35,11 +35,11 @@ f7: {
         console.log(a, b);
     }
     expect_exact: [
-        "var a = 100, b = 10;",
+        "var b = 10;",
         "",
         "!function() {",
-        "    for (;b = a, !1; ) ;",
-        "}(), console.log(a, b);",
+        "    for (;b = 100, !1; ) ;",
+        "}(), console.log(100, b);",
     ]
     expect_stdout: true
 }
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index b6f711a..7621dd4 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -1995,3 +1995,55 @@ catch_var: {
     }
     expect_stdout: "true"
 }
+
+issue_1814_1: {
+    options = {
+        evaluate: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        const a = 42;
+        !function() {
+            var b = a;
+            !function(a) {
+                console.log(a++, b);
+            }(0);
+        }();
+    }
+    expect: {
+        const a = 42;
+        !function() {
+            !function(a) {
+                console.log(a++, 42);
+            }(0);
+        }();
+    }
+    expect_stdout: "0 42"
+}
+
+issue_1814_2: {
+    options = {
+        evaluate: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        const a = "32";
+        !function() {
+            var b = a + 1;
+            !function(a) {
+                console.log(a++, b);
+            }(0);
+        }();
+    }
+    expect: {
+        const a = "32";
+        !function() {
+            !function(a) {
+                console.log(a++, "321");
+            }(0);
+        }();
+    }
+    expect_stdout: "0 '321'"
+}

-- 
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