Skip to content

Fix(54375) : Removing extra new lines from move to file #55073

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ function getNewStatementsAndRemoveFromOldFile(
moveStatementsToTargetFile(changes, program, body, targetFile, toMove);
}
else {
changes.insertNodesAtEndOfFile(targetFile, body, /*blankLineBetween*/ false);
changes.insertNodesAtEndOfFile(targetFile, body, /*blankLineSuffix*/ false, !imports || usage.oldImportsNeededByTargetFile.size === 0 ? true : false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can imports have length 0?
Could you add a comment explaining when we want to add a blank line prefix?

}
if (imports.length > 0) {
insertImports(changes, targetFile, imports, /*blankLineBetween*/ true, preferences);
insertImports(changes, targetFile, imports, usage.oldImportsNeededByTargetFile.size === 0 ? true : false, preferences);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, could you add a comment explaining this?

}
}
if (importAdder) {
Expand Down Expand Up @@ -1194,7 +1194,9 @@ function moveStatementsToTargetFile(changes: textChanges.ChangeTracker, program:
changes.insertNodesBefore(targetFile, lastReExport, statements, /*blankLineBetween*/ true);
}
else {
changes.insertNodesAfter(targetFile, targetFile.statements[targetFile.statements.length - 1], statements);
(isImportDeclaration(targetFile.statements[targetFile.statements.length - 1])) ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(isImportDeclaration(targetFile.statements[targetFile.statements.length - 1])) ?
isImportDeclaration(targetFile.statements[targetFile.statements.length - 1]) ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the case that targetFile.statements can't be empty at this point?

changes.insertNodesAtEndOfFile(targetFile, statements, /*blankLineSuffix*/ false, /*blankLinePrefix*/ true) :
changes.insertNodesAfter(targetFile, targetFile.statements[targetFile.statements.length - 1], statements);
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,8 @@ export class ChangeTracker {
private insertAtTopOfFile(sourceFile: SourceFile, insert: Statement | readonly Statement[], blankLineBetween: boolean): void {
const pos = getInsertionPositionAtSourceFileTop(sourceFile);
const options = {
prefix: pos === 0 ? undefined : this.newLineCharacter,
// While inserting import statements in a blank existing file, add a newline as a prefix only if the file is blank, or the import is added as the first statement without using importAdder.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this comment can be moved to where we call this function in moveToFile? Or modified to avoid referring to imports and importAdder, since if someone is looking solely at textChanges, without the context of moveToFile, it's hard to understand what this all means.

prefix: pos === 0 || pos === sourceFile.end && blankLineBetween ? undefined : this.newLineCharacter,
suffix: (isLineBreak(sourceFile.text.charCodeAt(pos)) ? "" : this.newLineCharacter) + (blankLineBetween ? this.newLineCharacter : ""),
};
if (isArray(insert)) {
Expand All @@ -634,18 +635,20 @@ export class ChangeTracker {
public insertNodesAtEndOfFile(
sourceFile: SourceFile,
newNodes: readonly Statement[],
blankLineBetween: boolean): void {
this.insertAtEndOfFile(sourceFile, newNodes, blankLineBetween);
blankLineSuffix: boolean,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an optional suggestion: I think it makes more sense to swap the order of blankLinePrefix and blankLineSuffix so that blankLinePrefix comes first, to prevent confusing the order of those when we call the function.

blankLinePrefix: boolean): void {
this.insertAtEndOfFile(sourceFile, newNodes, blankLineSuffix, blankLinePrefix);
}

private insertAtEndOfFile(
sourceFile: SourceFile,
insert: readonly Statement[],
blankLineBetween: boolean): void {
blankLineSuffix: boolean,
blankLinePrefix: boolean): void {
const pos = sourceFile.end + 1;
const options = {
prefix: this.newLineCharacter,
suffix: this.newLineCharacter + (blankLineBetween ? this.newLineCharacter : ""),
prefix: blankLinePrefix ? this.newLineCharacter : undefined,
suffix: this.newLineCharacter + (blankLineSuffix ? this.newLineCharacter : ""),
};
this.insertNodesAt(sourceFile, pos, insert, options);
}
Expand Down
3 changes: 0 additions & 3 deletions tests/cases/fourslash/moveToFile_blankExistingFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ verify.moveToFile({
"/bar.ts":
`//
import { p } from './a';


import { b } from './other';


const y: Date = p + b;
`,
},
Expand Down
33 changes: 33 additions & 0 deletions tests/cases/fourslash/moveToFile_consistentNewLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

/// <reference path='fourslash.ts' />

// @Filename: /bar.ts
////export const tt = 2;

// @Filename: /a.ts
////import { b } from './other';
////
////const a = 1;
////[|const c = a + b + 2;|]

// @Filename: /other.ts
////export const b = 1;


verify.moveToFile({
newFileContents: {
"/a.ts":
`
export const a = 1;
`,

"/bar.ts":
`import { a } from './a';
import { b } from './other';

export const tt = 2;
const c = a + b + 2;
`,
},
interactiveRefactorArguments: { targetFile: "/bar.ts" },
});
35 changes: 35 additions & 0 deletions tests/cases/fourslash/moveToFile_consistentNewLines2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

/// <reference path='fourslash.ts' />

// @Filename: /bar.ts
////import { d } from './other2';

// @Filename: /a.ts
////import { b } from './other';
////
////const a = 1;
////[|const c = a + b + 6;|]

// @Filename: /other.ts
////export const b = 1;

// @Filename: /other2.ts
////export const d = 1;


verify.moveToFile({
newFileContents: {
"/a.ts":
`
export const a = 1;
`,

"/bar.ts":
`import { a } from './a';
import { b } from './other';
import { d } from './other2';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other new test includes a blank line between the imports and the statement, and this one doesn't. Is there a reason why?

const c = a + b + 6;
`,
},
interactiveRefactorArguments: { targetFile: "/bar.ts" },
});
5 changes: 0 additions & 5 deletions tests/cases/fourslash/moveToFile_consistentQuoteStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ verify.moveToFile({

"/bar.ts":
`import { a } from './a';

import { b } from './other';

export const tt = 2;
const c = a + b;
`,
},
interactiveRefactorArguments: { targetFile: "/bar.ts" },

preferences: {
quotePreference: "single",
}
});
2 changes: 0 additions & 2 deletions tests/cases/fourslash/moveToFile_differentDirectories2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ y;`,

"/src/dir2/bar.ts":
`import { a } from '../dir1/a';

import { b } from '../dir1/other';


export const y = b + a;
`,
},
Expand Down
1 change: 0 additions & 1 deletion tests/cases/fourslash/moveToFile_unresolvedImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ verify.moveToFile({

"/bar.ts":
`import { a } from "doesnt-exist";

const a = 1;
a();
`,
Expand Down