Skip to content
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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

navya9singh
Copy link
Member

Fixes #54375

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jul 18, 2023
@@ -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]) ?

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

"/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?

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

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

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Status: Waiting on author
Development

Successfully merging this pull request may close these issues.

Move to file adds too many new lines
3 participants