diff --git a/src/MagicString.ts b/src/MagicString.ts index d4a6a11..2a9b64e 100644 --- a/src/MagicString.ts +++ b/src/MagicString.ts @@ -484,6 +484,9 @@ export default class MagicString { end = end + this.offset index = index + this.offset + if (start === end) + return this + if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself') diff --git a/test/MagicString.test.ts b/test/MagicString.test.ts index 1ec577a..fdf0a78 100644 --- a/test/MagicString.test.ts +++ b/test/MagicString.test.ts @@ -876,6 +876,15 @@ describe('magicString', () => { assert.throws(() => s.move(3, 6, 6), /Cannot move a selection inside itself/) }) + it('does nothing when moving a zero-length range', () => { + const s = new MagicString('abcdefghijkl') + + assert.doesNotThrow(() => s.move(0, 0, 6)) + assert.doesNotThrow(() => s.move(5, 5, 0)) + + assert.equal(s.toString(), 'abcdefghijkl') + }) + it('allows edits of moved content', () => { const s1 = new MagicString('abcdefghijkl')