aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/compress.js11
-rw-r--r--test/compress/conditionals.js61
2 files changed, 72 insertions, 0 deletions
diff --git a/lib/compress.js b/lib/compress.js
index b55361b3..77de5946 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2321,6 +2321,17 @@ merge(Compressor.prototype, {
alternative: alternative
});
}
+ // x=y?1:1 --> x=1
+ if (consequent instanceof AST_Constant
+ && alternative instanceof AST_Constant
+ && consequent.equivalent_to(alternative)) {
+ if (self.condition.has_side_effects(compressor)) {
+ return AST_Seq.from_array([self.condition, make_node_from_constant(compressor, consequent.value, self)]);
+ } else {
+ return make_node_from_constant(compressor, consequent.value, self);
+
+ }
+ }
return self;
});
diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js
index 213b246b..c20297aa 100644
--- a/test/compress/conditionals.js
+++ b/test/compress/conditionals.js
@@ -232,3 +232,64 @@ 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;
+ }
+
+ if (y) {
+ x = 1+1;
+ } else if (z) {
+ x = 2;
+ } else {
+ x = 3-1;
+ }
+
+ x = y ? 'foo' : 'fo'+'o';
+
+ x = y ? 'foo' : y ? 'foo' : 'fo'+'o';
+
+ // Compress conditions that have side effects
+ if (condition()) {
+ x = 10+10;
+ } else {
+ x = 20;
+ }
+
+ if (z) {
+ x = 'fuji';
+ } else if (condition()) {
+ x = 'fu'+'ji';
+ } else {
+ x = 'fuji';
+ }
+
+ x = condition() ? 'foobar' : 'foo'+'bar';
+
+ // don't compress these
+ x = y ? a : b;
+
+ x = y ? 'foo' : 'fo';
+ }
+ expect: {
+ x = 2;
+ x = 2;
+ x = 'foo';
+ x = 'foo';
+ x = (condition(), 20);
+ x = z ? 'fuji' : (condition(), 'fuji');
+ x = (condition(), 'foobar');
+ x = y ? a : b;
+ x = y ? 'foo' : 'fo';
+ }
+}
+