Skip to content

Commit

Permalink
Handle inserts in the middle distinctly from replacements.
Browse files Browse the repository at this point in the history
  • Loading branch information
eventualbuddha committed Jan 14, 2015
1 parent 8feab6b commit e200bbe
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/MagicString/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,14 @@ MagicString.prototype = {
} else if ( index === this.original.length ) {
this.append( content );
} else {
this.replace( index, index, content );
var mapped = this.locate(index);

if ( mapped === null ) {
throw new Error( 'Cannot insert at replaced character index: ' + index );
}

this.str = this.str.substr( 0, mapped ) + content + this.str.substr( mapped );
adjust( this.mappings, index, this.mappings.length, content.length );
}

return this;
Expand Down Expand Up @@ -220,6 +227,13 @@ MagicString.prototype = {
throw new Error( 'Cannot replace the same content twice' );
}

if ( firstChar > lastChar + 1 ) {
throw new Error(
'BUG! First character mapped to a position after the last character: ' +
'[' + start + ', ' + end + '] -> [' + firstChar + ', ' + ( lastChar + 1 ) + ']'
);
}

this.str = this.str.substr( 0, firstChar ) + content + this.str.substring( lastChar + 1 );

d = content.length - ( lastChar + 1 - firstChar );
Expand Down

0 comments on commit e200bbe

Please sign in to comment.