diff options
author | Mihai Bazon <mihai.bazon@gmail.com> | 2014-09-09 18:45:12 +0300 |
---|---|---|
committer | Mihai Bazon <mihai.bazon@gmail.com> | 2014-09-09 18:45:12 +0300 |
commit | 57dab1e1db7ed542ce9ace7caa3141130edffe12 (patch) | |
tree | e3a9544b13c9d6d6c75925ade652c650c71c4849 | |
parent | 4c64554808e0805b86893704dc87df9ec449961a (diff) | |
parent | fb0ec720a4bbd3e136284c4b01a88025fbde004e (diff) | |
download | tracifyjs-57dab1e1db7ed542ce9ace7caa3141130edffe12.tar.gz tracifyjs-57dab1e1db7ed542ce9ace7caa3141130edffe12.zip |
Merge pull request #541 from TalAter/conditional-improvements
Conditional assignment of equivalent constants compressed ( x=y?1:1 --> x=1 )
-rw-r--r-- | lib/compress.js | 11 | ||||
-rw-r--r-- | test/compress/conditionals.js | 61 |
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'; + } +} + |