fixUnusedIdentifier: Remove unused writes (#24805)

This commit is contained in:
Andy 2018-06-08 15:20:33 -07:00 committed by GitHub
parent 96cd34ab26
commit e821d613a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -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);

View 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() { }
}`,
});