[Pkg-javascript-commits] [uglifyjs] 15/26: Compress conditional assignments where all possible outcomes are equivalant and condition has no side effects

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


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

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

commit 885835a655b4c1b3a0a9cb0f78edaa6ac446a0e7
Author: Tal Ater <tal at talater.com>
Date:   Tue Sep 2 23:30:25 2014 +0300

    Compress conditional assignments where all possible outcomes are equivalant and condition has no side effects
---
 lib/compress.js               |  7 +++++++
 test/compress/conditionals.js | 44 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/lib/compress.js b/lib/compress.js
index b55361b..e493e5f 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2321,6 +2321,13 @@ merge(Compressor.prototype, {
                 alternative: alternative
             });
         }
+        // x=y?1:1 --> x=1
+        if (!self.condition.has_side_effects(compressor)
+            && consequent instanceof AST_Constant
+            && alternative instanceof AST_Constant
+            && consequent.equivalent_to(alternative)) {
+            return make_node_from_constant(compressor, consequent.value, self);
+        }
         return self;
     });
 
diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js
index 213b246..b023403 100644
--- a/test/compress/conditionals.js
+++ b/test/compress/conditionals.js
@@ -232,3 +232,47 @@ cond_5: {
         some_condition() && some_other_condition() && do_something();
     }
 }
+
+cond_7: {
+    options = {
+        conditionals: true,
+        evaluate    : true
+    };
+    input: {
+        // compress these
+        if (y) {
+            x = 1+1;
+        } else {
+            x = 2;
+        }
+
+        x = y ? 'foo' : 'fo'+'o';
+
+        x = y ? 'foo' : y ? 'foo' : 'fo'+'o';
+
+        // don't compress these
+        x = y ? a : b;
+
+        x = y ? 'foo' : 'fo';
+
+        // make sure not to mess with conditions that have side effects
+        // TODO: Make sure to mess with conditions that have side effects... proprely
+        if (some_condition()) {
+            x = 1+1;
+        } else {
+            x = 2;
+        }
+
+        x = some_condition() ? 'foo' : 'fo'+'o';
+    }
+    expect: {
+        x = 2;
+        x = 'foo';
+        x = 'foo';
+        x = y ? a : b;
+        x = y ? 'foo' : 'fo';
+        x = some_condition() ? 2 : 2;
+        x = some_condition() ? 'foo' : '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