Skip to content

Commit da33c58

Browse files
author
Andy
authored
Minor cleanups in checkUnusedIdentifiers (#28513)
1 parent cd08a22 commit da33c58

File tree

1 file changed

+63
-66
lines changed

1 file changed

+63
-66
lines changed

src/compiler/checker.ts

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -24682,7 +24682,7 @@ namespace ts {
2468224682

2468324683
function registerForUnusedIdentifiersCheck(node: PotentiallyUnusedIdentifier): void {
2468424684
// May be in a call such as getTypeOfNode that happened to call this. But potentiallyUnusedIdentifiers is only defined in the scope of `checkSourceFile`.
24685-
if (produceDiagnostics) {
24685+
if (produceDiagnostics && !(node.flags & NodeFlags.Ambient)) {
2468624686
const sourceFile = getSourceFileOfNode(node);
2468724687
let potentiallyUnusedIdentifiers = allPotentiallyUnusedIdentifiers.get(sourceFile.path);
2468824688
if (!potentiallyUnusedIdentifiers) {
@@ -24709,9 +24709,6 @@ namespace ts {
2470924709
checkUnusedClassMembers(node, addDiagnostic);
2471024710
checkUnusedTypeParameters(node, addDiagnostic);
2471124711
break;
24712-
case SyntaxKind.InterfaceDeclaration:
24713-
checkUnusedTypeParameters(node, addDiagnostic);
24714-
break;
2471524712
case SyntaxKind.SourceFile:
2471624713
case SyntaxKind.ModuleDeclaration:
2471724714
case SyntaxKind.Block:
@@ -24728,7 +24725,7 @@ namespace ts {
2472824725
case SyntaxKind.MethodDeclaration:
2472924726
case SyntaxKind.GetAccessor:
2473024727
case SyntaxKind.SetAccessor:
24731-
if (node.body) {
24728+
if (node.body) { // Don't report unused parameters in overloads
2473224729
checkUnusedLocalsAndParameters(node, addDiagnostic);
2473324730
}
2473424731
checkUnusedTypeParameters(node, addDiagnostic);
@@ -24739,9 +24736,12 @@ namespace ts {
2473924736
case SyntaxKind.FunctionType:
2474024737
case SyntaxKind.ConstructorType:
2474124738
case SyntaxKind.TypeAliasDeclaration:
24742-
case SyntaxKind.InferType:
24739+
case SyntaxKind.InterfaceDeclaration:
2474324740
checkUnusedTypeParameters(node, addDiagnostic);
2474424741
break;
24742+
case SyntaxKind.InferType:
24743+
checkUnusedInferTypeParameter(node, addDiagnostic);
24744+
break;
2474524745
default:
2474624746
Debug.assertNever(node, "Node should not have been registered for unused identifiers check");
2474724747
}
@@ -24759,77 +24759,74 @@ namespace ts {
2475924759
}
2476024760

2476124761
function checkUnusedClassMembers(node: ClassDeclaration | ClassExpression, addDiagnostic: AddUnusedDiagnostic): void {
24762-
if (!(node.flags & NodeFlags.Ambient)) {
24763-
for (const member of node.members) {
24764-
switch (member.kind) {
24765-
case SyntaxKind.MethodDeclaration:
24766-
case SyntaxKind.PropertyDeclaration:
24767-
case SyntaxKind.GetAccessor:
24768-
case SyntaxKind.SetAccessor:
24769-
if (member.kind === SyntaxKind.SetAccessor && member.symbol.flags & SymbolFlags.GetAccessor) {
24770-
// Already would have reported an error on the getter.
24771-
break;
24772-
}
24773-
const symbol = getSymbolOfNode(member);
24774-
if (!symbol.isReferenced && hasModifier(member, ModifierFlags.Private)) {
24775-
addDiagnostic(member, UnusedKind.Local, createDiagnosticForNode(member.name!, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolToString(symbol)));
24776-
}
24762+
for (const member of node.members) {
24763+
switch (member.kind) {
24764+
case SyntaxKind.MethodDeclaration:
24765+
case SyntaxKind.PropertyDeclaration:
24766+
case SyntaxKind.GetAccessor:
24767+
case SyntaxKind.SetAccessor:
24768+
if (member.kind === SyntaxKind.SetAccessor && member.symbol.flags & SymbolFlags.GetAccessor) {
24769+
// Already would have reported an error on the getter.
2477724770
break;
24778-
case SyntaxKind.Constructor:
24779-
for (const parameter of (<ConstructorDeclaration>member).parameters) {
24780-
if (!parameter.symbol.isReferenced && hasModifier(parameter, ModifierFlags.Private)) {
24781-
addDiagnostic(parameter, UnusedKind.Local, createDiagnosticForNode(parameter.name, Diagnostics.Property_0_is_declared_but_its_value_is_never_read, symbolName(parameter.symbol)));
24782-
}
24771+
}
24772+
const symbol = getSymbolOfNode(member);
24773+
if (!symbol.isReferenced && hasModifier(member, ModifierFlags.Private)) {
24774+
addDiagnostic(member, UnusedKind.Local, createDiagnosticForNode(member.name!, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolToString(symbol)));
24775+
}
24776+
break;
24777+
case SyntaxKind.Constructor:
24778+
for (const parameter of (<ConstructorDeclaration>member).parameters) {
24779+
if (!parameter.symbol.isReferenced && hasModifier(parameter, ModifierFlags.Private)) {
24780+
addDiagnostic(parameter, UnusedKind.Local, createDiagnosticForNode(parameter.name, Diagnostics.Property_0_is_declared_but_its_value_is_never_read, symbolName(parameter.symbol)));
2478324781
}
24784-
break;
24785-
case SyntaxKind.IndexSignature:
24786-
case SyntaxKind.SemicolonClassElement:
24787-
// Can't be private
24788-
break;
24789-
default:
24790-
Debug.fail();
24791-
}
24782+
}
24783+
break;
24784+
case SyntaxKind.IndexSignature:
24785+
case SyntaxKind.SemicolonClassElement:
24786+
// Can't be private
24787+
break;
24788+
default:
24789+
Debug.fail();
2479224790
}
2479324791
}
2479424792
}
2479524793

24796-
function checkUnusedTypeParameters(node: ClassLikeDeclaration | SignatureDeclaration | InterfaceDeclaration | TypeAliasDeclaration | InferTypeNode, addDiagnostic: AddUnusedDiagnostic): void {
24794+
function checkUnusedInferTypeParameter(node: InferTypeNode, addDiagnostic: AddUnusedDiagnostic): void {
24795+
const { typeParameter } = node;
24796+
if (isTypeParameterUnused(typeParameter)) {
24797+
addDiagnostic(node, UnusedKind.Parameter, createDiagnosticForNode(node, Diagnostics._0_is_declared_but_its_value_is_never_read, idText(typeParameter.name)));
24798+
}
24799+
}
24800+
24801+
function checkUnusedTypeParameters(node: ClassLikeDeclaration | SignatureDeclaration | InterfaceDeclaration | TypeAliasDeclaration, addDiagnostic: AddUnusedDiagnostic): void {
2479724802
// Only report errors on the last declaration for the type parameter container;
2479824803
// this ensures that all uses have been accounted for.
24799-
if (node.flags & NodeFlags.Ambient || node.kind !== SyntaxKind.InferType && last(getSymbolOfNode(node).declarations) !== node) return;
24804+
if (last(getSymbolOfNode(node).declarations) !== node) return;
2480024805

24801-
if (node.kind === SyntaxKind.InferType) {
24802-
const { typeParameter } = node;
24803-
if (isTypeParameterUnused(typeParameter)) {
24804-
addDiagnostic(node, UnusedKind.Parameter, createDiagnosticForNode(node, Diagnostics._0_is_declared_but_its_value_is_never_read, idText(typeParameter.name)));
24805-
}
24806-
}
24807-
else {
24808-
const typeParameters = getEffectiveTypeParameterDeclarations(node);
24809-
const seenParentsWithEveryUnused = new NodeSet<DeclarationWithTypeParameterChildren>();
24810-
24811-
for (const typeParameter of typeParameters) {
24812-
if (!isTypeParameterUnused(typeParameter)) continue;
24813-
24814-
const name = idText(typeParameter.name);
24815-
const { parent } = typeParameter;
24816-
if (parent.kind !== SyntaxKind.InferType && parent.typeParameters!.every(isTypeParameterUnused)) {
24817-
if (seenParentsWithEveryUnused.tryAdd(parent)) {
24818-
const range = isJSDocTemplateTag(parent)
24819-
// Whole @template tag
24820-
? rangeOfNode(parent)
24821-
// Include the `<>` in the error message
24822-
: rangeOfTypeParameters(parent.typeParameters!);
24823-
const only = typeParameters.length === 1;
24824-
const message = only ? Diagnostics._0_is_declared_but_its_value_is_never_read : Diagnostics.All_type_parameters_are_unused;
24825-
const arg0 = only ? name : undefined;
24826-
addDiagnostic(typeParameter, UnusedKind.Parameter, createFileDiagnostic(getSourceFileOfNode(parent), range.pos, range.end - range.pos, message, arg0));
24827-
}
24828-
}
24829-
else {
24830-
addDiagnostic(typeParameter, UnusedKind.Parameter, createDiagnosticForNode(typeParameter, Diagnostics._0_is_declared_but_its_value_is_never_read, name));
24806+
const typeParameters = getEffectiveTypeParameterDeclarations(node);
24807+
const seenParentsWithEveryUnused = new NodeSet<DeclarationWithTypeParameterChildren>();
24808+
24809+
for (const typeParameter of typeParameters) {
24810+
if (!isTypeParameterUnused(typeParameter)) continue;
24811+
24812+
const name = idText(typeParameter.name);
24813+
const { parent } = typeParameter;
24814+
if (parent.kind !== SyntaxKind.InferType && parent.typeParameters!.every(isTypeParameterUnused)) {
24815+
if (seenParentsWithEveryUnused.tryAdd(parent)) {
24816+
const range = isJSDocTemplateTag(parent)
24817+
// Whole @template tag
24818+
? rangeOfNode(parent)
24819+
// Include the `<>` in the error message
24820+
: rangeOfTypeParameters(parent.typeParameters!);
24821+
const only = typeParameters.length === 1;
24822+
const message = only ? Diagnostics._0_is_declared_but_its_value_is_never_read : Diagnostics.All_type_parameters_are_unused;
24823+
const arg0 = only ? name : undefined;
24824+
addDiagnostic(typeParameter, UnusedKind.Parameter, createFileDiagnostic(getSourceFileOfNode(parent), range.pos, range.end - range.pos, message, arg0));
2483124825
}
2483224826
}
24827+
else {
24828+
addDiagnostic(typeParameter, UnusedKind.Parameter, createDiagnosticForNode(typeParameter, Diagnostics._0_is_declared_but_its_value_is_never_read, name));
24829+
}
2483324830
}
2483424831
}
2483524832
function isTypeParameterUnused(typeParameter: TypeParameterDeclaration): boolean {

0 commit comments

Comments
 (0)