mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 11:54:44 -06:00
Refactor and inline getNodeToInsertMethodAfter
This commit is contained in:
parent
1bc5977e3a
commit
2ea66a6dbc
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user