-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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); | ||||||
} | ||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Same here, could you add a comment explaining this? |
||||||
} | ||||||
} | ||||||
if (importAdder) { | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it the case that |
||||||
changes.insertNodesAtEndOfFile(targetFile, statements, /*blankLineSuffix*/ false, /*blankLinePrefix*/ true) : | ||||||
changes.insertNodesAfter(targetFile, targetFile.statements[targetFile.statements.length - 1], statements); | ||||||
} | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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 |
||
prefix: pos === 0 || pos === sourceFile.end && blankLineBetween ? undefined : this.newLineCharacter, | ||
suffix: (isLineBreak(sourceFile.text.charCodeAt(pos)) ? "" : this.newLineCharacter) + (blankLineBetween ? this.newLineCharacter : ""), | ||
}; | ||
if (isArray(insert)) { | ||
|
@@ -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 commentThe 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: 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); | ||
} | ||
|
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" }, | ||
}); |
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" }, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ verify.moveToFile({ | |
|
||
"/bar.ts": | ||
`import { a } from "doesnt-exist"; | ||
|
||
const a = 1; | ||
a(); | ||
`, | ||
|
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?