File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed
Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -660,12 +660,18 @@ export default class MagicString {
660660 if ( DEBUG ) this . stats . time ( '_split' ) ;
661661
662662 let chunk = this . lastSearchedChunk ;
663+ let previousChunk = chunk ;
663664 const searchForward = index > chunk . end ;
664665
665666 while ( chunk ) {
666667 if ( chunk . contains ( index ) ) return this . _splitChunk ( chunk , index ) ;
667668
668669 chunk = searchForward ? this . byStart [ chunk . end ] : this . byEnd [ chunk . start ] ;
670+
671+ // Prevent infinite loop (e.g. via empty chunks, where start === end)
672+ if ( chunk === previousChunk ) return ;
673+
674+ previousChunk = chunk ;
669675 }
670676 }
671677
Original file line number Diff line number Diff line change @@ -98,6 +98,26 @@ describe('MagicString', () => {
9898
9999 assert . equal ( s . toString ( ) , 'x4213' ) ;
100100 } ) ;
101+
102+ it ( 'should append/prepend at end of string when index is out of upper bound' , ( ) => {
103+ const s = new MagicString ( 'x' ) ;
104+ s . prependLeft ( 6 , 'A' ) ;
105+ s . appendLeft ( 6 , 'B' ) ;
106+ s . prependRight ( 6 , 'C' ) ;
107+ s . appendRight ( 6 , 'D' ) ;
108+
109+ assert . equal ( s . toString ( ) , 'ABxCD' ) ;
110+ } ) ;
111+
112+ it ( 'should append/prepend on empty string when index is out of upper bound' , ( ) => {
113+ const s = new MagicString ( '' ) ;
114+ s . prependLeft ( 6 , 'A' ) ;
115+ s . appendLeft ( 6 , 'B' ) ;
116+ s . prependRight ( 6 , 'C' ) ;
117+ s . appendRight ( 6 , 'D' ) ;
118+
119+ assert . equal ( s . toString ( ) , 'ABCD' ) ;
120+ } ) ;
101121 } ) ;
102122
103123 describe ( 'appendLeft' , ( ) => {
You can’t perform that action at this time.
0 commit comments