Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c47df3a

Browse files
authored
Merge pull request microsoft#24438 from vpukhanov/issue-22674
addMethodDeclaration: add after quickfix location if possible
2 parents a8a31ed + 2ea66a6 commit c47df3a

File tree

4 files changed

+35
-28
lines changed

4 files changed

+35
-28
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ 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 containingMethodDeclaration = getAncestor(callExpression, SyntaxKind.MethodDeclaration);
203+
204+
if (containingMethodDeclaration && containingMethodDeclaration.parent === classDeclaration) {
205+
changeTracker.insertNodeAfter(classDeclarationSourceFile, containingMethodDeclaration, methodDeclaration);
206+
}
207+
else {
208+
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration);
209+
}
203210
}
204211
}

tests/cases/fourslash/codeFixAddMissingMember_all.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ verify.codeFixAll({
1414
newFileContent:
1515
`class C {
1616
x: number;
17-
y(): any {
18-
throw new Error("Method not implemented.");
19-
}
2017
method() {
2118
this.x = 0;
2219
this.y();
2320
this.x = "";
2421
}
22+
y(): any {
23+
throw new Error("Method not implemented.");
24+
}
2525
}`,
2626
});

tests/cases/fourslash/codeFixAddMissingMember_all_js.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ verify.codeFixAll({
1818
fixAllDescription: "Add all missing members",
1919
newFileContent:
2020
`class C {
21-
y() {
22-
throw new Error("Method not implemented.");
23-
}
2421
constructor() {
2522
this.x = undefined;
2623
}
@@ -29,5 +26,8 @@ verify.codeFixAll({
2926
this.y();
3027
this.x;
3128
}
29+
y() {
30+
throw new Error("Method not implemented.");
31+
}
3232
}`,
3333
});

tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ verify.codeFix({
1414
index: 0,
1515
newFileContent:
1616
`class A {
17-
static m1(arg0: any, arg1: any, arg2: any): any {
18-
throw new Error("Method not implemented.");
19-
}
2017
static foo0() {
2118
this.m1(1,2,3);
2219
A.m2(1,2);
2320
this.prop1 = 10;
2421
A.prop2 = "asdf";
2522
}
23+
static m1(arg0: any, arg1: any, arg2: any): any {
24+
throw new Error("Method not implemented.");
25+
}
2626
}`,
2727
});
2828

@@ -31,18 +31,18 @@ verify.codeFix({
3131
index: 0,
3232
newFileContent:
3333
`class A {
34-
static m2(arg0: any, arg1: any): any {
35-
throw new Error("Method not implemented.");
36-
}
37-
static m1(arg0: any, arg1: any, arg2: any): any {
38-
throw new Error("Method not implemented.");
39-
}
4034
static foo0() {
4135
this.m1(1,2,3);
4236
A.m2(1,2);
4337
this.prop1 = 10;
4438
A.prop2 = "asdf";
4539
}
40+
static m2(arg0: any, arg1: any): any {
41+
throw new Error("Method not implemented.");
42+
}
43+
static m1(arg0: any, arg1: any, arg2: any): any {
44+
throw new Error("Method not implemented.");
45+
}
4646
}`,
4747
});
4848

@@ -52,18 +52,18 @@ verify.codeFix({
5252
newFileContent:
5353
`class A {
5454
static prop1: number;
55-
static m2(arg0: any, arg1: any): any {
56-
throw new Error("Method not implemented.");
57-
}
58-
static m1(arg0: any, arg1: any, arg2: any): any {
59-
throw new Error("Method not implemented.");
60-
}
6155
static foo0() {
6256
this.m1(1,2,3);
6357
A.m2(1,2);
6458
this.prop1 = 10;
6559
A.prop2 = "asdf";
6660
}
61+
static m2(arg0: any, arg1: any): any {
62+
throw new Error("Method not implemented.");
63+
}
64+
static m1(arg0: any, arg1: any, arg2: any): any {
65+
throw new Error("Method not implemented.");
66+
}
6767
}`,
6868
});
6969

@@ -74,17 +74,17 @@ verify.codeFix({
7474
`class A {
7575
static prop1: number;
7676
static prop2: string;
77-
static m2(arg0: any, arg1: any): any {
78-
throw new Error("Method not implemented.");
79-
}
80-
static m1(arg0: any, arg1: any, arg2: any): any {
81-
throw new Error("Method not implemented.");
82-
}
8377
static foo0() {
8478
this.m1(1,2,3);
8579
A.m2(1,2);
8680
this.prop1 = 10;
8781
A.prop2 = "asdf";
8882
}
83+
static m2(arg0: any, arg1: any): any {
84+
throw new Error("Method not implemented.");
85+
}
86+
static m1(arg0: any, arg1: any, arg2: any): any {
87+
throw new Error("Method not implemented.");
88+
}
8989
}`,
9090
});

0 commit comments

Comments
 (0)