account for side-effects in conditional call inversion (#2562)

fixes #2560
This commit is contained in:
Alex Lam S.L
2017-12-08 01:15:31 +08:00
committed by GitHub
parent d21cb84696
commit 3e34f62a1c
2 changed files with 132 additions and 27 deletions

View File

@@ -4652,18 +4652,22 @@ merge(Compressor.prototype, {
});
}
// x ? y(a) : y(b) --> y(x ? a : b)
var arg_index;
if (consequent instanceof AST_Call
&& alternative.TYPE === consequent.TYPE
&& consequent.args.length == 1
&& alternative.args.length == 1
&& consequent.args.length > 0
&& consequent.args.length == alternative.args.length
&& consequent.expression.equivalent_to(alternative.expression)
&& !consequent.expression.has_side_effects(compressor)) {
consequent.args[0] = make_node(AST_Conditional, self, {
&& !self.condition.has_side_effects(compressor)
&& !consequent.expression.has_side_effects(compressor)
&& typeof (arg_index = single_arg_diff()) == "number") {
var node = consequent.clone();
node.args[arg_index] = make_node(AST_Conditional, self, {
condition: self.condition,
consequent: consequent.args[0],
alternative: alternative.args[0]
consequent: consequent.args[arg_index],
alternative: alternative.args[arg_index]
});
return consequent;
return node;
}
// x?y?z:a:a --> x&&y?z:a
if (consequent instanceof AST_Conditional
@@ -4760,6 +4764,19 @@ merge(Compressor.prototype, {
&& node.expression instanceof AST_Constant
&& node.expression.getValue());
}
function single_arg_diff() {
var a = consequent.args;
var b = alternative.args;
for (var i = 0, len = a.length; i < len; i++) {
if (!a[i].equivalent_to(b[i])) {
for (var j = i + 1; j < len; j++) {
if (!a[j].equivalent_to(b[j])) return;
}
return i;
}
}
}
});
OPT(AST_Boolean, function(self, compressor){