@@ -190,6 +190,7 @@ namespace ts.textChanges {
190
190
public deleteNodeInList ( sourceFile : SourceFile , node : Node ) {
191
191
const containingList = formatting . SmartIndenter . getContainingList ( node , sourceFile ) ;
192
192
if ( ! containingList ) {
193
+ Debug . fail ( "node is not a list element" ) ;
193
194
return this ;
194
195
}
195
196
const index = containingList . indexOf ( node ) ;
@@ -260,6 +261,7 @@ namespace ts.textChanges {
260
261
public insertNodeInListAfter ( sourceFile : SourceFile , after : Node , newNode : Node ) {
261
262
const containingList = formatting . SmartIndenter . getContainingList ( after , sourceFile ) ;
262
263
if ( ! containingList ) {
264
+ Debug . fail ( "node is not a list element" ) ;
263
265
return this ;
264
266
}
265
267
const index = containingList . indexOf ( after ) ;
@@ -407,10 +409,8 @@ namespace ts.textChanges {
407
409
changesPerFile . forEachValue ( path => {
408
410
const changesInFile = changesPerFile . get ( path ) ;
409
411
const sourceFile = changesInFile [ 0 ] . sourceFile ;
410
- ChangeTracker . normalize ( changesInFile ) ;
411
-
412
412
const fileTextChanges : FileTextChanges = { fileName : sourceFile . fileName , textChanges : [ ] } ;
413
- for ( const c of changesInFile ) {
413
+ for ( const c of ChangeTracker . normalize ( changesInFile ) ) {
414
414
fileTextChanges . textChanges . push ( {
415
415
span : this . computeSpan ( c , sourceFile ) ,
416
416
newText : this . computeNewText ( c , sourceFile )
@@ -463,11 +463,15 @@ namespace ts.textChanges {
463
463
464
464
private static normalize ( changes : Change [ ] ) {
465
465
// order changes by start position
466
- changes . sort ( ( a , b ) => a . range . pos - b . range . pos ) ;
466
+ const normalized = changes
467
+ . map ( ( c , i ) => ( { c, i } ) )
468
+ . sort ( ( { c : a , i : i1 } , { c : b , i : i2 } ) => ( a . range . pos - b . range . pos ) || i1 - i2 )
469
+ . map ( ( { c } ) => c ) ;
467
470
// verify that end position of the change is less than start position of the next change
468
- for ( let i = 0 ; i < changes . length - 2 ; i ++ ) {
469
- Debug . assert ( changes [ i ] . range . end <= changes [ i + 1 ] . range . pos ) ;
471
+ for ( let i = 0 ; i < normalized . length - 2 ; i ++ ) {
472
+ Debug . assert ( normalized [ i ] . range . end <= normalized [ i + 1 ] . range . pos ) ;
470
473
}
474
+ return normalized ;
471
475
}
472
476
}
473
477
0 commit comments