From 2ea66a6dbcb3f90790a1de07f15c3ea124c484b8 Mon Sep 17 00:00:00 2001 From: Vyacheslav Pukhanov Date: Wed, 30 May 2018 16:58:23 +0300 Subject: [PATCH] Refactor and inline getNodeToInsertMethodAfter --- src/services/codefixes/fixAddMissingMember.ts | 27 +++---------------- 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/src/services/codefixes/fixAddMissingMember.ts b/src/services/codefixes/fixAddMissingMember.ts index ccf5829f363..53492211042 100644 --- a/src/services/codefixes/fixAddMissingMember.ts +++ b/src/services/codefixes/fixAddMissingMember.ts @@ -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; - } }