Skip to content

addMethodDeclaration: add after quickfix location if possible #24438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/services/codefixes/fixAddMissingMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions tests/cases/fourslash/codeFixAddMissingMember_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}`,
});
6 changes: 3 additions & 3 deletions tests/cases/fourslash/codeFixAddMissingMember_all_js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -29,5 +26,8 @@ verify.codeFixAll({
this.y();
this.x;
}
y() {
throw new Error("Method not implemented.");
}
}`,
});
42 changes: 21 additions & 21 deletions tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}`,
});

Expand All @@ -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.");
}
}`,
});

Expand All @@ -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.");
}
}`,
});

Expand All @@ -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.");
}
}`,
});