diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-07-11 07:52:38 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-11 14:52:38 +0800 |
commit | 1ad830facb198878d7fe35586bca89c42b1ea263 (patch) | |
tree | 7c8e765244e22c91eed2807c4ef30eaaa0220bf1 | |
parent | 64ebf6efe939f9ea6a7b5bae8762460fdcc39ad7 (diff) | |
download | tracifyjs-1ad830facb198878d7fe35586bca89c42b1ea263.tar.gz tracifyjs-1ad830facb198878d7fe35586bca89c42b1ea263.zip |
fix corner cases in `unused` (#5073)
fixes #5071
-rw-r--r-- | lib/compress.js | 42 | ||||
-rw-r--r-- | test/compress/destructured.js | 33 |
2 files changed, 56 insertions, 19 deletions
diff --git a/lib/compress.js b/lib/compress.js index 28f127d5..e3816fa0 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6712,7 +6712,7 @@ merge(Compressor.prototype, { if (node.left instanceof AST_Destructured) { var trimmed = trim_destructured(node.left, node.right, function(node) { return node; - }, node.write_only); + }, node.write_only === true); if (!trimmed.name) { if (trimmed.value) return trimmed.value; if (parent instanceof AST_Sequence && parent.tail_node() !== node) return List.skip; @@ -7052,21 +7052,23 @@ merge(Compressor.prototype, { value = value.fixed_value(); } var values = value instanceof AST_Array && value.elements; - var elements = [], newValues = [], pos = 0; + var elements = [], newValues = drop && [], pos = 0; node.elements.forEach(function(element, index) { value = values && values[index]; if (value instanceof AST_Hole) { value = null; } else if (value instanceof AST_Spread) { - newValues.length = pos; - fill_holes(save_value, newValues); - [].push.apply(newValues, values.slice(index)); - save_value.elements = newValues; + if (drop) { + newValues.length = pos; + fill_holes(save_value, newValues); + [].push.apply(newValues, values.slice(index)); + save_value.elements = newValues; + } value = values = false; } element = element.transform(trimmer); if (element) elements[pos] = element; - if (value) newValues[pos] = value; + if (drop && value) newValues[pos] = value; if (element || value || !drop || !values) pos++; }); value = values && make_node(AST_Array, save_value, { @@ -7079,21 +7081,23 @@ merge(Compressor.prototype, { drop = was_drop; if (node.rest) elements.length = pos; } - if (drop && value && !node.rest) value = value.drop_side_effect_free(compressor); - if (value instanceof AST_Array) { - value = value.elements; - } else if (value instanceof AST_Sequence) { - value = value.expressions; - } else if (value) { - value = [ value ]; - } - if (value && value.length) { - newValues.length = pos; - [].push.apply(newValues, value); + if (drop) { + if (value && !node.rest) value = value.drop_side_effect_free(compressor); + if (value instanceof AST_Array) { + value = value.elements; + } else if (value instanceof AST_Sequence) { + value = value.expressions; + } else if (value) { + value = [ value ]; + } + if (value && value.length) { + newValues.length = pos; + [].push.apply(newValues, value); + } } value = save_value; drop = save_drop; - if (values && value instanceof AST_Array) { + if (values && newValues) { fill_holes(value, newValues); value.elements = newValues; } diff --git a/test/compress/destructured.js b/test/compress/destructured.js index 7db2d238..45724b1a 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -2783,3 +2783,36 @@ issue_5017: { expect_stdout: "PASS" node_version: ">=6" } + +issue_5071_1: { + options = { + unused: true, + } + input: { + var a; + console.log(([ , a ] = [ "PA", , ]).join("SS")); + } + expect: { + var a; + console.log(([ , a ] = [ "PA", , ]).join("SS")); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +issue_5071_2: { + options = { + pure_getters: "strict", + unused: true, + } + input: { + var a; + ([ a ] = []).p = console.log("PASS"); + } + expect: { + var a; + ([ a ] = []).p = console.log("PASS"); + } + expect_stdout: "PASS" + node_version: ">=6" +} |