@@ -311,7 +311,22 @@ namespace ts {
311
311
return node && getTypeArgumentConstraint(node);
312
312
},
313
313
314
- getSuggestionDiagnostics: file => (suggestionDiagnostics.get(file.fileName) || emptyArray).concat(getUnusedDiagnostics(file)),
314
+ getSuggestionDiagnostics: file => {
315
+ return (suggestionDiagnostics.get(file.fileName) || emptyArray).concat(getUnusedDiagnostics());
316
+ function getUnusedDiagnostics(): ReadonlyArray<Diagnostic> {
317
+ checkSourceFile(file);
318
+ const diagnostics: Diagnostic[] = [];
319
+ Debug.assert(!!(getNodeLinks(file).flags & NodeCheckFlags.TypeChecked));
320
+ if (!file.isDeclarationFile) {
321
+ checkUnusedIdentifiers(allPotentiallyUnusedIdentifiers.get(file.fileName)!, (kind, diag) => {
322
+ if (!unusedIsError(kind)) {
323
+ diagnostics.push({ ...diag, category: DiagnosticCategory.Suggestion });
324
+ }
325
+ });
326
+ }
327
+ return diagnostics;
328
+ }
329
+ },
315
330
};
316
331
317
332
const tupleTypes: GenericType[] = [];
@@ -18732,7 +18747,6 @@ namespace ts {
18732
18747
return type;
18733
18748
}
18734
18749
18735
- //why?
18736
18750
function checkFunctionExpressionOrObjectLiteralMethodDeferred(node: ArrowFunction | FunctionExpression | MethodDeclaration) {
18737
18751
Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node));
18738
18752
@@ -21564,11 +21578,16 @@ namespace ts {
21564
21578
}
21565
21579
21566
21580
function registerForUnusedIdentifiersCheck(node: PotentiallyUnusedIdentifier): void {
21581
+ // May be in a call such as getTypeOfNode that happened to call this. But potentiallyUnusedIdentifiers is only defined in the scope of `checkSourceFile`.
21582
+ if (potentiallyUnusedIdentifiers === undefined) return;
21583
+
21567
21584
if (contains(potentiallyUnusedIdentifiers, node)) {
21568
- // TODO: #22491 Apparently we check the BlockStatement in the callback in `getPropertyAssignment` twice.
21585
+ // TODO: GH #22491 Apparently we check the BlockStatement in the callback in `getPropertyAssignment` twice.
21569
21586
// Debug.fail();
21570
- } else
21587
+ }
21588
+ else {
21571
21589
potentiallyUnusedIdentifiers.push(node);
21590
+ }
21572
21591
}
21573
21592
21574
21593
type PotentiallyUnusedIdentifier =
@@ -21581,15 +21600,15 @@ namespace ts {
21581
21600
switch (node.kind) {
21582
21601
case SyntaxKind.SourceFile:
21583
21602
case SyntaxKind.ModuleDeclaration:
21584
- checkUnusedModuleMembers(<ModuleDeclaration | SourceFile> node, addDiagnostic);
21603
+ checkUnusedModuleMembers(node, addDiagnostic);
21585
21604
break;
21586
21605
case SyntaxKind.ClassDeclaration:
21587
21606
case SyntaxKind.ClassExpression:
21588
- checkUnusedClassMembers(<ClassDeclaration | ClassExpression> node, addDiagnostic);
21589
- checkUnusedTypeParameters(<ClassDeclaration | ClassExpression> node, addDiagnostic);
21607
+ checkUnusedClassMembers(node, addDiagnostic);
21608
+ checkUnusedTypeParameters(node, addDiagnostic);
21590
21609
break;
21591
21610
case SyntaxKind.InterfaceDeclaration:
21592
- checkUnusedTypeParameters(<InterfaceDeclaration> node, addDiagnostic);
21611
+ checkUnusedTypeParameters(node, addDiagnostic);
21593
21612
break;
21594
21613
case SyntaxKind.Block:
21595
21614
case SyntaxKind.CaseBlock:
@@ -21605,18 +21624,18 @@ namespace ts {
21605
21624
case SyntaxKind.MethodDeclaration:
21606
21625
case SyntaxKind.GetAccessor:
21607
21626
case SyntaxKind.SetAccessor:
21608
- if ((<FunctionLikeDeclaration> node) .body) {
21609
- checkUnusedLocalsAndParameters(<FunctionLikeDeclaration> node, addDiagnostic);
21627
+ if (node.body) {
21628
+ checkUnusedLocalsAndParameters(node, addDiagnostic);
21610
21629
}
21611
- checkUnusedTypeParameters(<FunctionLikeDeclaration> node, addDiagnostic);
21630
+ checkUnusedTypeParameters(node, addDiagnostic);
21612
21631
break;
21613
21632
case SyntaxKind.MethodSignature:
21614
21633
case SyntaxKind.CallSignature:
21615
21634
case SyntaxKind.ConstructSignature:
21616
21635
case SyntaxKind.FunctionType:
21617
21636
case SyntaxKind.ConstructorType:
21618
21637
case SyntaxKind.TypeAliasDeclaration:
21619
- checkUnusedTypeParameters(<MethodSignature | CallSignatureDeclaration | ConstructSignatureDeclaration | FunctionTypeNode | ConstructorTypeNode | TypeAliasDeclaration> node, addDiagnostic);
21638
+ checkUnusedTypeParameters(node, addDiagnostic);
21620
21639
break;
21621
21640
default:
21622
21641
Debug.assertNever(node, "Node should not have been registered for unused identifiers check");
@@ -24549,19 +24568,6 @@ namespace ts {
24549
24568
}
24550
24569
}
24551
24570
24552
- function getUnusedDiagnostics(node: SourceFile): ReadonlyArray<Diagnostic> {
24553
- const diagnostics: Diagnostic[] = [];
24554
- Debug.assert(!!(getNodeLinks(node).flags & NodeCheckFlags.TypeChecked));
24555
- if (!node.isDeclarationFile) {
24556
- checkUnusedIdentifiers(allPotentiallyUnusedIdentifiers.get(node.fileName)!, (kind, diag) => {
24557
- if (!unusedIsError(kind)) {
24558
- diagnostics.push(diag);
24559
- }
24560
- });
24561
- }
24562
- return diagnostics;
24563
- }
24564
-
24565
24571
// Fully type check a source file and collect the relevant diagnostics.
24566
24572
function checkSourceFileWorker(node: SourceFile) {
24567
24573
const links = getNodeLinks(node);
@@ -24593,7 +24599,7 @@ namespace ts {
24593
24599
registerForUnusedIdentifiersCheck(node);
24594
24600
}
24595
24601
24596
- if (!node.isDeclarationFile) { // && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters)) {
24602
+ if (!node.isDeclarationFile && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters)) {
24597
24603
checkUnusedIdentifiers(potentiallyUnusedIdentifiers, (kind, diag) => {
24598
24604
if (unusedIsError(kind)) {
24599
24605
diagnostics.add(diag);
0 commit comments