@@ -407,10 +407,8 @@ namespace ts.textChanges {
407407 changesPerFile . forEachValue ( path => {
408408 const changesInFile = changesPerFile . get ( path ) ;
409409 const sourceFile = changesInFile [ 0 ] . sourceFile ;
410- ChangeTracker . normalize ( changesInFile ) ;
411-
412410 const fileTextChanges : FileTextChanges = { fileName : sourceFile . fileName , textChanges : [ ] } ;
413- for ( const c of changesInFile ) {
411+ for ( const c of ChangeTracker . normalize ( changesInFile ) ) {
414412 fileTextChanges . textChanges . push ( {
415413 span : this . computeSpan ( c , sourceFile ) ,
416414 newText : this . computeNewText ( c , sourceFile )
@@ -463,11 +461,15 @@ namespace ts.textChanges {
463461
464462 private static normalize ( changes : Change [ ] ) {
465463 // order changes by start position
466- changes . sort ( ( a , b ) => a . range . pos - b . range . pos ) ;
464+ const normalized = changes
465+ . map ( ( c , i ) => ( { c, i } ) )
466+ . sort ( ( { c : a , i : i1 } , { c : b , i : i2 } ) => ( a . range . pos - b . range . pos ) || i1 - i2 )
467+ . map ( ( { c } ) => c ) ;
467468 // 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 ) ;
469+ for ( let i = 0 ; i < normalized . length - 2 ; i ++ ) {
470+ Debug . assert ( normalized [ i ] . range . end <= normalized [ i + 1 ] . range . pos ) ;
470471 }
472+ return normalized ;
471473 }
472474 }
473475
0 commit comments