Skip to content

Commit 1a7d4b3

Browse files
committed
addMethodDeclaration: add after quickfix location if possible (microsoft#22674)
1 parent 927343c commit 1a7d4b3

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,34 @@ namespace ts.codefix {
199199
preferences: UserPreferences,
200200
): void {
201201
const methodDeclaration = createMethodFromCallExpression(callExpression, token.text, inJs, makeStatic, preferences);
202-
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration);
202+
const currentMethod = getNodeToInsertMethodAfter(classDeclaration, callExpression);
203+
204+
if (currentMethod) {
205+
changeTracker.insertNodeAfter(classDeclarationSourceFile, currentMethod, methodDeclaration);
206+
}
207+
else {
208+
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration);
209+
}
210+
}
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;
203231
}
204232
}

0 commit comments

Comments
 (0)