Skip to content

Commit 2ea66a6

Browse files
committed
Refactor and inline getNodeToInsertMethodAfter
1 parent 1bc5977 commit 2ea66a6

File tree

1 file changed

+3
-24
lines changed

1 file changed

+3
-24
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -199,34 +199,13 @@ namespace ts.codefix {
199199
preferences: UserPreferences,
200200
): void {
201201
const methodDeclaration = createMethodFromCallExpression(callExpression, token.text, inJs, makeStatic, preferences);
202-
const currentMethod = getNodeToInsertMethodAfter(classDeclaration, callExpression);
202+
const containingMethodDeclaration = getAncestor(callExpression, SyntaxKind.MethodDeclaration);
203203

204-
if (currentMethod) {
205-
changeTracker.insertNodeAfter(classDeclarationSourceFile, currentMethod, methodDeclaration);
204+
if (containingMethodDeclaration && containingMethodDeclaration.parent === classDeclaration) {
205+
changeTracker.insertNodeAfter(classDeclarationSourceFile, containingMethodDeclaration, methodDeclaration);
206206
}
207207
else {
208208
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration);
209209
}
210210
}
211-
212-
// 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.
213-
function getNodeToInsertMethodAfter(cls: ClassLikeDeclaration, node: Node): MethodDeclaration | undefined {
214-
const nodeMethod = getParentMethodDeclaration(node);
215-
if (nodeMethod) {
216-
const isClsMethod = contains(cls.members, nodeMethod);
217-
if (isClsMethod) {
218-
return nodeMethod;
219-
}
220-
}
221-
return undefined;
222-
}
223-
224-
// Gets the MethodDeclaration of the method that contains the node, or undefined if the node is not in a method.
225-
function getParentMethodDeclaration(node: Node): MethodDeclaration | undefined {
226-
while (node) {
227-
if (isMethodDeclaration(node)) break;
228-
node = node.parent;
229-
}
230-
return node;
231-
}
232211
}

0 commit comments

Comments
 (0)