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) {
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;
}

View File

@@ -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"
}