@@ -190,6 +190,7 @@ namespace ts.textChanges {
190190 public deleteNodeInList ( sourceFile : SourceFile , node : Node ) {
191191 const containingList = formatting . SmartIndenter . getContainingList ( node , sourceFile ) ;
192192 if ( ! containingList ) {
193+ Debug . fail ( "node is not a list element" ) ;
193194 return this ;
194195 }
195196 const index = containingList . indexOf ( node ) ;
@@ -260,6 +261,7 @@ namespace ts.textChanges {
260261 public insertNodeInListAfter ( sourceFile : SourceFile , after : Node , newNode : Node ) {
261262 const containingList = formatting . SmartIndenter . getContainingList ( after , sourceFile ) ;
262263 if ( ! containingList ) {
264+ Debug . fail ( "node is not a list element" ) ;
263265 return this ;
264266 }
265267 const index = containingList . indexOf ( after ) ;
@@ -407,10 +409,8 @@ namespace ts.textChanges {
407409 changesPerFile . forEachValue ( path => {
408410 const changesInFile = changesPerFile . get ( path ) ;
409411 const sourceFile = changesInFile [ 0 ] . sourceFile ;
410- ChangeTracker . normalize ( changesInFile ) ;
411-
412412 const fileTextChanges : FileTextChanges = { fileName : sourceFile . fileName , textChanges : [ ] } ;
413- for ( const c of changesInFile ) {
413+ for ( const c of ChangeTracker . normalize ( changesInFile ) ) {
414414 fileTextChanges . textChanges . push ( {
415415 span : this . computeSpan ( c , sourceFile ) ,
416416 newText : this . computeNewText ( c , sourceFile )
@@ -463,11 +463,15 @@ namespace ts.textChanges {
463463
464464 private static normalize ( changes : Change [ ] ) {
465465 // 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 ) ;
467470 // 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 ) ;
470473 }
474+ return normalized ;
471475 }
472476 }
473477
0 commit comments