diff --git a/src/services/codefixes/fixAddMissingMember.ts b/src/services/codefixes/fixAddMissingMember.ts index 2eafb5cdd6f..53492211042 100644 --- a/src/services/codefixes/fixAddMissingMember.ts +++ b/src/services/codefixes/fixAddMissingMember.ts @@ -199,6 +199,13 @@ namespace ts.codefix { preferences: UserPreferences, ): void { const methodDeclaration = createMethodFromCallExpression(callExpression, token.text, inJs, makeStatic, preferences); - changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration); + const containingMethodDeclaration = getAncestor(callExpression, SyntaxKind.MethodDeclaration); + + if (containingMethodDeclaration && containingMethodDeclaration.parent === classDeclaration) { + changeTracker.insertNodeAfter(classDeclarationSourceFile, containingMethodDeclaration, methodDeclaration); + } + else { + changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration); + } } } diff --git a/tests/cases/fourslash/codeFixAddMissingMember_all.ts b/tests/cases/fourslash/codeFixAddMissingMember_all.ts index 5a2f8606f2d..021968a8c6a 100644 --- a/tests/cases/fourslash/codeFixAddMissingMember_all.ts +++ b/tests/cases/fourslash/codeFixAddMissingMember_all.ts @@ -14,13 +14,13 @@ verify.codeFixAll({ newFileContent: `class C { x: number; - y(): any { - throw new Error("Method not implemented."); - } method() { this.x = 0; this.y(); this.x = ""; } + y(): any { + throw new Error("Method not implemented."); + } }`, }); diff --git a/tests/cases/fourslash/codeFixAddMissingMember_all_js.ts b/tests/cases/fourslash/codeFixAddMissingMember_all_js.ts index 5e5a2895fdc..7c900be76f3 100644 --- a/tests/cases/fourslash/codeFixAddMissingMember_all_js.ts +++ b/tests/cases/fourslash/codeFixAddMissingMember_all_js.ts @@ -18,9 +18,6 @@ verify.codeFixAll({ fixAllDescription: "Add all missing members", newFileContent: `class C { - y() { - throw new Error("Method not implemented."); - } constructor() { this.x = undefined; } @@ -29,5 +26,8 @@ verify.codeFixAll({ this.y(); this.x; } + y() { + throw new Error("Method not implemented."); + } }`, }); diff --git a/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts b/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts index 60693a22fca..9faa15745fc 100644 --- a/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts +++ b/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts @@ -14,15 +14,15 @@ verify.codeFix({ index: 0, newFileContent: `class A { - static m1(arg0: any, arg1: any, arg2: any): any { - throw new Error("Method not implemented."); - } static foo0() { this.m1(1,2,3); A.m2(1,2); this.prop1 = 10; A.prop2 = "asdf"; } + static m1(arg0: any, arg1: any, arg2: any): any { + throw new Error("Method not implemented."); + } }`, }); @@ -31,18 +31,18 @@ verify.codeFix({ index: 0, newFileContent: `class A { - static m2(arg0: any, arg1: any): any { - throw new Error("Method not implemented."); - } - static m1(arg0: any, arg1: any, arg2: any): any { - throw new Error("Method not implemented."); - } static foo0() { this.m1(1,2,3); A.m2(1,2); this.prop1 = 10; A.prop2 = "asdf"; } + static m2(arg0: any, arg1: any): any { + throw new Error("Method not implemented."); + } + static m1(arg0: any, arg1: any, arg2: any): any { + throw new Error("Method not implemented."); + } }`, }); @@ -52,18 +52,18 @@ verify.codeFix({ newFileContent: `class A { static prop1: number; - static m2(arg0: any, arg1: any): any { - throw new Error("Method not implemented."); - } - static m1(arg0: any, arg1: any, arg2: any): any { - throw new Error("Method not implemented."); - } static foo0() { this.m1(1,2,3); A.m2(1,2); this.prop1 = 10; A.prop2 = "asdf"; } + static m2(arg0: any, arg1: any): any { + throw new Error("Method not implemented."); + } + static m1(arg0: any, arg1: any, arg2: any): any { + throw new Error("Method not implemented."); + } }`, }); @@ -74,17 +74,17 @@ verify.codeFix({ `class A { static prop1: number; static prop2: string; - static m2(arg0: any, arg1: any): any { - throw new Error("Method not implemented."); - } - static m1(arg0: any, arg1: any, arg2: any): any { - throw new Error("Method not implemented."); - } static foo0() { this.m1(1,2,3); A.m2(1,2); this.prop1 = 10; A.prop2 = "asdf"; } + static m2(arg0: any, arg1: any): any { + throw new Error("Method not implemented."); + } + static m1(arg0: any, arg1: any, arg2: any): any { + throw new Error("Method not implemented."); + } }`, });