Skip to content

Commit

Permalink
Merge pull request #35 from eventualbuddha/fix-insert-at-end-of-remov…
Browse files Browse the repository at this point in the history
…ed-range

Do not overwrite inserts at the end of patched ranges.
  • Loading branch information
Rich-Harris committed Jan 2, 2016
2 parents 8c07c00 + d254b90 commit 4c0ad40
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/MagicString.js
Expand Up @@ -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 );
}

Expand Down
16 changes: 16 additions & 0 deletions test/index.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 4c0ad40

Please sign in to comment.