From 885835a655b4c1b3a0a9cb0f78edaa6ac446a0e7 Mon Sep 17 00:00:00 2001 From: Tal Ater Date: Tue, 2 Sep 2014 23:30:25 +0300 Subject: 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 b55361b3..e493e5fe 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 213b246b..b0234031 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'; + } +} + -- cgit v1.2.3 From 7971ed33d13d33ad2b1ebf1ddcc57c466b819514 Mon Sep 17 00:00:00 2001 From: Tal Ater Date: Wed, 3 Sep 2014 01:35:30 +0300 Subject: Added a test for else if --- test/compress/conditionals.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js index b0234031..c244dc84 100644 --- a/test/compress/conditionals.js +++ b/test/compress/conditionals.js @@ -246,6 +246,14 @@ cond_7: { 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'; @@ -266,6 +274,7 @@ cond_7: { x = some_condition() ? 'foo' : 'fo'+'o'; } expect: { + x = 2; x = 2; x = 'foo'; x = 'foo'; -- cgit v1.2.3 From fb0ec720a4bbd3e136284c4b01a88025fbde004e Mon Sep 17 00:00:00 2001 From: Tal Ater Date: Thu, 4 Sep 2014 02:57:49 +0300 Subject: Compress conditions that have side effects using sequences --- lib/compress.js | 10 +++++++--- test/compress/conditionals.js | 32 ++++++++++++++++++++------------ 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'; } } -- cgit v1.2.3