diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index d5ef90ff3ef..eca1c639196 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -442,19 +442,25 @@ namespace ts.textChanges { public insertNodeAtClassStart(sourceFile: SourceFile, cls: ClassLikeDeclaration, newElement: ClassElement): void { const clsStart = cls.getStart(sourceFile); - let prefix = ""; - let suffix = this.newLineCharacter; - if (addToSeen(this.classesWithNodesInsertedAtStart, getNodeId(cls), cls)) { - prefix = this.newLineCharacter; - // For `class C {\n}`, don't add the trailing "\n" - if (cls.members.length === 0 && !(positionsAreOnSameLine as any)(...getClassBraceEnds(cls, sourceFile), sourceFile)) { // TODO: GH#4130 remove 'as any' - suffix = ""; - } - } - const indentation = formatting.SmartIndenter.findFirstNonWhitespaceColumn(getLineStartPositionForPosition(clsStart, sourceFile), clsStart, sourceFile, this.formatContext.options) + this.formatContext.options.indentSize; - this.insertNodeAt(sourceFile, cls.members.pos, newElement, { indentation, prefix, suffix }); + this.insertNodeAt(sourceFile, cls.members.pos, newElement, { indentation, ...this.getInsertNodeAtClassStartPrefixSuffix(sourceFile, cls) }); + } + + private getInsertNodeAtClassStartPrefixSuffix(sourceFile: SourceFile, cls: ClassLikeDeclaration): { prefix: string, suffix: string } { + if (cls.members.length === 0) { + if (addToSeen(this.classesWithNodesInsertedAtStart, getNodeId(cls), cls)) { + // For `class C {\n}`, don't add the trailing "\n" + const shouldSuffix = (positionsAreOnSameLine as any)(...getClassBraceEnds(cls, sourceFile), sourceFile); // TODO: GH#4130 remove 'as any' + return { prefix: this.newLineCharacter, suffix: shouldSuffix ? this.newLineCharacter : "" }; + } + else { + return { prefix: "", suffix: this.newLineCharacter }; + } + } + else { + return { prefix: this.newLineCharacter, suffix: "" }; + } } public insertNodeAfter(sourceFile: SourceFile, after: Node, newNode: Node): this { diff --git a/tests/cases/fourslash/codeFixAddMissingMember.ts b/tests/cases/fourslash/codeFixAddMissingMember.ts index c507d8b80c9..f5bdf567b20 100644 --- a/tests/cases/fourslash/codeFixAddMissingMember.ts +++ b/tests/cases/fourslash/codeFixAddMissingMember.ts @@ -11,7 +11,6 @@ verify.codeFix({ index: 0, newFileContent: `class C { foo: number; - method() { this.foo = 10; } diff --git a/tests/cases/fourslash/codeFixAddMissingMember2.ts b/tests/cases/fourslash/codeFixAddMissingMember2.ts index b19632dd104..a4b7ffb3bfe 100644 --- a/tests/cases/fourslash/codeFixAddMissingMember2.ts +++ b/tests/cases/fourslash/codeFixAddMissingMember2.ts @@ -11,7 +11,6 @@ verify.codeFix({ index: 1, newFileContent: `class C { [x: string]: number; - method() { this.foo = 10; } diff --git a/tests/cases/fourslash/codeFixAddMissingMember3.ts b/tests/cases/fourslash/codeFixAddMissingMember3.ts index 88ea2efef66..82512406985 100644 --- a/tests/cases/fourslash/codeFixAddMissingMember3.ts +++ b/tests/cases/fourslash/codeFixAddMissingMember3.ts @@ -11,7 +11,6 @@ verify.codeFix({ index: 0, newFileContent: `class C { static foo: number; - static method() { this.foo = 10; } diff --git a/tests/cases/fourslash/codeFixAddMissingMember_all.ts b/tests/cases/fourslash/codeFixAddMissingMember_all.ts index 0e7abb79cc2..5a2f8606f2d 100644 --- a/tests/cases/fourslash/codeFixAddMissingMember_all.ts +++ b/tests/cases/fourslash/codeFixAddMissingMember_all.ts @@ -17,7 +17,6 @@ verify.codeFixAll({ y(): any { throw new Error("Method not implemented."); } - method() { this.x = 0; this.y(); diff --git a/tests/cases/fourslash/codeFixAddMissingMember_all_js.ts b/tests/cases/fourslash/codeFixAddMissingMember_all_js.ts index 48455943af5..5e5a2895fdc 100644 --- a/tests/cases/fourslash/codeFixAddMissingMember_all_js.ts +++ b/tests/cases/fourslash/codeFixAddMissingMember_all_js.ts @@ -21,7 +21,6 @@ verify.codeFixAll({ y() { throw new Error("Method not implemented."); } - constructor() { this.x = undefined; } diff --git a/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts b/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts index 84b0ebd4414..60693a22fca 100644 --- a/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts +++ b/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts @@ -17,7 +17,6 @@ verify.codeFix({ 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); @@ -35,11 +34,9 @@ verify.codeFix({ 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); @@ -55,15 +52,12 @@ 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); @@ -80,15 +74,12 @@ 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); diff --git a/tests/cases/fourslash/codeFixUndeclaredMethod.ts b/tests/cases/fourslash/codeFixUndeclaredMethod.ts index b85e5fbfdfa..41e27dcf207 100644 --- a/tests/cases/fourslash/codeFixUndeclaredMethod.ts +++ b/tests/cases/fourslash/codeFixUndeclaredMethod.ts @@ -18,7 +18,6 @@ verify.codeFix({ foo1(arg0: any, arg1: any, arg2: any): any { throw new Error("Method not implemented."); } - constructor() { this.foo1(1,2,3); // 7 type args @@ -37,11 +36,9 @@ verify.codeFix({ foo2(): any { throw new Error("Method not implemented."); } - foo1(arg0: any, arg1: any, arg2: any): any { throw new Error("Method not implemented."); } - constructor() { this.foo1(1,2,3); // 7 type args @@ -60,15 +57,12 @@ verify.codeFix({ foo3(): any { throw new Error("Method not implemented."); } - foo2(): any { throw new Error("Method not implemented."); } - foo1(arg0: any, arg1: any, arg2: any): any { throw new Error("Method not implemented."); } - constructor() { this.foo1(1,2,3); // 7 type args diff --git a/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess16.ts b/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess16.ts index bb2e20eaa8f..5206eada35d 100644 --- a/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess16.ts +++ b/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess16.ts @@ -16,7 +16,6 @@ edit.applyRefactor({ public set a(value: string) { this._a = value; } - constructor(private /*RENAME*/_a: string) { } }`, }); diff --git a/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess17.ts b/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess17.ts index 85d5c001490..71e058b7c83 100644 --- a/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess17.ts +++ b/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess17.ts @@ -16,7 +16,6 @@ edit.applyRefactor({ protected set a(value: string) { this._a = value; } - constructor(private /*RENAME*/_a: string) { } }`, }); diff --git a/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess18.ts b/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess18.ts index 4bc91af7ff5..f1203730b3c 100644 --- a/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess18.ts +++ b/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess18.ts @@ -16,7 +16,6 @@ edit.applyRefactor({ public set a(value: string) { this._a = value; } - constructor(private /*RENAME*/_a: string) { } }`, }); diff --git a/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess22.ts b/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess22.ts index 68622150edc..ceec2721709 100644 --- a/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess22.ts +++ b/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess22.ts @@ -17,7 +17,6 @@ edit.applyRefactor({ public set a(value: string) { this._a = value; } - public a_1: number; constructor(private /*RENAME*/_a: string) { } }`,