@@ -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