Simplify tryDeleteDeclaration (#24808)

This commit is contained in:
Andy 2018-06-11 13:10:29 -07:00 committed by GitHub
parent e07e2e0e1f
commit ed20f7d983
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -148,20 +148,9 @@ namespace ts.codefix {
return false; return false;
} }
function tryDeleteDeclaration(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, deletedAncestors: NodeSet | undefined, checker: TypeChecker, isFixAll: boolean): void { function tryDeleteDeclaration(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, deletedAncestors: NodeSet | undefined, checker: TypeChecker, isFixAll: boolean) {
switch (token.kind) { tryDeleteDeclarationWorker(changes, sourceFile, token, deletedAncestors, checker, isFixAll);
case SyntaxKind.Identifier: if (isIdentifier(token)) deleteAssignments(changes, sourceFile, token, checker);
tryDeleteIdentifier(changes, sourceFile, <Identifier>token, deletedAncestors, checker, isFixAll);
deleteAssignments(changes, sourceFile, token as Identifier, checker);
break;
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.NamespaceImport:
if (deletedAncestors) deletedAncestors.add(token.parent);
changes.deleteNode(sourceFile, token.parent);
break;
default:
tryDeleteDefault(changes, sourceFile, token, deletedAncestors);
}
} }
function deleteAssignments(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Identifier, checker: TypeChecker) { function deleteAssignments(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Identifier, checker: TypeChecker) {
@ -173,19 +162,8 @@ namespace ts.codefix {
}); });
} }
function tryDeleteDefault(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, deletedAncestors: NodeSet | undefined): void { function tryDeleteDeclarationWorker(changes: textChanges.ChangeTracker, sourceFile: SourceFile, token: Node, deletedAncestors: NodeSet | undefined, checker: TypeChecker, isFixAll: boolean): void {
if (isDeclarationName(token)) { const parent = token.parent;
if (deletedAncestors) deletedAncestors.add(token.parent);
changes.deleteNode(sourceFile, token.parent);
}
else if (isLiteralComputedPropertyDeclarationName(token)) {
if (deletedAncestors) deletedAncestors.add(token.parent.parent);
changes.deleteNode(sourceFile, token.parent.parent);
}
}
function tryDeleteIdentifier(changes: textChanges.ChangeTracker, sourceFile: SourceFile, identifier: Identifier, deletedAncestors: NodeSet | undefined, checker: TypeChecker, isFixAll: boolean): void {
const parent = identifier.parent;
switch (parent.kind) { switch (parent.kind) {
case SyntaxKind.VariableDeclaration: case SyntaxKind.VariableDeclaration:
tryDeleteVariableDeclaration(changes, sourceFile, <VariableDeclaration>parent, deletedAncestors); tryDeleteVariableDeclaration(changes, sourceFile, <VariableDeclaration>parent, deletedAncestors);
@ -250,7 +228,7 @@ namespace ts.codefix {
// handle case where 'import a = A;' // handle case where 'import a = A;'
case SyntaxKind.ImportEqualsDeclaration: case SyntaxKind.ImportEqualsDeclaration:
const importEquals = getAncestor(identifier, SyntaxKind.ImportEqualsDeclaration)!; const importEquals = getAncestor(token, SyntaxKind.ImportEqualsDeclaration)!;
changes.deleteNode(sourceFile, importEquals); changes.deleteNode(sourceFile, importEquals);
break; break;
@ -290,7 +268,14 @@ namespace ts.codefix {
break; break;
default: default:
tryDeleteDefault(changes, sourceFile, identifier, deletedAncestors); if (isDeclarationName(token)) {
if (deletedAncestors) deletedAncestors.add(token.parent);
changes.deleteNode(sourceFile, token.parent);
}
else if (isLiteralComputedPropertyDeclarationName(token)) {
if (deletedAncestors) deletedAncestors.add(token.parent.parent);
changes.deleteNode(sourceFile, token.parent.parent);
}
break; break;
} }
} }