moveToNewFile: Format the new file (#24379) (#24382)

This commit is contained in:
Andy
2018-05-24 11:35:02 -07:00
committed by GitHub
parent 68bc957cf6
commit a7b7b3c2ad
3 changed files with 28 additions and 5 deletions
+7 -3
View File
@@ -687,7 +687,7 @@ namespace ts.textChanges {
this.finishTrailingCommaAfterDeletingNodesInList();
const changes = changesToText.getTextChangesFromChanges(this.changes, this.newLineCharacter, this.formatContext, validate);
for (const { oldFile, fileName, statements } of this.newFiles) {
changes.push(changesToText.newFileChanges(oldFile, fileName, statements, this.newLineCharacter));
changes.push(changesToText.newFileChanges(oldFile, fileName, statements, this.newLineCharacter, this.formatContext));
}
return changes;
}
@@ -726,8 +726,12 @@ namespace ts.textChanges {
});
}
export function newFileChanges(oldFile: SourceFile, fileName: string, statements: ReadonlyArray<Statement>, newLineCharacter: string): FileTextChanges {
const text = statements.map(s => getNonformattedText(s, oldFile, newLineCharacter).text).join(newLineCharacter);
export function newFileChanges(oldFile: SourceFile, fileName: string, statements: ReadonlyArray<Statement>, newLineCharacter: string, formatContext: formatting.FormatContext): FileTextChanges {
// TODO: this emits the file, parses it back, then formats it that -- may be a less roundabout way to do this
const nonFormattedText = statements.map(s => getNonformattedText(s, oldFile, newLineCharacter).text).join(newLineCharacter);
const sourceFile = createSourceFile(fileName, nonFormattedText, ScriptTarget.ESNext);
const changes = formatting.formatDocument(sourceFile, formatContext);
const text = applyChanges(nonFormattedText, changes);
return { fileName, textChanges: [createTextChange(createTextSpan(0, 0), text)], isNewFile: true };
}
-2
View File
@@ -69,9 +69,7 @@ declare module ts {
writeByteOrderMark: boolean;
text: string;
}
}
declare namespace ts {
function flatMap<T, U>(array: ReadonlyArray<T>, mapfn: (x: T, i: number) => U | ReadonlyArray<U> | undefined): U[];
}
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />
// @Filename: /a.ts
////[|function f() {
//// const x = 0;
////}|]
var copy = format.copyFormatOptions();
copy.ConvertTabsToSpaces = false;
format.setFormatOptions(copy);
verify.moveToNewFile({
newFileContents: {
"/a.ts":
``,
"/f.ts":
`function f() {
const x = 0;
}`,
},
});