fix corner case in unused (#5867)

fixes #5866
This commit is contained in:
Alex Lam S.L
2024-06-26 16:13:29 +03:00
committed by GitHub
parent ce8ef52e2b
commit ed36c1ec5c
2 changed files with 253 additions and 2 deletions

View File

@@ -8129,6 +8129,7 @@ Compressor.prototype.compress = function(node) {
}
function trim_destructured(node, value, process, drop, root) {
var unwind = true;
var trimmer = new TreeTransformer(function(node) {
if (node instanceof AST_DefaultValue) {
if (!(compressor.option("default_values") && value && value.is_defined(compressor))) {
@@ -8146,21 +8147,27 @@ Compressor.prototype.compress = function(node) {
}
if (node instanceof AST_DestructuredArray) {
var save_drop = drop;
var save_unwind = unwind;
var save_value = value;
if (value instanceof AST_SymbolRef) {
drop = false;
value = value.fixed_value();
}
var native, values;
var last_side_effects, native, values;
if (value instanceof AST_Array) {
native = true;
values = value.elements;
if (save_unwind) for (last_side_effects = values.length; --last_side_effects >= 0;) {
if (values[last_side_effects].has_side_effects(compressor)) break;
}
} else {
native = value && value.is_string(compressor);
values = false;
last_side_effects = node.elements.length;
}
var elements = [], newValues = drop && [], pos = 0;
node.elements.forEach(function(element, index) {
if (save_unwind) unwind = index >= last_side_effects;
value = values && values[index];
if (value instanceof AST_Hole) {
value = null;
@@ -8203,6 +8210,7 @@ Compressor.prototype.compress = function(node) {
}
}
value = save_value;
unwind = save_unwind;
drop = save_drop;
if (values && newValues) {
fill_holes(value, newValues);
@@ -8218,6 +8226,7 @@ Compressor.prototype.compress = function(node) {
return null;
case 1:
if (!drop) break;
if (!unwind) break;
if (node === root) break;
var sym = elements[0];
if (sym.has_side_effects(compressor)) break;
@@ -8236,16 +8245,19 @@ Compressor.prototype.compress = function(node) {
}
if (node instanceof AST_DestructuredObject) {
var save_drop = drop;
var save_unwind = unwind;
var save_value = value;
if (value instanceof AST_SymbolRef) {
drop = false;
value = value.fixed_value();
}
var prop_keys, prop_map, values;
var last_side_effects, prop_keys, prop_map, values;
if (value instanceof AST_Object) {
last_side_effects = -1;
prop_keys = [];
prop_map = new Dictionary();
values = value.properties.map(function(prop, index) {
if (save_unwind && prop.has_side_effects(compressor)) last_side_effects = index;
prop = prop.clone();
if (prop instanceof AST_Spread) {
prop_map = false;
@@ -8261,6 +8273,8 @@ Compressor.prototype.compress = function(node) {
}
return prop;
});
} else {
last_side_effects = node.properties.length;
}
if (node.rest) {
value = false;
@@ -8283,6 +8297,7 @@ Compressor.prototype.compress = function(node) {
return key;
}).forEach(function(key, index) {
var prop = node.properties[index], trimmed;
if (save_unwind) unwind = index >= last_side_effects;
if (key instanceof AST_Node) {
drop = false;
value = false;
@@ -8323,6 +8338,7 @@ Compressor.prototype.compress = function(node) {
}
});
value = save_value;
unwind = save_unwind;
drop = save_drop;
if (drop_keys && prop_keys) {
value = value.clone();
@@ -8357,6 +8373,7 @@ Compressor.prototype.compress = function(node) {
return null;
case 1:
if (!drop) break;
if (!unwind) break;
if (node === root) break;
var prop = properties[0];
if (prop.key instanceof AST_Node) break;