[Pkg-javascript-commits] [uglifyjs] 27/491: improve literal return optimization (#1860)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:19 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 ea9289771b79c273347af72fba024ca29cfa035d
Author: kzc <kzc at users.noreply.github.com>
Date:   Mon May 1 12:10:11 2017 -0400

    improve literal return optimization (#1860)
---
 lib/compress.js              | 31 ++++++++++++++++++++++++++++++-
 test/compress/functions.js   | 22 ++++++++++++++++++++++
 test/compress/issue-1787.js  |  6 +-----
 test/compress/negate-iife.js |  8 ++------
 4 files changed, 55 insertions(+), 12 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 4e86a30..e5a7af2 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1780,6 +1780,35 @@ merge(Compressor.prototype, {
         node.DEFMETHOD("has_side_effects", func);
     });
 
+    // determine if expression is constant
+    (function(def){
+        function all(list) {
+            for (var i = list.length; --i >= 0;)
+                if (!list[i].is_constant_expression())
+                    return false;
+            return true;
+        }
+        def(AST_Node, return_false);
+        def(AST_Constant, return_true);
+        def(AST_Unary, function(){
+            return this.expression.is_constant_expression();
+        });
+        def(AST_Binary, function(){
+            return this.left.is_constant_expression() && this.right.is_constant_expression();
+        });
+        def(AST_Array, function(){
+            return all(this.elements);
+        });
+        def(AST_Object, function(){
+            return all(this.properties);
+        });
+        def(AST_ObjectProperty, function(){
+            return this.value.is_constant_expression();
+        });
+    })(function(node, func){
+        node.DEFMETHOD("is_constant_expression", func);
+    });
+
     // tell me if a statement aborts
     function aborts(thing) {
         return thing && thing.aborts();
@@ -3004,7 +3033,7 @@ merge(Compressor.prototype, {
         if (exp instanceof AST_Function) {
             if (exp.body[0] instanceof AST_Return) {
                 var value = exp.body[0].value;
-                if (!value || value.is_constant()) {
+                if (!value || value.is_constant_expression()) {
                     var args = self.args.concat(value || make_node(AST_Undefined, self));
                     return make_sequence(self, args).transform(compressor);
                 }
diff --git a/test/compress/functions.js b/test/compress/functions.js
index 2a2d096..3a560f0 100644
--- a/test/compress/functions.js
+++ b/test/compress/functions.js
@@ -145,3 +145,25 @@ issue_1841_2: {
     }
     expect_exact: "42"
 }
+
+function_returning_constant_literal: {
+    options = {
+        reduce_vars: true,
+        unsafe: true,
+        toplevel: true,
+        evaluate: true,
+        cascade: true,
+        unused: true,
+    }
+    input: {
+        function greeter() {
+            return { message: 'Hello there' };
+        }
+        var greeting = greeter();
+        console.log(greeting.message);
+    }
+    expect: {
+        console.log("Hello there");
+    }
+    expect_stdout: "Hello there"
+}
diff --git a/test/compress/issue-1787.js b/test/compress/issue-1787.js
index 43d1f1b..02fa0f9 100644
--- a/test/compress/issue-1787.js
+++ b/test/compress/issue-1787.js
@@ -10,10 +10,6 @@ unary_prefix: {
             return x;
         }());
     }
-    expect: {
-        console.log(function() {
-            return -2 / 3;
-        }());
-    }
+    expect_exact: "console.log(-2/3);"
     expect_stdout: true
 }
diff --git a/test/compress/negate-iife.js b/test/compress/negate-iife.js
index 343e8e1..514a15c 100644
--- a/test/compress/negate-iife.js
+++ b/test/compress/negate-iife.js
@@ -25,11 +25,9 @@ negate_iife_2: {
         negate_iife: true
     };
     input: {
-        (function(){ return {} })().x = 10; // should not transform this one
-    }
-    expect: {
         (function(){ return {} })().x = 10;
     }
+    expect_exact: "({}).x=10;"
 }
 
 negate_iife_2_side_effects: {
@@ -38,11 +36,9 @@ negate_iife_2_side_effects: {
         side_effects: true,
     }
     input: {
-        (function(){ return {} })().x = 10; // should not transform this one
-    }
-    expect: {
         (function(){ return {} })().x = 10;
     }
+    expect_exact: "({}).x=10;"
 }
 
 negate_iife_3: {

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