@@ -28,9 +28,11 @@ namespace ts.textChanges {
28
28
}
29
29
30
30
export interface ConfigurableStart {
31
+ /** True to use getStart() (NB, not getFullStart()) without adjustment. */
31
32
useNonAdjustedStartPosition ?: boolean ;
32
33
}
33
34
export interface ConfigurableEnd {
35
+ /** True to use getEnd() without adjustment. */
34
36
useNonAdjustedEndPosition ?: boolean ;
35
37
}
36
38
@@ -132,7 +134,7 @@ namespace ts.textChanges {
132
134
133
135
export function getAdjustedStartPosition ( sourceFile : SourceFile , node : Node , options : ConfigurableStart , position : Position ) {
134
136
if ( options . useNonAdjustedStartPosition ) {
135
- return node . getFullStart ( ) ;
137
+ return node . getStart ( ) ;
136
138
}
137
139
const fullStart = node . getFullStart ( ) ;
138
140
const start = node . getStart ( sourceFile ) ;
@@ -280,51 +282,46 @@ namespace ts.textChanges {
280
282
return this ;
281
283
}
282
284
283
- public replaceRange ( sourceFile : SourceFile , range : TextRange , newNode : Node , options : InsertNodeOptions = { } ) {
285
+ public replaceRange ( sourceFile : SourceFile , range : TextRange , newNode : Node , options : ChangeNodeOptions = { } ) {
284
286
this . changes . push ( { kind : ChangeKind . ReplaceWithSingleNode , sourceFile, range, options, node : newNode } ) ;
285
287
return this ;
286
288
}
287
289
288
290
public replaceNode ( sourceFile : SourceFile , oldNode : Node , newNode : Node , options : ChangeNodeOptions = { } ) {
289
- const startPosition = getAdjustedStartPosition ( sourceFile , oldNode , options , Position . Start ) ;
290
- const endPosition = getAdjustedEndPosition ( sourceFile , oldNode , options ) ;
291
- return this . replaceWithSingle ( sourceFile , startPosition , endPosition , newNode , options ) ;
291
+ const pos = getAdjustedStartPosition ( sourceFile , oldNode , options , Position . Start ) ;
292
+ const end = getAdjustedEndPosition ( sourceFile , oldNode , options ) ;
293
+ return this . replaceRange ( sourceFile , { pos , end } , newNode , options ) ;
292
294
}
293
295
294
296
public replaceNodeRange ( sourceFile : SourceFile , startNode : Node , endNode : Node , newNode : Node , options : ChangeNodeOptions = { } ) {
295
- const startPosition = getAdjustedStartPosition ( sourceFile , startNode , options , Position . Start ) ;
296
- const endPosition = getAdjustedEndPosition ( sourceFile , endNode , options ) ;
297
- return this . replaceWithSingle ( sourceFile , startPosition , endPosition , newNode , options ) ;
297
+ const pos = getAdjustedStartPosition ( sourceFile , startNode , options , Position . Start ) ;
298
+ const end = getAdjustedEndPosition ( sourceFile , endNode , options ) ;
299
+ return this . replaceRange ( sourceFile , { pos , end } , newNode , options ) ;
298
300
}
299
301
300
- private replaceWithSingle ( sourceFile : SourceFile , startPosition : number , endPosition : number , newNode : Node , options : ChangeNodeOptions ) : this {
301
- this . changes . push ( {
302
- kind : ChangeKind . ReplaceWithSingleNode ,
303
- sourceFile,
304
- options,
305
- node : newNode ,
306
- range : { pos : startPosition , end : endPosition }
307
- } ) ;
308
- return this ;
302
+ private getDefaultChangeMultipleNodesOptions ( ) : ChangeMultipleNodesOptions {
303
+ return {
304
+ nodeSeparator : this . newLineCharacter ,
305
+ useNonAdjustedStartPosition : true ,
306
+ useNonAdjustedEndPosition : true ,
307
+ } ;
309
308
}
310
309
311
- private replaceWithMultiple ( sourceFile : SourceFile , startPosition : number , endPosition : number , newNodes : ReadonlyArray < Node > , options : ChangeMultipleNodesOptions ) : this {
312
- this . changes . push ( {
313
- kind : ChangeKind . ReplaceWithMultipleNodes ,
314
- sourceFile,
315
- options,
316
- nodes : newNodes ,
317
- range : { pos : startPosition , end : endPosition }
318
- } ) ;
310
+ public replaceRangeWithNodes ( sourceFile : SourceFile , range : TextRange , newNodes : ReadonlyArray < Node > , options : ChangeMultipleNodesOptions = this . getDefaultChangeMultipleNodesOptions ( ) ) {
311
+ this . changes . push ( { kind : ChangeKind . ReplaceWithMultipleNodes , sourceFile, range, options, nodes : newNodes } ) ;
319
312
return this ;
320
313
}
321
314
322
- public replaceNodeWithNodes ( sourceFile : SourceFile , oldNode : Node , newNodes : ReadonlyArray < Node > ) : void {
323
- this . replaceWithMultiple ( sourceFile , oldNode . getStart ( sourceFile ) , oldNode . getEnd ( ) , newNodes , { nodeSeparator : this . newLineCharacter } ) ;
315
+ public replaceNodeWithNodes ( sourceFile : SourceFile , oldNode : Node , newNodes : ReadonlyArray < Node > , options : ChangeMultipleNodesOptions = this . getDefaultChangeMultipleNodesOptions ( ) ) {
316
+ const pos = getAdjustedStartPosition ( sourceFile , oldNode , options , Position . Start ) ;
317
+ const end = getAdjustedEndPosition ( sourceFile , oldNode , options ) ;
318
+ return this . replaceRangeWithNodes ( sourceFile , { pos, end } , newNodes , options ) ;
324
319
}
325
320
326
- public replaceNodesWithNodes ( sourceFile : SourceFile , oldNodes : ReadonlyArray < Node > , newNodes : ReadonlyArray < Node > ) : void {
327
- this . replaceWithMultiple ( sourceFile , first ( oldNodes ) . getStart ( sourceFile ) , last ( oldNodes ) . getEnd ( ) , newNodes , { nodeSeparator : this . newLineCharacter } ) ;
321
+ public replaceNodeRangeWithNodes ( sourceFile : SourceFile , startNode : Node , endNode : Node , newNodes : ReadonlyArray < Node > , options : ChangeMultipleNodesOptions = this . getDefaultChangeMultipleNodesOptions ( ) ) {
322
+ const pos = getAdjustedStartPosition ( sourceFile , startNode , options , Position . Start ) ;
323
+ const end = getAdjustedEndPosition ( sourceFile , endNode , options ) ;
324
+ return this . replaceRangeWithNodes ( sourceFile , { pos, end } , newNodes , options ) ;
328
325
}
329
326
330
327
private insertNodeAt ( sourceFile : SourceFile , pos : number , newNode : Node , options : InsertNodeOptions = { } ) {
@@ -341,18 +338,18 @@ namespace ts.textChanges {
341
338
}
342
339
343
340
public insertNodeBefore ( sourceFile : SourceFile , before : Node , newNode : Node , blankLineBetween = false ) {
344
- const startPosition = getAdjustedStartPosition ( sourceFile , before , { } , Position . Start ) ;
345
- return this . replaceWithSingle ( sourceFile , startPosition , startPosition , newNode , this . getOptionsForInsertNodeBefore ( before , blankLineBetween ) ) ;
341
+ const pos = getAdjustedStartPosition ( sourceFile , before , { } , Position . Start ) ;
342
+ return this . replaceRange ( sourceFile , { pos , end : pos } , newNode , this . getOptionsForInsertNodeBefore ( before , blankLineBetween ) ) ;
346
343
}
347
344
348
345
public insertModifierBefore ( sourceFile : SourceFile , modifier : SyntaxKind , before : Node ) : void {
349
346
const pos = before . getStart ( sourceFile ) ;
350
- this . replaceWithSingle ( sourceFile , pos , pos , createToken ( modifier ) , { suffix : " " } ) ;
347
+ this . replaceRange ( sourceFile , { pos, end : pos } , createToken ( modifier ) , { suffix : " " } ) ;
351
348
}
352
349
353
350
public changeIdentifierToPropertyAccess ( sourceFile : SourceFile , prefix : string , node : Identifier ) : void {
354
- const startPosition = getAdjustedStartPosition ( sourceFile , node , { } , Position . Start ) ;
355
- this . replaceWithSingle ( sourceFile , startPosition , startPosition , createPropertyAccess ( createIdentifier ( prefix ) , "" ) , { } ) ;
351
+ const pos = getAdjustedStartPosition ( sourceFile , node , { } , Position . Start ) ;
352
+ this . replaceRange ( sourceFile , { pos , end : pos } , createPropertyAccess ( createIdentifier ( prefix ) , "" ) , { } ) ;
356
353
}
357
354
358
355
private getOptionsForInsertNodeBefore ( before : Node , doubleNewlines : boolean ) : ChangeNodeOptions {
@@ -390,8 +387,8 @@ namespace ts.textChanges {
390
387
}
391
388
392
389
public insertNodeAtEndOfScope ( sourceFile : SourceFile , scope : Node , newNode : Node ) : void {
393
- const startPosition = getAdjustedStartPosition ( sourceFile , scope . getLastToken ( ) , { } , Position . Start ) ;
394
- this . replaceWithSingle ( sourceFile , startPosition , startPosition , newNode , {
390
+ const pos = getAdjustedStartPosition ( sourceFile , scope . getLastToken ( ) , { } , Position . Start ) ;
391
+ this . replaceRange ( sourceFile , { pos , end : pos } , newNode , {
395
392
prefix : isLineBreak ( sourceFile . text . charCodeAt ( scope . getLastToken ( ) . pos ) ) ? this . newLineCharacter : this . newLineCharacter + this . newLineCharacter ,
396
393
suffix : this . newLineCharacter
397
394
} ) ;
@@ -433,7 +430,7 @@ namespace ts.textChanges {
433
430
}
434
431
}
435
432
const endPosition = getAdjustedEndPosition ( sourceFile , after , { } ) ;
436
- return this . replaceWithSingle ( sourceFile , endPosition , endPosition , newNode , this . getInsertNodeAfterOptions ( after ) ) ;
433
+ return this . replaceRange ( sourceFile , { pos : endPosition , end : endPosition } , newNode , this . getInsertNodeAfterOptions ( after ) ) ;
437
434
}
438
435
439
436
private getInsertNodeAfterOptions ( node : Node ) : InsertNodeOptions {
0 commit comments