-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
base: main
Are you sure you want to change the base?
Conversation
@@ -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])) ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(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. |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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])) ? |
There was a problem hiding this comment.
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?
Fixes #54375