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

fix: allowed overwrite across moved content preceded by split #192

Merged
merged 3 commits into from Mar 2, 2022
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
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