Skip to content

fix: respect the offset in snip - #363

Merged
antfu merged 1 commit into
Rich-Harris:masterfrom
youdie006:fix/snip-offset
Oct 26, 2026
Merged

antfu merged 1 commit into
Rich-Harris:masterfrom
youdie006:fix/snip-offset

Conversation

@youdie006

@youdie006 youdie006 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #362, where I noted snip was not included. snip removes (0, start) and (end, original.length), and remove adds this.offset to both, so with an offset the end runs past the string and the text before the offset is kept:

const s = new MagicString('hello world', { offset: 6 })
s.slice(0, 5)  // 'world'
s.snip(0, 5)   // throws: [MagicString] range 11–17 is out of bounds

It now removes (-offset, start) and (end, original.length - offset), which slice already uses for the end. The new test in tests/MagicString/snip.test.ts fails on master; reverting either call fails it. pnpm run lint, pnpm run build and vitest run (455 tests) pass; pnpm run typecheck reports the same 23 errors as master.

Written with AI assistance (Claude); I have reviewed the change.

snip removed (0, start) and (end, original.length), and remove adds the
offset to both, so with an offset the end ran past the string and the
text before the offset was kept. Remove relative to the offset, as slice
already measures the end.
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