Skip to content

Commit

Permalink
fix: allowed overwrite across moved content preceded by split (#192)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
mvolfik and antfu committed Mar 2, 2022
1 parent 2815e77 commit 403fa86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
19 changes: 7 additions & 12 deletions src/MagicString.js
Expand Up @@ -372,21 +372,16 @@ export default class MagicString {
const last = this.byEnd[end];

if (first) {
if (end > first.end && first.next !== this.byStart[first.end]) {
throw new Error('Cannot overwrite across a split point');
}

first.edit(content, storeName, contentOnly);

if (first !== last) {
let chunk = first.next;
while (chunk !== last) {
chunk.edit('', false);
chunk = chunk.next;
let chunk = first;
while (chunk !== last) {
if (chunk.next !== this.byStart[chunk.end]) {
throw new Error('Cannot overwrite across a split point');
}

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

first.edit(content, storeName, contentOnly);
} else {
// must be inserting at the end
const newChunk = new Chunk(start, end, '').edit(content, storeName);
Expand Down
17 changes: 16 additions & 1 deletion test/MagicString.js
Expand Up @@ -824,13 +824,28 @@ describe('MagicString', () => {
assert.equal(s.toString(), 'a&^...!?defghijkl');
});

it('disallows overwriting across moved content', () => {
it('disallows overwriting partially overlapping moved content', () => {
const s = new MagicString('abcdefghijkl');

s.move(6, 9, 3);
assert.throws(() => s.overwrite(5, 7, 'XX'), /Cannot overwrite across a split point/);
});

it('disallows overwriting fully surrounding content moved away', () => {
const s = new MagicString('abcdefghijkl');

s.move(6, 9, 3);
assert.throws(() => s.overwrite(4, 11, 'XX'), /Cannot overwrite across a split point/);
});

it('disallows overwriting fully surrounding content moved away even if there is another split', () => {
const s = new MagicString('abcdefghijkl');

s.move(6, 9, 3);
s.appendLeft(5, 'foo');
assert.throws(() => s.overwrite(4, 11, 'XX'), /Cannot overwrite across a split point/);
});

it('allows later insertions at the end', () => {
const s = new MagicString('abcdefg');

Expand Down

0 comments on commit 403fa86

Please sign in to comment.