Skip to content

Commit 93451e8

Browse files
authored
fix(57497): "Remove unused declaration" does not work on overloaded function declarations (#57517)
1 parent c0ac582 commit 93451e8

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
isComputedPropertyName,
2828
isDeclarationWithTypeParameterChildren,
2929
isExpressionStatement,
30+
isFunctionDeclaration,
3031
isIdentifier,
3132
isImportClause,
3233
isImportDeclaration,
@@ -129,6 +130,10 @@ registerCodeFix({
129130
];
130131
}
131132

133+
if (isIdentifier(token) && isFunctionDeclaration(token.parent)) {
134+
return [createDeleteFix(textChanges.ChangeTracker.with(context, t => deleteFunctionLikeDeclaration(t, sourceFile, token.parent as FunctionLikeDeclaration)), [Diagnostics.Remove_unused_declaration_for_Colon_0, token.getText(sourceFile)])];
135+
}
136+
132137
const result: CodeFixAction[] = [];
133138
if (token.kind === SyntaxKind.InferKeyword) {
134139
const changes = textChanges.ChangeTracker.with(context, t => changeInferToUnknown(t, sourceFile, token));
@@ -431,3 +436,12 @@ function mayDeleteExpression(node: Node) {
431436
return ((isBinaryExpression(node.parent) && node.parent.left === node) ||
432437
((isPostfixUnaryExpression(node.parent) || isPrefixUnaryExpression(node.parent)) && node.parent.operand === node)) && isExpressionStatement(node.parent.parent);
433438
}
439+
440+
function deleteFunctionLikeDeclaration(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: FunctionLikeDeclaration) {
441+
const declarations = node.symbol.declarations;
442+
if (declarations) {
443+
for (const declaration of declarations) {
444+
changes.delete(sourceFile, declaration);
445+
}
446+
}
447+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
5+
////function foo(): number;
6+
////function foo(x: number, y: number): number;
7+
////function foo(x?: number, y?: number): number {
8+
//// return 1234;
9+
////}
10+
////
11+
////export {}
12+
13+
verify.codeFix({
14+
index: 0,
15+
description: "Remove unused declaration for: 'foo'",
16+
newFileContent:
17+
`
18+
export {}`,
19+
});

0 commit comments

Comments
 (0)