avoid generating ternary with spread (#2293)

fixes #2292
This commit is contained in:
kzc
2017-08-30 12:57:07 -04:00
committed by Alex Lam S.L
parent 067d52b6ba
commit 2779a29a86
2 changed files with 45 additions and 0 deletions

View File

@@ -4500,6 +4500,8 @@ merge(Compressor.prototype, {
&& alternative.TYPE === consequent.TYPE && alternative.TYPE === consequent.TYPE
&& consequent.args.length == 1 && consequent.args.length == 1
&& alternative.args.length == 1 && alternative.args.length == 1
&& !(consequent.args[0] instanceof AST_Expansion)
&& !(alternative.args[0] instanceof AST_Expansion)
&& consequent.expression.equivalent_to(alternative.expression) && consequent.expression.equivalent_to(alternative.expression)
&& !consequent.expression.has_side_effects(compressor)) { && !consequent.expression.has_side_effects(compressor)) {
consequent.args[0] = make_node(AST_Conditional, self, { consequent.args[0] = make_node(AST_Conditional, self, {

View File

@@ -26,3 +26,46 @@ expand_parameters: {
expect_exact: "(function(a,...b){});(function(...args){});" expect_exact: "(function(a,...b){});(function(...args){});"
} }
avoid_spread_in_ternary: {
options = {
comparisons: true,
conditionals: true,
evaluate: true,
}
input: {
function print(...x) {
console.log(...x);
}
var a = [1, 2], b = [3, 4];
if (Math)
print(a);
else
print(b);
if (Math)
print(...a);
else
print(b);
if (Math.no_such_property)
print(a);
else
print(...b);
}
expect: {
function print(...x) {
console.log(...x);
}
var a = [ 1, 2 ], b = [ 3, 4 ];
print(Math ? a : b);
Math ? print(...a) : print(b);
Math.no_such_property ? print(a) : print(...b);
}
expect_stdout: [
"[ 1, 2 ]",
"1 2",
"3 4",
]
node_version: ">=6"
}