[Pkg-javascript-commits] [uglifyjs] 61/228: optimize trivial IIFEs returning constants (#1530)

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:17 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 ee3b39b909c279a0115c7562cab2c9b37fb37c21
Author: kzc <kzc at users.noreply.github.com>
Date:   Thu Mar 2 02:11:40 2017 -0500

    optimize trivial IIFEs returning constants (#1530)
---
 lib/compress.js                |  6 ++++
 test/compress/collapse_vars.js |  2 +-
 test/compress/functions.js     | 68 ++++++++++++++++++++++++++++++++++++++++++
 test/compress/negate-iife.js   | 28 ++++++++---------
 test/mocha/minify.js           |  4 +--
 5 files changed, 91 insertions(+), 17 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index ddf2b10..9923598 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2690,6 +2690,12 @@ merge(Compressor.prototype, {
                 }
             }
         }
+        if (self.args.length == 0
+            && self.expression instanceof AST_Function
+            && self.expression.body[0] instanceof AST_Return
+            && self.expression.body[0].value.is_constant()) {
+            return self.expression.body[0].value;
+        }
         if (compressor.option("negate_iife")
             && compressor.parent() instanceof AST_SimpleStatement
             && is_iife_call(self)) {
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index d7432f3..5f63488 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -1141,7 +1141,7 @@ collapse_vars_constants: {
         function f3(x) {
             var b = x.prop;
             sideeffect1();
-            return b + (function() { return -9; })();
+            return b + -9;
         }
     }
 }
diff --git a/test/compress/functions.js b/test/compress/functions.js
index 3a8701b..d3d99b5 100644
--- a/test/compress/functions.js
+++ b/test/compress/functions.js
@@ -6,3 +6,71 @@ non_ascii_function_identifier_name: {
     }
     expect_exact: "function fooλ(δλ){}function λ(δλ){}(function λ(δλ){})();"
 }
+
+iifes_returning_constants_keep_fargs_true: {
+    options = {
+        keep_fargs    : true,
+        side_effects  : true,
+        evaluate      : true,
+        unused        : true,
+        dead_code     : true,
+        conditionals  : true,
+        comparisons   : true,
+        booleans      : true,
+        if_return     : true,
+        join_vars     : true,
+        reduce_vars   : true,
+        cascade       : true,
+    }
+    input: {
+        (function(){ return -1.23; }());
+        console.log( function foo(){ return "okay"; }() );
+        console.log( function foo(x, y, z){ return 123; }() );
+        console.log( function(x, y, z){ return z; }() );
+        console.log( function(x, y, z){ if (x) return y; return z; }(1, 2, 3) );
+        console.log( function(x, y){ return x * y; }(2, 3) );
+        console.log( function(x, y){ return x * y; }(2, 3, a(), b()) );
+    }
+    expect: {
+        console.log("okay");
+        console.log(123);
+        console.log(void 0);
+        console.log(function(x,y,z){return 2}(1,2,3));
+        console.log(function(x,y){return 6}(2,3));
+        console.log(function(x, y){return 6}(2,3,a(),b()));
+    }
+}
+
+iifes_returning_constants_keep_fargs_false: {
+    options = {
+        keep_fargs    : false,
+        side_effects  : true,
+        evaluate      : true,
+        unused        : true,
+        dead_code     : true,
+        conditionals  : true,
+        comparisons   : true,
+        booleans      : true,
+        if_return     : true,
+        join_vars     : true,
+        reduce_vars   : true,
+        cascade       : true,
+    }
+    input: {
+        (function(){ return -1.23; }());
+        console.log( function foo(){ return "okay"; }() );
+        console.log( function foo(x, y, z){ return 123; }() );
+        console.log( function(x, y, z){ return z; }() );
+        console.log( function(x, y, z){ if (x) return y; return z; }(1, 2, 3) );
+        console.log( function(x, y){ return x * y; }(2, 3) );
+        console.log( function(x, y){ return x * y; }(2, 3, a(), b()) );
+    }
+    expect: {
+        console.log("okay");
+        console.log(123);
+        console.log(void 0);
+        console.log(2);
+        console.log(6);
+        console.log(function(){return 6}(a(),b()));
+    }
+}
diff --git a/test/compress/negate-iife.js b/test/compress/negate-iife.js
index 001795c..f17ae20 100644
--- a/test/compress/negate-iife.js
+++ b/test/compress/negate-iife.js
@@ -38,10 +38,10 @@ negate_iife_3: {
         conditionals: true
     };
     input: {
-        (function(){ return true })() ? console.log(true) : console.log(false);
+        (function(){ return t })() ? console.log(true) : console.log(false);
     }
     expect: {
-        !function(){ return true }() ? console.log(false) : console.log(true);
+        !function(){ return t }() ? console.log(false) : console.log(true);
     }
 }
 
