-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Don't use text change's createNewFile
for existing empty file
#54358
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
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 |
---|---|---|
|
@@ -176,8 +176,8 @@ registerRefactor(refactorNameForMoveToFile, { | |
function doChange(context: RefactorContext, oldFile: SourceFile, targetFile: string, program: Program, toMove: ToMove, changes: textChanges.ChangeTracker, host: LanguageServiceHost, preferences: UserPreferences): void { | ||
const checker = program.getTypeChecker(); | ||
const usage = getUsageInfo(oldFile, toMove.all, checker); | ||
//For a new file or an existing blank target file | ||
if (!host.fileExists(targetFile) || host.fileExists(targetFile) && program.getSourceFile(targetFile)?.statements.length === 0) { | ||
//For a new file | ||
if (!host.fileExists(targetFile)) { | ||
changes.createNewFile(oldFile, targetFile, getNewStatementsAndRemoveFromOldFile(oldFile, targetFile, usage, changes, toMove, program, host, preferences)); | ||
addNewFileToTsconfig(program, changes, oldFile.fileName, targetFile, hostGetCanonicalFileName(host)); | ||
} | ||
|
@@ -189,7 +189,15 @@ function doChange(context: RefactorContext, oldFile: SourceFile, targetFile: str | |
} | ||
|
||
function getNewStatementsAndRemoveFromOldFile( | ||
oldFile: SourceFile, targetFile: string | SourceFile, usage: UsageInfo, changes: textChanges.ChangeTracker, toMove: ToMove, program: Program, host: LanguageServiceHost, preferences: UserPreferences, importAdder?: codefix.ImportAdder | ||
oldFile: SourceFile, | ||
targetFile: string | SourceFile, | ||
usage: UsageInfo, | ||
changes: textChanges.ChangeTracker, | ||
toMove: ToMove, | ||
program: Program, | ||
host: LanguageServiceHost, | ||
preferences: UserPreferences, | ||
importAdder?: codefix.ImportAdder | ||
) { | ||
const checker = program.getTypeChecker(); | ||
const prologueDirectives = takeWhile(oldFile.statements, isPrologueDirective); | ||
|
@@ -218,6 +226,9 @@ function getNewStatementsAndRemoveFromOldFile( | |
if (targetFile.statements.length > 0) { | ||
changes.insertNodesAfter(targetFile, targetFile.statements[targetFile.statements.length - 1], body); | ||
} | ||
else { | ||
changes.insertNodesAtEndOfFile(targetFile, body, /*blankLineBetween*/ false); | ||
} | ||
if (imports.length > 0) { | ||
insertImports(changes, targetFile, imports, /*blankLineBetween*/ true, 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. Regarding the extra new lines, we could also change this code to pass import { a } from "./a";
const x = 2; instead of import { a } from "./a";
const x = 2; |
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -652,7 +652,7 @@ export interface LanguageService { | |
* arguments for any interactive action before offering it. | ||
*/ | ||
getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string, includeInteractiveActions?: boolean): ApplicableRefactorInfo[]; | ||
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined, includeInteractiveActions?: InteractiveRefactorArguments): RefactorEditInfo | undefined; | ||
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. Drive-by fix, I thought this parameter had a misleading name, since it's not a boolean. |
||
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string, preferences: UserPreferences | undefined, interactiveRefactorArguments?: InteractiveRefactorArguments): RefactorEditInfo | undefined; | ||
getMoveToRefactoringFileSuggestions(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences | undefined, triggerReason?: RefactorTriggerReason, kind?: string): { newFileName: string, files: string[] }; | ||
organizeImports(args: OrganizeImportsArgs, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[]; | ||
getEditsForFileRename(oldFilePath: string, newFilePath: string, formatOptions: FormatCodeSettings, preferences: UserPreferences | undefined): readonly FileTextChanges[]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/// <reference path='../fourslash.ts' /> | ||
|
||
// @Filename: /source.ts | ||
////[|export const a = 1;|] | ||
////const b = 2; | ||
////console.log(a, b); | ||
|
||
// @Filename: /target.ts | ||
/////** empty */ | ||
|
||
// @Filename: /tsconfig.json | ||
///// { "compilerOptions": { "newLine": "lf" } } | ||
|
||
verify.moveToFile({ | ||
newFileContents: { | ||
"/source.ts": | ||
`import { a } from "./target"; | ||
|
||
const b = 2; | ||
console.log(a, b);`, | ||
"/target.ts": | ||
`/** empty */ | ||
export const a = 1; | ||
`, | ||
}, | ||
interactiveRefactorArguments: { targetFile: "/target.ts" }, | ||
}); |
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.
Fourslash server tests previously didn't work with move to file