aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTal Ater <tal@talater.com>2014-09-04 02:57:49 +0300
committerTal Ater <tal@talater.com>2014-09-04 02:57:49 +0300
commitfb0ec720a4bbd3e136284c4b01a88025fbde004e (patch)
treee3a9544b13c9d6d6c75925ade652c650c71c4849
parent7971ed33d13d33ad2b1ebf1ddcc57c466b819514 (diff)
downloadtracifyjs-fb0ec720a4bbd3e136284c4b01a88025fbde004e.tar.gz
tracifyjs-fb0ec720a4bbd3e136284c4b01a88025fbde004e.zip
Compress conditions that have side effects using sequences
-rw-r--r--lib/compress.js10
-rw-r--r--test/compress/conditionals.js32
2 files changed, 27 insertions, 15 deletions
diff --git a/lib/compress.js b/lib/compress.js
index e493e5fe..77de5946 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2322,11 +2322,15 @@ merge(Compressor.prototype, {
});
}
// x=y?1:1 --> x=1
- if (!self.condition.has_side_effects(compressor)
- && consequent instanceof AST_Constant
+ if (consequent instanceof AST_Constant
&& alternative instanceof AST_Constant
&& consequent.equivalent_to(alternative)) {
- return make_node_from_constant(compressor, consequent.value, self);
+ 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 c244dc84..c20297aa 100644
--- a/test/compress/conditionals.js
+++ b/test/compress/conditionals.js
@@ -258,30 +258,38 @@ cond_7: {
x = y ? 'foo' : y ? 'foo' : 'fo'+'o';
- // don't compress these
- x = y ? a : b;
-
- x = y ? 'foo' : 'fo';
+ // Compress conditions that have side effects
+ if (condition()) {
+ x = 10+10;
+ } else {
+ x = 20;
+ }
- // 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;
+ if (z) {
+ x = 'fuji';
+ } else if (condition()) {
+ x = 'fu'+'ji';
} else {
- x = 2;
+ x = 'fuji';
}
- x = some_condition() ? 'foo' : 'fo'+'o';
+ 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';
- x = some_condition() ? 2 : 2;
- x = some_condition() ? 'foo' : 'foo';
}
}