@@ -197,13 +197,12 @@ namespace ts.textChanges {
197
197
198
198
export class ChangeTracker {
199
199
private readonly changes : Change [ ] = [ ] ;
200
- private readonly newLineCharacter : string ;
201
200
private readonly deletedNodesInLists : true [ ] = [ ] ; // Stores ids of nodes in lists that we already deleted. Used to avoid deleting `, ` twice in `a, b`.
202
201
// Map from class id to nodes to insert at the start
203
202
private readonly nodesInsertedAtClassStarts = createMap < { sourceFile : SourceFile , cls : ClassLikeDeclaration , members : ClassElement [ ] } > ( ) ;
204
203
205
204
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 ) ;
207
206
}
208
207
209
208
public static with ( context : TextChangesContext , cb : ( tracker : ChangeTracker ) => void ) : FileTextChanges [ ] {
@@ -212,11 +211,11 @@ namespace ts.textChanges {
212
211
return tracker . getChanges ( ) ;
213
212
}
214
213
214
+ /** Public for tests only. Other callers should use `ChangeTracker.with`. */
215
215
constructor (
216
- private readonly newLine : NewLineKind ,
216
+ private readonly newLineCharacter : string ,
217
217
private readonly formatContext : ts . formatting . FormatContext ,
218
218
private readonly validator ?: ( text : NonFormattedText ) => void ) {
219
- this . newLineCharacter = getNewLineCharacter ( { newLine } ) ;
220
219
}
221
220
222
221
public deleteRange ( sourceFile : SourceFile , range : TextRange ) {
@@ -631,7 +630,7 @@ namespace ts.textChanges {
631
630
}
632
631
633
632
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 ) ;
635
634
if ( this . validator ) {
636
635
this . validator ( nonformattedText ) ;
637
636
}
@@ -671,10 +670,9 @@ namespace ts.textChanges {
671
670
readonly node : Node ;
672
671
}
673
672
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 ) ;
678
676
printer . writeNode ( EmitHint . Unspecified , node , sourceFile , writer ) ;
679
677
return { text : writer . getText ( ) , node : assignPositionsToNode ( node ) } ;
680
678
}
0 commit comments