From b6868a030afb7a86efcbc3b77b0cbda68c762a68 Mon Sep 17 00:00:00 2001 From: kzc Date: Wed, 22 Nov 2017 18:35:09 -0500 Subject: [PATCH] fix `properties` for array literal with spread --- lib/compress.js | 11 +++-- test/compress/harmony.js | 86 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index aa97b13c..9a2ceeb9 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4996,21 +4996,26 @@ merge(Compressor.prototype, { && prop instanceof AST_Number && expr instanceof AST_Array) { var index = prop.getValue(); var elements = expr.elements; - if (index in elements) { + FLATTEN: if (index in elements) { var flatten = true; var values = []; for (var i = elements.length; --i > index;) { - var value = elements[i].drop_side_effect_free(compressor); + var value = elements[i]; + if (value instanceof AST_Expansion) break FLATTEN; + var value = value.drop_side_effect_free(compressor); if (value) { values.unshift(value); if (flatten && value.has_side_effects(compressor)) flatten = false; } } var retValue = elements[index]; + if (retValue instanceof AST_Expansion) break FLATTEN; retValue = retValue instanceof AST_Hole ? make_node(AST_Undefined, retValue) : retValue; if (!flatten) values.unshift(retValue); while (--i >= 0) { - var value = elements[i].drop_side_effect_free(compressor); + var value = elements[i]; + if (value instanceof AST_Expansion) break FLATTEN; + value = value.drop_side_effect_free(compressor); if (value) values.unshift(value); else index--; } diff --git a/test/compress/harmony.js b/test/compress/harmony.js index 11b36b2d..1c7b7fbf 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -975,3 +975,89 @@ shorthand_keywords: { expect_stdout: true node_version: ">=4" } + +array_literal_with_spread: { + options = { + properties: true, + side_effects: true, + } + input: { + var f = (x) => [...x][0]; + console.log(f(["PASS"])); + + console.log([10, ...[], 20, ...[30, 40], 50]["length"]); + console.log([10, ...[], 20, ...[30, 40], 50][0]); + console.log([10, ...[], 20, ...[30, 40], 50][1]); + console.log([10, ...[], 20, ...[30, 40], 50][2]); + console.log([10, ...[], 20, ...[30, 40], 50][3]); + console.log([10, ...[], 20, ...[30, 40], 50][4]); + console.log([10, ...[], 20, ...[30, 40], 50][5]); + + console.log([10, 20][0]); + console.log([10, 20][1]); + console.log([10, 20][2]); + + console.log([...[], 10, 20][0]); + console.log([...[], 10, 20][1]); + console.log([...[], 10, 20][2]); + + console.log([10, ...[], 20][0]); + console.log([10, ...[], 20][1]); + console.log([10, ...[], 20][2]); + + console.log([10, 20, ...[]][0]); + console.log([10, 20, ...[]][1]); + console.log([10, 20, ...[]][2]); + } + expect: { + var f = x => [ ...x ][0]; + console.log(f([ "PASS" ])); + + console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ]["length"]); + console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ][0]); + console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ][1]); + console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ][2]); + console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ][3]); + console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ][4]); + console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ][5]); + + console.log(10); + console.log(20); + console.log([ 10, 20 ][2]); + + console.log([...[], 10, 20][0]); + console.log([...[], 10, 20][1]); + console.log([...[], 10, 20][2]); + + console.log([10, ...[], 20][0]); + console.log([10, ...[], 20][1]); + console.log([10, ...[], 20][2]); + + console.log([10, 20, ...[]][0]); + console.log([10, 20, ...[]][1]); + console.log([10, 20, ...[]][2]); + } + expect_stdout: [ + "PASS", + "5", + "10", + "20", + "30", + "40", + "50", + "undefined", + "10", + "20", + "undefined", + "10", + "20", + "undefined", + "10", + "20", + "undefined", + "10", + "20", + "undefined", + ] + node_version: ">=6" +}