Skip to content

Commit 16af96b

Browse files
author
Andy
authored
moveToNewFile: Format the new file (microsoft#24379)
1 parent bc570cd commit 16af96b

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/services/textChanges.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ namespace ts.textChanges {
687687
this.finishTrailingCommaAfterDeletingNodesInList();
688688
const changes = changesToText.getTextChangesFromChanges(this.changes, this.newLineCharacter, this.formatContext, validate);
689689
for (const { oldFile, fileName, statements } of this.newFiles) {
690-
changes.push(changesToText.newFileChanges(oldFile, fileName, statements, this.newLineCharacter));
690+
changes.push(changesToText.newFileChanges(oldFile, fileName, statements, this.newLineCharacter, this.formatContext));
691691
}
692692
return changes;
693693
}
@@ -726,8 +726,12 @@ namespace ts.textChanges {
726726
});
727727
}
728728

729-
export function newFileChanges(oldFile: SourceFile, fileName: string, statements: ReadonlyArray<Statement>, newLineCharacter: string): FileTextChanges {
730-
const text = statements.map(s => getNonformattedText(s, oldFile, newLineCharacter).text).join(newLineCharacter);
729+
export function newFileChanges(oldFile: SourceFile, fileName: string, statements: ReadonlyArray<Statement>, newLineCharacter: string, formatContext: formatting.FormatContext): FileTextChanges {
730+
// TODO: this emits the file, parses it back, then formats it that -- may be a less roundabout way to do this
731+
const nonFormattedText = statements.map(s => getNonformattedText(s, oldFile, newLineCharacter).text).join(newLineCharacter);
732+
const sourceFile = createSourceFile(fileName, nonFormattedText, ScriptTarget.ESNext);
733+
const changes = formatting.formatDocument(sourceFile, formatContext);
734+
const text = applyChanges(nonFormattedText, changes);
731735
return { fileName, textChanges: [createTextChange(createTextSpan(0, 0), text)], isNewFile: true };
732736
}
733737

tests/cases/fourslash/fourslash.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ declare module ts {
6969
writeByteOrderMark: boolean;
7070
text: string;
7171
}
72-
}
7372

74-
declare namespace ts {
7573
function flatMap<T, U>(array: ReadonlyArray<T>, mapfn: (x: T, i: number) => U | ReadonlyArray<U> | undefined): U[];
7674
}
7775

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////[|function f() {
5+
//// const x = 0;
6+
////}|]
7+
8+
var copy = format.copyFormatOptions();
9+
copy.ConvertTabsToSpaces = false;
10+
format.setFormatOptions(copy);
11+
12+
verify.moveToNewFile({
13+
newFileContents: {
14+
"/a.ts":
15+
``,
16+
"/f.ts":
17+
`function f() {
18+
const x = 0;
19+
}`,
20+
},
21+
});

0 commit comments

Comments
 (0)