fix corner cases in unused (#5073)

fixes #5071
This commit is contained in:
Alex Lam S.L
2021-07-11 07:52:38 +01:00
committed by GitHub
parent 64ebf6efe9
commit 1ad830facb
2 changed files with 56 additions and 19 deletions

View File

@@ -6712,7 +6712,7 @@ merge(Compressor.prototype, {
if (node.left instanceof AST_Destructured) { if (node.left instanceof AST_Destructured) {
var trimmed = trim_destructured(node.left, node.right, function(node) { var trimmed = trim_destructured(node.left, node.right, function(node) {
return node; return node;
}, node.write_only); }, node.write_only === true);
if (!trimmed.name) { if (!trimmed.name) {
if (trimmed.value) return trimmed.value; if (trimmed.value) return trimmed.value;
if (parent instanceof AST_Sequence && parent.tail_node() !== node) return List.skip; if (parent instanceof AST_Sequence && parent.tail_node() !== node) return List.skip;
@@ -7052,21 +7052,23 @@ merge(Compressor.prototype, {
value = value.fixed_value(); value = value.fixed_value();
} }
var values = value instanceof AST_Array && value.elements; 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) { node.elements.forEach(function(element, index) {
value = values && values[index]; value = values && values[index];
if (value instanceof AST_Hole) { if (value instanceof AST_Hole) {
value = null; value = null;
} else if (value instanceof AST_Spread) { } else if (value instanceof AST_Spread) {
newValues.length = pos; if (drop) {
fill_holes(save_value, newValues); newValues.length = pos;
[].push.apply(newValues, values.slice(index)); fill_holes(save_value, newValues);
save_value.elements = newValues; [].push.apply(newValues, values.slice(index));
save_value.elements = newValues;
}
value = values = false; value = values = false;
} }
element = element.transform(trimmer); element = element.transform(trimmer);
if (element) elements[pos] = element; if (element) elements[pos] = element;
if (value) newValues[pos] = value; if (drop && value) newValues[pos] = value;
if (element || value || !drop || !values) pos++; if (element || value || !drop || !values) pos++;
}); });
value = values && make_node(AST_Array, save_value, { value = values && make_node(AST_Array, save_value, {
@@ -7079,21 +7081,23 @@ merge(Compressor.prototype, {
drop = was_drop; drop = was_drop;
if (node.rest) elements.length = pos; if (node.rest) elements.length = pos;
} }
if (drop && value && !node.rest) value = value.drop_side_effect_free(compressor); if (drop) {
if (value instanceof AST_Array) { if (value && !node.rest) value = value.drop_side_effect_free(compressor);
value = value.elements; if (value instanceof AST_Array) {
} else if (value instanceof AST_Sequence) { value = value.elements;
value = value.expressions; } else if (value instanceof AST_Sequence) {
} else if (value) { value = value.expressions;
value = [ value ]; } else if (value) {
} value = [ value ];
if (value && value.length) { }
newValues.length = pos; if (value && value.length) {
[].push.apply(newValues, value); newValues.length = pos;
[].push.apply(newValues, value);
}
} }
value = save_value; value = save_value;
drop = save_drop; drop = save_drop;
if (values && value instanceof AST_Array) { if (values && newValues) {
fill_holes(value, newValues); fill_holes(value, newValues);
value.elements = newValues; value.elements = newValues;
} }

View File

@@ -2783,3 +2783,36 @@ issue_5017: {
expect_stdout: "PASS" expect_stdout: "PASS"
node_version: ">=6" 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"
}