fix(57497): "Remove unused declaration" does not work on overloaded function declarations (#57517)

This commit is contained in:
Oleksandr T 2024-04-19 21:28:00 +03:00 committed by GitHub
parent c0ac582284
commit 93451e8dd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 0 deletions

View File

@ -27,6 +27,7 @@ import {
isComputedPropertyName,
isDeclarationWithTypeParameterChildren,
isExpressionStatement,
isFunctionDeclaration,
isIdentifier,
isImportClause,
isImportDeclaration,
@ -129,6 +130,10 @@ registerCodeFix({
];
}
if (isIdentifier(token) && isFunctionDeclaration(token.parent)) {
return [createDeleteFix(textChanges.ChangeTracker.with(context, t => deleteFunctionLikeDeclaration(t, sourceFile, token.parent as FunctionLikeDeclaration)), [Diagnostics.Remove_unused_declaration_for_Colon_0, token.getText(sourceFile)])];
}
const result: CodeFixAction[] = [];
if (token.kind === SyntaxKind.InferKeyword) {
const changes = textChanges.ChangeTracker.with(context, t => changeInferToUnknown(t, sourceFile, token));
@ -431,3 +436,12 @@ function mayDeleteExpression(node: Node) {
return ((isBinaryExpression(node.parent) && node.parent.left === node) ||
((isPostfixUnaryExpression(node.parent) || isPrefixUnaryExpression(node.parent)) && node.parent.operand === node)) && isExpressionStatement(node.parent.parent);
}
function deleteFunctionLikeDeclaration(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: FunctionLikeDeclaration) {
const declarations = node.symbol.declarations;
if (declarations) {
for (const declaration of declarations) {
changes.delete(sourceFile, declaration);
}
}
}

View File

@ -0,0 +1,19 @@
/// <reference path="fourslash.ts" />
// @noUnusedLocals: true
////function foo(): number;
////function foo(x: number, y: number): number;
////function foo(x?: number, y?: number): number {
//// return 1234;
////}
////
////export {}
verify.codeFix({
index: 0,
description: "Remove unused declaration for: 'foo'",
newFileContent:
`
export {}`,
});