fix corner case in unused (#5323)

fixes #5322
This commit is contained in:
Alex Lam S.L
2022-01-29 11:28:19 +00:00
committed by GitHub
parent 28943bcebb
commit e7d6dd2ea2
2 changed files with 48 additions and 13 deletions

View File

@@ -6507,28 +6507,29 @@ Compressor.prototype.compress = function(node) {
if (scope === self) {
if (node instanceof AST_DefClass) {
var def = node.name.definition();
if ((!drop_funcs || def.exported) && !(def.id in in_use_ids)) {
var drop = drop_funcs && !def.exported;
if (!drop && !(def.id in in_use_ids)) {
in_use_ids[def.id] = true;
in_use.push(def);
}
if (node.extends) node.extends.walk(tw);
var is_export = false;
if (tw.parent() instanceof AST_ExportDefault) {
is_export = true;
export_defaults[def.id] = true;
}
var used = tw.parent() instanceof AST_ExportDefault;
if (used) export_defaults[def.id] = true;
var values = [];
node.properties.forEach(function(prop) {
if (prop.key instanceof AST_Node) prop.key.walk(tw);
if (!prop.value) return;
if (is_export || prop instanceof AST_ClassField && prop.static) {
var save_scope = scope;
scope = node;
prop.value.walk(tw);
scope = save_scope;
var value = prop.value;
if (!value) return;
if (prop instanceof AST_ClassField && prop.static) {
if (!used && value.contains_this()) used = true;
walk_class_prop(value);
} else {
initializations.add(def.id, prop.value);
values.push(value);
}
});
values.forEach(drop && used ? walk_class_prop : function(value) {
initializations.add(def.id, value);
});
return true;
}
if (node instanceof AST_LambdaDefinition) {
@@ -6594,6 +6595,13 @@ Compressor.prototype.compress = function(node) {
}
}
return scan_ref_scoped(node, descend, true);
function walk_class_prop(value) {
var save_scope = scope;
scope = node;
value.walk(tw);
scope = save_scope;
}
});
tw.directives = Object.create(compressor.directives);
self.walk(tw);