diff --git a/lib/compress.js b/lib/compress.js index 4b8a2eeb..56a3e362 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4500,6 +4500,8 @@ merge(Compressor.prototype, { && alternative.TYPE === consequent.TYPE && consequent.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.has_side_effects(compressor)) { consequent.args[0] = make_node(AST_Conditional, self, { diff --git a/test/compress/expansions.js b/test/compress/expansions.js index 8879e67f..c818fad7 100644 --- a/test/compress/expansions.js +++ b/test/compress/expansions.js @@ -26,3 +26,46 @@ expand_parameters: { 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" +}