Skip to content

Commit

Permalink
Merge pull request #89 from alangpierce/fix-overwrite-remove-ranges
Browse files Browse the repository at this point in the history
Change overwrite and remove to remove interior inserts
  • Loading branch information
Rich-Harris committed Aug 12, 2016
2 parents 179010d + 3f6dd4d commit 404dcc8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Chunk.js
Expand Up @@ -56,6 +56,8 @@ Chunk.prototype = {

edit ( content, storeName ) {
this.content = content;
this.intro = '';
this.outro = '';
this.storeName = storeName;

this.edited = true;
Expand Down
4 changes: 0 additions & 4 deletions src/MagicString.js
Expand Up @@ -308,17 +308,13 @@ MagicString.prototype = {
first.edit( content, storeName );

if ( first !== last ) {
first.outro = '';

let chunk = first.next;
while ( chunk !== last ) {
chunk.edit( '', false );
chunk.intro = chunk.outro = '';
chunk = chunk.next;
}

chunk.edit( '', false );
chunk.intro = '';
}
}

Expand Down
24 changes: 23 additions & 1 deletion test/index.js
Expand Up @@ -698,7 +698,7 @@ describe( 'MagicString', function () {
var s = new MagicString( 'abcdefghijkl' );

s.remove( 0, 6 );
s.insertRight( 6, 'DEF' );
s.insertLeft( 6, 'DEF' );
s.overwrite( 6, 9, 'GHI' );
assert.equal( s.toString(), 'DEFGHIjkl' );
});
Expand Down Expand Up @@ -742,6 +742,17 @@ describe( 'MagicString', function () {
TypeError
);
});

it ( 'replaces interior inserts', function() {
var s = new MagicString( 'abcdefghijkl' );

s.insertLeft( 1, '&' );
s.insertRight( 1, '^' );
s.insertLeft( 3, '!' );
s.insertRight( 3, '?' );
s.overwrite( 1, 3, '...' );
assert.equal( s.toString(), 'a&...?defghijkl' );
})
});

describe( 'prepend', function () {
Expand Down Expand Up @@ -831,6 +842,17 @@ describe( 'MagicString', function () {
assert.equal( s.toString(), '(ab);' );
});

it( 'should remove interior inserts', function () {
var s = new MagicString( 'abc;' );

s.insertLeft( 1, '[' );
s.insertRight( 1, '(' );
s.insertLeft( 2, ')' );
s.insertRight( 2, ']' );
s.remove( 1, 2 );
assert.equal( s.toString(), 'a[]c;' );
});

it( 'should provide a useful error when illegal removals are attempted', function () {
var s = new MagicString( 'abcdefghijkl' );

Expand Down

0 comments on commit 404dcc8

Please sign in to comment.