Skip to content

Commit f8f4bb8

Browse files
author
Andy
authored
textChanges: Clean up handling of newLineCharacter (#21970)
1 parent 347bff1 commit f8f4bb8

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/harness/unittests/textChanges.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace ts {
5757
Harness.Baseline.runBaseline(`textChanges/${caption}.js`, () => {
5858
const sourceFile = createSourceFile("source.ts", text, ScriptTarget.ES2015, /*setParentNodes*/ true);
5959
const rulesProvider = getRuleProvider(placeOpenBraceOnNewLineForFunctions);
60-
const changeTracker = new textChanges.ChangeTracker(printerOptions.newLine, rulesProvider, validateNodes ? verifyPositions : undefined);
60+
const changeTracker = new textChanges.ChangeTracker(newLineCharacter, rulesProvider, validateNodes ? verifyPositions : undefined);
6161
testBlock(sourceFile, changeTracker);
6262
const changes = changeTracker.getChanges();
6363
assert.equal(changes.length, 1);

src/services/textChanges.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,12 @@ namespace ts.textChanges {
197197

198198
export class ChangeTracker {
199199
private readonly changes: Change[] = [];
200-
private readonly newLineCharacter: string;
201200
private readonly deletedNodesInLists: true[] = []; // Stores ids of nodes in lists that we already deleted. Used to avoid deleting `, ` twice in `a, b`.
202201
// Map from class id to nodes to insert at the start
203202
private readonly nodesInsertedAtClassStarts = createMap<{ sourceFile: SourceFile, cls: ClassLikeDeclaration, members: ClassElement[] }>();
204203

205204
public static fromContext(context: TextChangesContext): ChangeTracker {
206-
return new ChangeTracker(getNewLineOrDefaultFromHost(context.host, context.formatContext.options) === "\n" ? NewLineKind.LineFeed : NewLineKind.CarriageReturnLineFeed, context.formatContext);
205+
return new ChangeTracker(getNewLineOrDefaultFromHost(context.host, context.formatContext.options), context.formatContext);
207206
}
208207

209208
public static with(context: TextChangesContext, cb: (tracker: ChangeTracker) => void): FileTextChanges[] {
@@ -212,11 +211,11 @@ namespace ts.textChanges {
212211
return tracker.getChanges();
213212
}
214213

214+
/** Public for tests only. Other callers should use `ChangeTracker.with`. */
215215
constructor(
216-
private readonly newLine: NewLineKind,
216+
private readonly newLineCharacter: string,
217217
private readonly formatContext: ts.formatting.FormatContext,
218218
private readonly validator?: (text: NonFormattedText) => void) {
219-
this.newLineCharacter = getNewLineCharacter({ newLine });
220219
}
221220

222221
public deleteRange(sourceFile: SourceFile, range: TextRange) {
@@ -631,7 +630,7 @@ namespace ts.textChanges {
631630
}
632631

633632
private getFormattedTextOfNode(node: Node, sourceFile: SourceFile, pos: number, options: ChangeNodeOptions): string {
634-
const nonformattedText = getNonformattedText(node, sourceFile, this.newLine);
633+
const nonformattedText = getNonformattedText(node, sourceFile, this.newLineCharacter);
635634
if (this.validator) {
636635
this.validator(nonformattedText);
637636
}
@@ -671,10 +670,9 @@ namespace ts.textChanges {
671670
readonly node: Node;
672671
}
673672

674-
function getNonformattedText(node: Node, sourceFile: SourceFile | undefined, newLine: NewLineKind): NonFormattedText {
675-
const options = { newLine, target: sourceFile && sourceFile.languageVersion };
676-
const writer = new Writer(getNewLineCharacter(options));
677-
const printer = createPrinter(options, writer);
673+
function getNonformattedText(node: Node, sourceFile: SourceFile | undefined, newLine: string): NonFormattedText {
674+
const writer = new Writer(newLine);
675+
const printer = createPrinter({ newLine: newLine === "\n" ? NewLineKind.LineFeed : NewLineKind.CarriageReturnLineFeed }, writer);
678676
printer.writeNode(EmitHint.Unspecified, node, sourceFile, writer);
679677
return { text: writer.getText(), node: assignPositionsToNode(node) };
680678
}

0 commit comments

Comments
 (0)