From d254b908730103b3ad9a24e59aa6a38e1a413fbb Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Sat, 2 Jan 2016 12:24:38 -0800 Subject: [PATCH] Do not overwrite inserts at the end of patched ranges. --- src/MagicString.js | 2 ++ test/index.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/MagicString.js b/src/MagicString.js index 8e0110a..9c0bbfd 100644 --- a/src/MagicString.js +++ b/src/MagicString.js @@ -194,6 +194,8 @@ MagicString.prototype = { if ( start !== end && start <= previous.start && end >= previous.end ) { // unless it's an insert at the start if ( previous.start === previous.end && previous.start === start ) break; + // or it's an insert at the end + if ( previous.start === previous.end && previous.end === end ) continue; this.patches.splice( i, 1 ); } diff --git a/test/index.js b/test/index.js index b3f09e3..f8835f2 100644 --- a/test/index.js +++ b/test/index.js @@ -393,6 +393,13 @@ describe( 'MagicString', function () { TypeError ); }); + + it( 'should allow inserting after removed range', function () { + var s = new MagicString( 'abcd' ); + s.remove( 1, 2 ); + s.insert( 2, 'z' ); + assert.equal( s.toString(), 'azcd' ); + }); }); describe( 'locate', function () { @@ -687,6 +694,15 @@ describe( 'MagicString', function () { var s = new MagicString( 'abcdefghijkl' ); assert.strictEqual( s.remove( 3, 4 ), s ); }); + + it( 'should not remove content inserted after the end of removed range', function () { + var s = new MagicString( 'ab.c;' ); + + s.insert( 0, '(' ); + s.insert( 4, ')' ); + s.remove( 2, 4 ); + assert.equal( s.toString(), '(ab);' ); + }); }); describe( 'slice', function () {