Rich-Harris / magic-string

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

Infinite loop when removing in empty string. #280

Closed mysteryven closed 3 months ago

mysteryven commented 3 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 3 months ago

Would you like to send a quick PR? Thanks!