Refactor and inline getNodeToInsertMethodAfter

This commit is contained in:
Vyacheslav Pukhanov 2018-05-30 16:58:23 +03:00
parent 1bc5977e3a
commit 2ea66a6dbc

View File

@ -199,34 +199,13 @@ namespace ts.codefix {
preferences: UserPreferences,
): void {
const methodDeclaration = createMethodFromCallExpression(callExpression, token.text, inJs, makeStatic, preferences);
const currentMethod = getNodeToInsertMethodAfter(classDeclaration, callExpression);
const containingMethodDeclaration = getAncestor(callExpression, SyntaxKind.MethodDeclaration);
if (currentMethod) {
changeTracker.insertNodeAfter(classDeclarationSourceFile, currentMethod, methodDeclaration);
if (containingMethodDeclaration && containingMethodDeclaration.parent === classDeclaration) {
changeTracker.insertNodeAfter(classDeclarationSourceFile, containingMethodDeclaration, methodDeclaration);
}
else {
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration);
}
}
// Gets the MethodDeclaration of a method of the cls class that contains the node, or undefined if the node is not in a method or that method is not in the cls class.
function getNodeToInsertMethodAfter(cls: ClassLikeDeclaration, node: Node): MethodDeclaration | undefined {
const nodeMethod = getParentMethodDeclaration(node);
if (nodeMethod) {
const isClsMethod = contains(cls.members, nodeMethod);
if (isClsMethod) {
return nodeMethod;
}
}
return undefined;
}
// Gets the MethodDeclaration of the method that contains the node, or undefined if the node is not in a method.
function getParentMethodDeclaration(node: Node): MethodDeclaration | undefined {
while (node) {
if (isMethodDeclaration(node)) break;
node = node.parent;
}
return node;
}
}