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

Change overwrite and remove to remove interior inserts #89

Merged
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/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