Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not overwrite inserts at the end of patched ranges. #35

Merged
merged 1 commit into from Jan 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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