Rich-Harris / magic-string

Manipulate strings like a wizard
MIT License
2.34k stars 113 forks source link

Infinite loop when removing in empty string. #280

Closed mysteryven closed 2 months ago

mysteryven commented 2 months ago

Description

When using a negative index to remove, it will cause infinite loop.

const magicString = require('magic-string');
let s = ""
let ms = new magicString(s);
ms.remove(-2, -1);
console.log(map);

https://stackblitz.com/edit/stackblitz-starters-qxzkza?file=index.js

To reproduce it, run node index.js in the terminal.

Expect behaviour

it should throw error: Character is out of bounds

antfu commented 2 months ago

Would you like to send a quick PR? Thanks!