From 850ab84495913196f2d26c20ccd54b8bb82bb78e Mon Sep 17 00:00:00 2001 From: kzc Date: Wed, 22 Nov 2017 21:57:14 -0500 Subject: [PATCH] optimize elements in array literal before spread --- lib/compress.js | 4 +-- test/compress/harmony.js | 77 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 9a2ceeb9..8a6a5f25 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5000,9 +5000,7 @@ merge(Compressor.prototype, { var flatten = true; var values = []; for (var i = elements.length; --i > index;) { - var value = elements[i]; - if (value instanceof AST_Expansion) break FLATTEN; - var value = value.drop_side_effect_free(compressor); + var value = elements[i].drop_side_effect_free(compressor); if (value) { values.unshift(value); if (flatten && value.has_side_effects(compressor)) flatten = false; diff --git a/test/compress/harmony.js b/test/compress/harmony.js index c197ddb5..ded16f09 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -1009,7 +1009,7 @@ array_literal_with_spread_2: { } expect: { console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ]["length"]); - console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ][0]); + console.log(10); console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ][1]); console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ][2]); console.log([ 10, ...[], 20, ...[ 30, 40 ], 50 ][3]); @@ -1059,12 +1059,12 @@ array_literal_with_spread_3: { console.log([...[], 10, 20][1]); console.log([...[], 10, 20][2]); - console.log([10, ...[], 20][0]); + console.log(10); console.log([10, ...[], 20][1]); console.log([10, ...[], 20][2]); - console.log([10, 20, ...[]][0]); - console.log([10, 20, ...[]][1]); + console.log(10); + console.log(20); console.log([10, 20, ...[]][2]); } expect_stdout: [ @@ -1083,3 +1083,72 @@ array_literal_with_spread_3: { ] node_version: ">=6" } + +array_literal_with_spread_4: { + options = { + properties: true, + side_effects: true, + } + input: { + function t(x) { + console.log("(" + x + ")"); + return 10 * x; + } + + console.log([t(1), t(2)][0]); + console.log([t(1), t(2)][1]); + console.log([t(1), t(2)][2]); + + console.log([...[], t(1), t(2)][0]); + console.log([...[], t(1), t(2)][1]); + console.log([...[], t(1), t(2)][2]); + + console.log([t(1), ...[], t(2)][0]); + console.log([t(1), ...[], t(2)][1]); + console.log([t(1), ...[], t(2)][2]); + + console.log([t(1), t(2), ...[]][0]); + console.log([t(1), t(2), ...[]][1]); + console.log([t(1), t(2), ...[]][2]); + } + expect: { + function t(x) { + console.log("(" + x + ")"); + return 10 * x; + } + + console.log([ t(1), t(2) ][0]); + console.log((t(1), t(2))); + console.log([ t(1), t(2) ][2]); + + console.log([ ...[], t(1), t(2) ][0]); + console.log([ ...[], t(1), t(2) ][1]); + console.log([ ...[], t(1), t(2) ][2]); + + console.log([ t(1), t(2) ][0]); + console.log([ t(1), ...[], t(2) ][1]); + console.log([ t(1), ...[], t(2) ][2]); + + console.log([ t(1), t(2) ][0]); + console.log((t(1), t(2))); + console.log([ t(1), t(2), ...[] ][2]); + } + expect_stdout: [ + "(1)", "(2)", "10", + "(1)", "(2)", "20", + "(1)", "(2)", "undefined", + + "(1)", "(2)", "10", + "(1)", "(2)", "20", + "(1)", "(2)", "undefined", + + "(1)", "(2)", "10", + "(1)", "(2)", "20", + "(1)", "(2)", "undefined", + + "(1)", "(2)", "10", + "(1)", "(2)", "20", + "(1)", "(2)", "undefined", + ] + node_version: ">=6" +}