Skip to content

Commit e821d61

Browse files
author
Andy
authored
fixUnusedIdentifier: Remove unused writes (microsoft#24805)
1 parent 96cd34a commit e821d61

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ namespace ts.codefix {
152152
switch (token.kind) {
153153
case SyntaxKind.Identifier:
154154
tryDeleteIdentifier(changes, sourceFile, <Identifier>token, deletedAncestors, checker, isFixAll);
155+
deleteAssignments(changes, sourceFile, token as Identifier, checker);
155156
break;
156157
case SyntaxKind.PropertyDeclaration:
157158
case SyntaxKind.NamespaceImport:
@@ -163,6 +164,15 @@ namespace ts.codefix {
163164
}
164165
}
165166

167+
function deleteAssignments(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Identifier, checker: TypeChecker) {
168+
FindAllReferences.Core.eachSymbolReferenceInFile(token, checker, sourceFile, (ref: Node) => {
169+
if (ref.parent.kind === SyntaxKind.PropertyAccessExpression) ref = ref.parent;
170+
if (ref.parent.kind === SyntaxKind.BinaryExpression && ref.parent.parent.kind === SyntaxKind.ExpressionStatement) {
171+
changes.deleteNode(sourceFile, ref.parent.parent);
172+
}
173+
});
174+
}
175+
166176
function tryDeleteDefault(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, deletedAncestors: NodeSet | undefined): void {
167177
if (isDeclarationName(token)) {
168178
if (deletedAncestors) deletedAncestors.add(token.parent);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noLib: true
4+
// @noUnusedLocals: true
5+
6+
////let x = 0;
7+
////x = 1;
8+
////
9+
////export class C {
10+
//// private p: number;
11+
////
12+
//// m() { this.p = 0; }
13+
////}
14+
15+
verify.codeFixAll({
16+
fixId: "unusedIdentifier_delete",
17+
fixAllDescription: "Delete all unused declarations",
18+
newFileContent:
19+
`
20+
export class C {
21+
22+
m() { }
23+
}`,
24+
});

0 commit comments

Comments
 (0)