[Pkg-javascript-commits] [uglifyjs] 37/77: Optimize conditionals where the consequent and alternative are both booleans and not equivalent

Jonas Smedegaard dr at jones.dk
Tue May 19 00:02:30 UTC 2015


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

js pushed a commit to tag v2.4.18
in repository uglifyjs.

commit a1a4c2ada70760851c63a7e3ff51ee7f786d3e8f
Author: Tal Ater <tal at talater.com>
Date:   Sat Sep 13 18:59:19 2014 +0300

    Optimize conditionals where the consequent and alternative are both booleans and not equivalent
---
 lib/compress.js               | 14 +++++++++++
 test/compress/conditionals.js | 54 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)

diff --git a/lib/compress.js b/lib/compress.js
index 72e4d92..ae20d48 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2386,6 +2386,20 @@ merge(Compressor.prototype, {
 
             }
         }
+        // x=y?true:false --> x=!!y
+        if (consequent instanceof AST_True
+            && alternative instanceof AST_False) {
+            self.condition = self.condition.negate(compressor);
+            return make_node(AST_UnaryPrefix, self.condition, {
+                operator: "!",
+                expression: self.condition
+            });
+        }
+        // x=y?false:true --> x=!y
+        if (consequent instanceof AST_False
+            && alternative instanceof AST_True) {
+            return self.condition.negate(compressor)
+        }
         return self;
     });
 
diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js
index 2212f85..9af1630 100644
--- a/test/compress/conditionals.js
+++ b/test/compress/conditionals.js
@@ -312,3 +312,57 @@ cond_7_1: {
         x = (y, 2);
     }
 }
+
+cond_8: {
+    options = {
+        conditionals: true,
+        evaluate    : true
+    };
+    input: {
+        // compress these
+        a = condition ? true : false;
+
+        a = !condition ? true : false;
+
+        a = condition() ? true : false;
+
+        if (condition) {
+            a = true;
+        } else {
+            a = false;
+        }
+
+        a = condition ? false : true;
+
+        a = !condition ? false : true;
+
+        a = condition() ? false : true;
+
+        if (condition) {
+            a = false;
+        } else {
+            a = true;
+        }
+
+        // don't compress these
+        a = condition ? 1 : false;
+
+        a = !condition ? true : 0;
+
+        a = condition ? 1 : 0;
+
+    }
+    expect: {
+        a = !!condition;
+        a = !condition;
+        a = !!condition();
+        a = !!condition;
+        a = !condition;
+        a = !!condition;
+        a = !condition();
+        a = !condition;
+        a = condition ? 1 : false;
+        a = condition ? 0 : true;
+        a = condition ? 1 : 0;
+    }
+}
\ No newline at end of file

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