@@ -51,10 +51,10 @@ negate_iife_3_off: {
         conditionals: true,
     };
     input: {
-        (function(){ return true })() ? console.log(true) : console.log(false);
+        (function(){ return t })() ? console.log(true) : console.log(false);
     }
     expect: {
-        !function(){ return true }() ? console.log(false) : console.log(true);
+        !function(){ return t }() ? console.log(false) : console.log(true);
     }
 }
 
@@ -65,13 +65,13 @@ negate_iife_4: {
         sequences: true
     };
     input: {
-        (function(){ return true })() ? console.log(true) : console.log(false);
+        (function(){ return t })() ? console.log(true) : console.log(false);
         (function(){
             console.log("something");
         })();
     }
     expect: {
-        !function(){ return true }() ? console.log(false) : console.log(true), function(){
+        !function(){ return t }() ? console.log(false) : console.log(true), function(){
             console.log("something");
         }();
     }
@@ -86,7 +86,7 @@ sequence_off: {
     };
     input: {
         function f() {
-            (function(){ return true })() ? console.log(true) : console.log(false);
+            (function(){ return t })() ? console.log(true) : console.log(false);
             (function(){
                 console.log("something");
             })();
@@ -95,19 +95,19 @@ sequence_off: {
             (function(){
                 console.log("something");
             })();
-            (function(){ return true })() ? console.log(true) : console.log(false);
+            (function(){ return t })() ? console.log(true) : console.log(false);
         }
     }
     expect: {
         function f() {
-            !function(){ return true }() ? console.log(false) : console.log(true), function(){
+            !function(){ return t }() ? console.log(false) : console.log(true), function(){
                 console.log("something");
             }();
         }
         function g() {
             (function(){
                 console.log("something");
-            })(), function(){ return true }() ? console.log(true) : console.log(false);
+            })(), function(){ return t }() ? console.log(true) : console.log(false);
         }
     }
 }
@@ -119,7 +119,7 @@ negate_iife_5: {
         conditionals: true,
     };
     input: {
-        if ((function(){ return true })()) {
+        if ((function(){ return t })()) {
             foo(true);
         } else {
             bar(false);
@@ -129,7 +129,7 @@ negate_iife_5: {
         })();
     }
     expect: {
-        !function(){ return true }() ? bar(false) : foo(true), function(){
+        !function(){ return t }() ? bar(false) : foo(true), function(){
             console.log("something");
         }();
     }
@@ -142,7 +142,7 @@ negate_iife_5_off: {
         conditionals: true,
     };
     input: {
-        if ((function(){ return true })()) {
+        if ((function(){ return t })()) {
             foo(true);
         } else {
             bar(false);
@@ -152,7 +152,7 @@ negate_iife_5_off: {
         })();
     }
     expect: {
-        !function(){ return true }() ? bar(false) : foo(true), function(){
+        !function(){ return t }() ? bar(false) : foo(true), function(){
             console.log("something");
         }();
     }
diff --git a/test/mocha/minify.js b/test/mocha/minify.js
index 2cc800f..51c46b2 100644
--- a/test/mocha/minify.js
+++ b/test/mocha/minify.js
@@ -155,7 +155,7 @@ describe("minify", function() {
             assert.strictEqual(code, "//  comment1   comment2\nbar();");
         });
         it("should not drop #__PURE__ hint if function is retained", function() {
-            var result = Uglify.minify("var a = /*#__PURE__*/(function(){return 1})();", {
+            var result = Uglify.minify("var a = /*#__PURE__*/(function(){ foo(); })();", {
                 fromString: true,
                 output: {
                     comments: "all",
@@ -163,7 +163,7 @@ describe("minify", function() {
                 }
             });
             var code = result.code;
-            assert.strictEqual(code, "var a=/*#__PURE__*/function(){return 1}();");
+            assert.strictEqual(code, "var a=/*#__PURE__*/function(){foo()}();");
         })
     });
 

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