[Pkg-javascript-commits] [uglifyjs] 112/228: make `collapse_vars` consistent with `toplevel` (#1608)

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:22 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 5ae04b35452693e886a7f836e62e3290b08016a1
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Thu Mar 16 13:22:26 2017 +0800

    make `collapse_vars` consistent with `toplevel` (#1608)
    
    fixes #1605
---
 lib/compress.js                |  4 +++-
 test/compress/collapse_vars.js | 47 +++++++++++++++++++++++++++++++++++++++++-
 test/compress/issue-973.js     |  1 +
 test/mocha/cli.js              |  2 +-
 test/mocha/minify.js           |  1 +
 5 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 49b618e..dac1f36 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -521,6 +521,7 @@ merge(Compressor.prototype, {
 
             var self = compressor.self();
             var var_defs_removed = false;
+            var toplevel = compressor.option("toplevel");
             for (var stat_index = statements.length; --stat_index >= 0;) {
                 var stat = statements[stat_index];
                 if (stat instanceof AST_Definitions) continue;
@@ -558,7 +559,8 @@ merge(Compressor.prototype, {
 
                     // Only interested in cases with just one reference to the variable.
                     var def = self.find_variable && self.find_variable(var_name);
-                    if (!def || !def.references || def.references.length !== 1 || var_name == "arguments") {
+                    if (!def || !def.references || def.references.length !== 1
+                        || var_name == "arguments" || (!toplevel && def.global)) {
                         side_effects_encountered = true;
                         continue;
                     }
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index 6d7e2d9..558a9ee 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -1152,7 +1152,8 @@ collapse_vars_arguments: {
     options = {
         collapse_vars:true, sequences:true, properties:true, dead_code:true, conditionals:true,
         comparisons:true, evaluate:true, booleans:true, loops:true, unused:true, hoist_funs:true,
-        keep_fargs:true, if_return:true, join_vars:true, cascade:true, side_effects:true
+        keep_fargs:true, if_return:true, join_vars:true, cascade:true, side_effects:true,
+        toplevel:true
     }
     input: {
         var outer = function() {
@@ -1335,6 +1336,7 @@ issue_1537: {
 issue_1562: {
     options = {
         collapse_vars: true,
+        toplevel: true,
     }
     input: {
         var v = 1, B = 2;
@@ -1363,3 +1365,46 @@ issue_1562: {
         for (; f(z + 2) ;) bar(30);
     }
 }
+
+issue_1605_1: {
+    options = {
+        collapse_vars: true,
+        toplevel: false,
+    }
+    input: {
+        function foo(x) {
+            var y = x;
+            return y;
+        }
+        var o = new Object;
+        o.p = 1;
+    }
+    expect: {
+        function foo(x) {
+            return x;
+        }
+        var o = new Object;
+        o.p = 1;
+    }
+}
+
+issue_1605_2: {
+    options = {
+        collapse_vars: true,
+        toplevel: "vars",
+    }
+    input: {
+        function foo(x) {
+            var y = x;
+            return y;
+        }
+        var o = new Object;
+        o.p = 1;
+    }
+    expect: {
+        function foo(x) {
+            return x;
+        }
+        (new Object).p = 1;
+    }
+}
diff --git a/test/compress/issue-973.js b/test/compress/issue-973.js
index 0e04092..30f886a 100644
--- a/test/compress/issue-973.js
+++ b/test/compress/issue-973.js
@@ -50,6 +50,7 @@ this_binding_conditionals: {
 this_binding_collapse_vars: {
     options = {
         collapse_vars: true,
+        toplevel: true,
     };
     input: {
         var c = a; c();
diff --git a/test/mocha/cli.js b/test/mocha/cli.js
index fa952d9..2b44c90 100644
--- a/test/mocha/cli.js
+++ b/test/mocha/cli.js
@@ -152,7 +152,7 @@ describe("bin/uglifyjs", function () {
        });
     });
     it("Should process inline source map", function(done) {
-        var command = uglifyjscmd + ' test/input/issue-520/input.js -cm toplevel --in-source-map inline --source-map-inline';
+        var command = uglifyjscmd + ' test/input/issue-520/input.js -mc toplevel --in-source-map inline --source-map-inline';
 
         exec(command, function (err, stdout) {
             if (err) throw err;
diff --git a/test/mocha/minify.js b/test/mocha/minify.js
index 51c46b2..a4587cb 100644
--- a/test/mocha/minify.js
+++ b/test/mocha/minify.js
@@ -78,6 +78,7 @@ describe("minify", function() {
         });
         it("Should process inline source map", function() {
             var code = Uglify.minify("./test/input/issue-520/input.js", {
+                compress: { toplevel: true },
                 inSourceMap: "inline",
                 sourceMapInline: true
             }).code + "\n";

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