mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
fixUnusedIdentifier: Remove unused writes (#24805)
This commit is contained in:
parent
96cd34ab26
commit
e821d613a1
@ -152,6 +152,7 @@ namespace ts.codefix {
|
||||
switch (token.kind) {
|
||||
case SyntaxKind.Identifier:
|
||||
tryDeleteIdentifier(changes, sourceFile, <Identifier>token, deletedAncestors, checker, isFixAll);
|
||||
deleteAssignments(changes, sourceFile, token as Identifier, checker);
|
||||
break;
|
||||
case SyntaxKind.PropertyDeclaration:
|
||||
case SyntaxKind.NamespaceImport:
|
||||
@ -163,6 +164,15 @@ namespace ts.codefix {
|
||||
}
|
||||
}
|
||||
|
||||
function deleteAssignments(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Identifier, checker: TypeChecker) {
|
||||
FindAllReferences.Core.eachSymbolReferenceInFile(token, checker, sourceFile, (ref: Node) => {
|
||||
if (ref.parent.kind === SyntaxKind.PropertyAccessExpression) ref = ref.parent;
|
||||
if (ref.parent.kind === SyntaxKind.BinaryExpression && ref.parent.parent.kind === SyntaxKind.ExpressionStatement) {
|
||||
changes.deleteNode(sourceFile, ref.parent.parent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function tryDeleteDefault(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, deletedAncestors: NodeSet | undefined): void {
|
||||
if (isDeclarationName(token)) {
|
||||
if (deletedAncestors) deletedAncestors.add(token.parent);
|
||||
|
||||
24
tests/cases/fourslash/codeFixUnusedIdentifier_deleteWrite.ts
Normal file
24
tests/cases/fourslash/codeFixUnusedIdentifier_deleteWrite.ts
Normal file
@ -0,0 +1,24 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
// @noLib: true
|
||||
// @noUnusedLocals: true
|
||||
|
||||
////let x = 0;
|
||||
////x = 1;
|
||||
////
|
||||
////export class C {
|
||||
//// private p: number;
|
||||
////
|
||||
//// m() { this.p = 0; }
|
||||
////}
|
||||
|
||||
verify.codeFixAll({
|
||||
fixId: "unusedIdentifier_delete",
|
||||
fixAllDescription: "Delete all unused declarations",
|
||||
newFileContent:
|
||||
`
|
||||
export class C {
|
||||
|
||||
m() { }
|
||||
}`,
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user