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 (scope === self) {
if (node instanceof AST_DefClass) { if (node instanceof AST_DefClass) {
var def = node.name.definition(); 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_ids[def.id] = true;
in_use.push(def); in_use.push(def);
} }
if (node.extends) node.extends.walk(tw); if (node.extends) node.extends.walk(tw);
var is_export = false; var used = tw.parent() instanceof AST_ExportDefault;
if (tw.parent() instanceof AST_ExportDefault) { if (used) export_defaults[def.id] = true;
is_export = true; var values = [];
export_defaults[def.id] = true;
}
node.properties.forEach(function(prop) { node.properties.forEach(function(prop) {
if (prop.key instanceof AST_Node) prop.key.walk(tw); if (prop.key instanceof AST_Node) prop.key.walk(tw);
if (!prop.value) return; var value = prop.value;
if (is_export || prop instanceof AST_ClassField && prop.static) { if (!value) return;
var save_scope = scope; if (prop instanceof AST_ClassField && prop.static) {
scope = node; if (!used && value.contains_this()) used = true;
prop.value.walk(tw); walk_class_prop(value);
scope = save_scope;
} else { } 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; return true;
} }
if (node instanceof AST_LambdaDefinition) { if (node instanceof AST_LambdaDefinition) {
@@ -6594,6 +6595,13 @@ Compressor.prototype.compress = function(node) {
} }
} }
return scan_ref_scoped(node, descend, true); 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); tw.directives = Object.create(compressor.directives);
self.walk(tw); self.walk(tw);

View File

@@ -2449,3 +2449,30 @@ issue_5294_4: {
expect_stdout: "PASS" expect_stdout: "PASS"
node_version: ">=12" node_version: ">=12"
} }
issue_5322: {
options = {
toplevel: true,
unused: true,
}
input: {
var a = 41;
class A {
static p() {
console.log(++a);
}
static q = this.p();
}
}
expect: {
var a = 41;
(class {
static p() {
console.log(++a);
}
static q = this.p();
});
}
expect_stdout: "42"
node_version: ">=12"
}