Skip to content

Commit 004a558

Browse files
author
Andy
authored
Avoid unnecessary newline when inserting node at start of class (microsoft#23935)
1 parent 2604bb4 commit 004a558

12 files changed

+17
-35
lines changed

src/services/textChanges.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -442,19 +442,25 @@ namespace ts.textChanges {
442442

443443
public insertNodeAtClassStart(sourceFile: SourceFile, cls: ClassLikeDeclaration, newElement: ClassElement): void {
444444
const clsStart = cls.getStart(sourceFile);
445-
let prefix = "";
446-
let suffix = this.newLineCharacter;
447-
if (addToSeen(this.classesWithNodesInsertedAtStart, getNodeId(cls), cls)) {
448-
prefix = this.newLineCharacter;
449-
// For `class C {\n}`, don't add the trailing "\n"
450-
if (cls.members.length === 0 && !(positionsAreOnSameLine as any)(...getClassBraceEnds(cls, sourceFile), sourceFile)) { // TODO: GH#4130 remove 'as any'
451-
suffix = "";
452-
}
453-
}
454-
455445
const indentation = formatting.SmartIndenter.findFirstNonWhitespaceColumn(getLineStartPositionForPosition(clsStart, sourceFile), clsStart, sourceFile, this.formatContext.options)
456446
+ this.formatContext.options.indentSize;
457-
this.insertNodeAt(sourceFile, cls.members.pos, newElement, { indentation, prefix, suffix });
447+
this.insertNodeAt(sourceFile, cls.members.pos, newElement, { indentation, ...this.getInsertNodeAtClassStartPrefixSuffix(sourceFile, cls) });
448+
}
449+
450+
private getInsertNodeAtClassStartPrefixSuffix(sourceFile: SourceFile, cls: ClassLikeDeclaration): { prefix: string, suffix: string } {
451+
if (cls.members.length === 0) {
452+
if (addToSeen(this.classesWithNodesInsertedAtStart, getNodeId(cls), cls)) {
453+
// For `class C {\n}`, don't add the trailing "\n"
454+
const shouldSuffix = (positionsAreOnSameLine as any)(...getClassBraceEnds(cls, sourceFile), sourceFile); // TODO: GH#4130 remove 'as any'
455+
return { prefix: this.newLineCharacter, suffix: shouldSuffix ? this.newLineCharacter : "" };
456+
}
457+
else {
458+
return { prefix: "", suffix: this.newLineCharacter };
459+
}
460+
}
461+
else {
462+
return { prefix: this.newLineCharacter, suffix: "" };
463+
}
458464
}
459465

460466
public insertNodeAfter(sourceFile: SourceFile, after: Node, newNode: Node): this {

tests/cases/fourslash/codeFixAddMissingMember.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ verify.codeFix({
1111
index: 0,
1212
newFileContent: `class C {
1313
foo: number;
14-
1514
method() {
1615
this.foo = 10;
1716
}

tests/cases/fourslash/codeFixAddMissingMember2.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ verify.codeFix({
1111
index: 1,
1212
newFileContent: `class C {
1313
[x: string]: number;
14-
1514
method() {
1615
this.foo = 10;
1716
}

tests/cases/fourslash/codeFixAddMissingMember3.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ verify.codeFix({
1111
index: 0,
1212
newFileContent: `class C {
1313
static foo: number;
14-
1514
static method() {
1615
this.foo = 10;
1716
}

tests/cases/fourslash/codeFixAddMissingMember_all.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ verify.codeFixAll({
1717
y(): any {
1818
throw new Error("Method not implemented.");
1919
}
20-
2120
method() {
2221
this.x = 0;
2322
this.y();

tests/cases/fourslash/codeFixAddMissingMember_all_js.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ verify.codeFixAll({
2121
y() {
2222
throw new Error("Method not implemented.");
2323
}
24-
2524
constructor() {
2625
this.x = undefined;
2726
}

tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ verify.codeFix({
1717
static m1(arg0: any, arg1: any, arg2: any): any {
1818
throw new Error("Method not implemented.");
1919
}
20-
2120
static foo0() {
2221
this.m1(1,2,3);
2322
A.m2(1,2);
@@ -35,11 +34,9 @@ verify.codeFix({
3534
static m2(arg0: any, arg1: any): any {
3635
throw new Error("Method not implemented.");
3736
}
38-
3937
static m1(arg0: any, arg1: any, arg2: any): any {
4038
throw new Error("Method not implemented.");
4139
}
42-
4340
static foo0() {
4441
this.m1(1,2,3);
4542
A.m2(1,2);
@@ -55,15 +52,12 @@ verify.codeFix({
5552
newFileContent:
5653
`class A {
5754
static prop1: number;
58-
5955
static m2(arg0: any, arg1: any): any {
6056
throw new Error("Method not implemented.");
6157
}
62-
6358
static m1(arg0: any, arg1: any, arg2: any): any {
6459
throw new Error("Method not implemented.");
6560
}
66-
6761
static foo0() {
6862
this.m1(1,2,3);
6963
A.m2(1,2);
@@ -80,15 +74,12 @@ verify.codeFix({
8074
`class A {
8175
static prop1: number;
8276
static prop2: string;
83-
8477
static m2(arg0: any, arg1: any): any {
8578
throw new Error("Method not implemented.");
8679
}
87-
8880
static m1(arg0: any, arg1: any, arg2: any): any {
8981
throw new Error("Method not implemented.");
9082
}
91-
9283
static foo0() {
9384
this.m1(1,2,3);
9485
A.m2(1,2);

tests/cases/fourslash/codeFixUndeclaredMethod.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ verify.codeFix({
1818
foo1(arg0: any, arg1: any, arg2: any): any {
1919
throw new Error("Method not implemented.");
2020
}
21-
2221
constructor() {
2322
this.foo1(1,2,3);
2423
// 7 type args
@@ -37,11 +36,9 @@ verify.codeFix({
3736
foo2<T, U, V, W, X, Y, Z>(): any {
3837
throw new Error("Method not implemented.");
3938
}
40-
4139
foo1(arg0: any, arg1: any, arg2: any): any {
4240
throw new Error("Method not implemented.");
4341
}
44-
4542
constructor() {
4643
this.foo1(1,2,3);
4744
// 7 type args
@@ -60,15 +57,12 @@ verify.codeFix({
6057
foo3<T0, T1, T2, T3, T4, T5, T6, T7>(): any {
6158
throw new Error("Method not implemented.");
6259
}
63-
6460
foo2<T, U, V, W, X, Y, Z>(): any {
6561
throw new Error("Method not implemented.");
6662
}
67-
6863
foo1(arg0: any, arg1: any, arg2: any): any {
6964
throw new Error("Method not implemented.");
7065
}
71-
7266
constructor() {
7367
this.foo1(1,2,3);
7468
// 7 type args

tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess16.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ edit.applyRefactor({
1616
public set a(value: string) {
1717
this._a = value;
1818
}
19-
2019
constructor(private /*RENAME*/_a: string) { }
2120
}`,
2221
});

tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess17.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ edit.applyRefactor({
1616
protected set a(value: string) {
1717
this._a = value;
1818
}
19-
2019
constructor(private /*RENAME*/_a: string) { }
2120
}`,
2221
});

0 commit comments

Comments
 (0)