Skip to content

Commit 56fe8cb

Browse files
fix: avoid crash when moving a zero-length range (#307)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 992baba commit 56fe8cb

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/MagicString.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ export default class MagicString {
484484
end = end + this.offset
485485
index = index + this.offset
486486

487+
if (start === end)
488+
return this
489+
487490
if (index >= start && index <= end)
488491
throw new Error('Cannot move a selection inside itself')
489492

test/MagicString.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,15 @@ describe('magicString', () => {
876876
assert.throws(() => s.move(3, 6, 6), /Cannot move a selection inside itself/)
877877
})
878878

879+
it('does nothing when moving a zero-length range', () => {
880+
const s = new MagicString('abcdefghijkl')
881+
882+
assert.doesNotThrow(() => s.move(0, 0, 6))
883+
assert.doesNotThrow(() => s.move(5, 5, 0))
884+
885+
assert.equal(s.toString(), 'abcdefghijkl')
886+
})
887+
879888
it('allows edits of moved content', () => {
880889
const s1 = new MagicString('abcdefghijkl')
881890

0 commit comments

Comments
 (0)