Skip to content

fix: avoid crash when moving a zero-length range#307

Merged
antfu merged 2 commits into
Rich-Harris:masterfrom
sarathfrancis90:fix-move-zero-length-range
Jul 16, 2026
Merged

fix: avoid crash when moving a zero-length range#307
antfu merged 2 commits into
Rich-Harris:masterfrom
sarathfrancis90:fix-move-zero-length-range

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

move() crashes with an opaque TypeError: Cannot read properties of undefined (reading 'next') when called with a zero-length range (start === end):

new MagicString('abcdefghijkl').move(0, 0, 6);

The reason is that for an empty range byEnd[end] has no chunk, so last is undefined and the subsequent last.next blows up. remove() already short-circuits a zero-length range as a no-op (if (start === end) return this;), so I did the same in move() — moving nothing has no effect.

I found this while fuzzing random valid operation sequences. Added a test alongside the other move edge cases; full suite passes.

move(start, end, index) threw a TypeError ('Cannot read properties of
undefined') when start === end, because byEnd[end] is undefined for an
empty range so reading last.next failed. remove() already treats a
zero-length range as a no-op; do the same in move().
@antfu antfu changed the title Fix crash when moving a zero-length range fix: crash when moving a zero-length range Jul 16, 2026
…h-range

# Conflicts:
#	src/MagicString.js
#	test/MagicString.test.js
@antfu antfu changed the title fix: crash when moving a zero-length range fix: avoid crash when moving a zero-length range Jul 16, 2026
@antfu
antfu merged commit 56fe8cb into Rich-Harris:master Jul 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants