@@ -340,6 +340,33 @@ describe('magicString', () => {
340340 assert . strictEqual ( new MagicString ( '00' ) . remove ( 0 , 2 ) . replace ( / 0 / , '1' ) . toString ( ) , '' )
341341 } )
342342
343+ it ( 'replaces a match over a range that one overwrite spread across several chunks' , ( ) => {
344+ // `appendLeft(1, '')` inserts nothing but splits the range, so the
345+ // replacement is stored on the first chunk and the second is emptied,
346+ // which must not count as removed
347+ assert . strictEqual ( new MagicString ( 'aa' ) . update ( 0 , 2 , 'Q' ) . replace ( 'aa' , 'P' ) . toString ( ) , 'P' )
348+ const s = new MagicString ( 'aa' ) . appendLeft ( 1 , '' ) . update ( 0 , 2 , 'Q' )
349+ assert . strictEqual ( s . clone ( ) . replace ( 'aa' , 'P' ) . toString ( ) , 'P' )
350+ assert . strictEqual ( s . clone ( ) . replace ( / a + / , 'P' ) . toString ( ) , 'P' )
351+
352+ // the emptied chunk may itself be split afterwards
353+ const t = new MagicString ( 'aaa' ) . appendLeft ( 1 , '' ) . overwrite ( 0 , 3 , 'Q' ) . appendLeft ( 2 , '' )
354+ assert . strictEqual ( t . replace ( 'aaa' , 'P' ) . toString ( ) , 'P' )
355+ } )
356+
357+ it ( 'skips a match over the rest of an overwritten range when the replacement is left out or gone' , ( ) => {
358+ const create = ( ) => new MagicString ( 'abc' ) . appendLeft ( 1 , '' ) . update ( 0 , 3 , 'Q' )
359+
360+ // the match leaves out the first chunk, so the replacement would be doubled
361+ assert . strictEqual ( create ( ) . replace ( 'bc' , 'P' ) . toString ( ) , 'Q' )
362+ // editing the first chunk on its own drops the replacement for the rest
363+ assert . strictEqual ( create ( ) . update ( 0 , 1 , 'R' ) . replace ( 'abc' , 'P' ) . toString ( ) , 'R' )
364+ assert . strictEqual ( create ( ) . reset ( 0 , 1 ) . replace ( 'abc' , 'P' ) . toString ( ) , 'a' )
365+ // and removed content stays removed
366+ assert . strictEqual ( create ( ) . remove ( 1 , 3 ) . replace ( 'abc' , 'P' ) . toString ( ) , 'Q' )
367+ assert . strictEqual ( create ( ) . update ( 1 , 3 , '' ) . replace ( 'abc' , 'P' ) . toString ( ) , 'Q' )
368+ } )
369+
343370 it ( 'skips an empty match between two removed ranges' , ( ) => {
344371 // the lookahead matches at 2, which is removed whether [1, 3) went in one
345372 // call or two
0 commit comments