Avoid unnecessary newline when inserting node at start of class (#23935)

This commit is contained in:
Andy
2018-05-07 12:40:33 -07:00
committed by GitHub
parent 2604bb4dd3
commit 004a558125
12 changed files with 17 additions and 35 deletions

View File

@@ -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 {

View File

@@ -11,7 +11,6 @@ verify.codeFix({
index: 0,
newFileContent: `class C {
foo: number;
method() {
this.foo = 10;
}

View File

@@ -11,7 +11,6 @@ verify.codeFix({
index: 1,
newFileContent: `class C {
[x: string]: number;
method() {
this.foo = 10;
}

View File

@@ -11,7 +11,6 @@ verify.codeFix({
index: 0,
newFileContent: `class C {
static foo: number;
static method() {
this.foo = 10;
}

View File

@@ -17,7 +17,6 @@ verify.codeFixAll({
y(): any {
throw new Error("Method not implemented.");
}
method() {
this.x = 0;
this.y();

View File

@@ -21,7 +21,6 @@ verify.codeFixAll({
y() {
throw new Error("Method not implemented.");
}
constructor() {
this.x = undefined;
}

View File

@@ -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);

View File

@@ -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<T, U, V, W, X, Y, Z>(): 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<T0, T1, T2, T3, T4, T5, T6, T7>(): any {
throw new Error("Method not implemented.");
}
foo2<T, U, V, W, X, Y, Z>(): 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

View File

@@ -16,7 +16,6 @@ edit.applyRefactor({
public set a(value: string) {
this._a = value;
}
constructor(private /*RENAME*/_a: string) { }
}`,
});

View File

@@ -16,7 +16,6 @@ edit.applyRefactor({
protected set a(value: string) {
this._a = value;
}
constructor(private /*RENAME*/_a: string) { }
}`,
});

View File

@@ -16,7 +16,6 @@ edit.applyRefactor({
public set a(value: string) {
this._a = value;
}
constructor(private /*RENAME*/_a: string) { }
}`,
});

View File

@@ -17,7 +17,6 @@ edit.applyRefactor({
public set a(value: string) {
this._a = value;
}
public a_1: number;
constructor(private /*RENAME*/_a: string) { }
}`,