diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2018-02-28 23:34:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-28 23:34:48 +0800 |
commit | 56e2a369d0b522c315a3b146bdf7581d8e71d9cc (patch) | |
tree | c2b1ef773365210f97fe41a235b1565f5fac48c0 /lib | |
parent | 0daa199fa85e60b78b04b82a91acdb230cb42229 (diff) | |
download | tracifyjs-56e2a369d0b522c315a3b146bdf7581d8e71d9cc.tar.gz tracifyjs-56e2a369d0b522c315a3b146bdf7581d8e71d9cc.zip |
enhance `conditionals` (#2966)
- `x ? (y, w) : (z, w)` => `x ? y : z, w`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/compress.js b/lib/compress.js index e83524c5..504d270b 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5727,6 +5727,18 @@ merge(Compressor.prototype, { consequent ]).optimize(compressor); } + // x ? (y, w) : (z, w) --> x ? y : z, w + if ((consequent instanceof AST_Sequence || alternative instanceof AST_Sequence) + && consequent.tail_node().equivalent_to(alternative.tail_node())) { + return make_sequence(self, [ + make_node(AST_Conditional, self, { + condition: self.condition, + consequent: pop_seq(consequent), + alternative: pop_seq(alternative) + }), + consequent.tail_node() + ]).optimize(compressor); + } // x ? y || z : z --> x && y || z if (consequent instanceof AST_Binary && consequent.operator == "||" @@ -5829,6 +5841,13 @@ merge(Compressor.prototype, { } } } + + function pop_seq(node) { + if (!(node instanceof AST_Sequence)) return make_node(AST_Number, node, { + value: 0 + }); + return make_sequence(node, node.expressions.slice(0, -1)); + } }); OPT(AST_Boolean, function(self, compressor){ |