Skip to content

Commit

Permalink
fix: not use negative indices for remove in empty string (#281)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
mysteryven and antfu authored Jul 29, 2024
1 parent d6b2171 commit 5c1cba0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
10 changes: 6 additions & 4 deletions benchmark/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,12 @@ class MagicString {
return this.intro + lineStr;
}
slice(start = 0, end = this.original.length) {
while (start < 0)
start += this.original.length;
while (end < 0)
end += this.original.length;
if (this.original.length !== 0) {
while (start < 0)
start += this.original.length;
while (end < 0)
end += this.original.length;
}
let result = "";
let chunk = this.firstChunk;
while (chunk && (chunk.start > start || chunk.end <= start)) {
Expand Down
24 changes: 16 additions & 8 deletions src/MagicString.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,10 @@ export default class MagicString {
update(start, end, content, options) {
if (typeof content !== 'string') throw new TypeError('replacement content must be a string');

while (start < 0) start += this.original.length;
while (end < 0) end += this.original.length;
if (this.original.length !== 0) {
while (start < 0) start += this.original.length;
while (end < 0) end += this.original.length;
}

if (end > this.original.length) throw new Error('end is out of bounds');
if (start === end)
Expand Down Expand Up @@ -469,8 +471,10 @@ export default class MagicString {
}

remove(start, end) {
while (start < 0) start += this.original.length;
while (end < 0) end += this.original.length;
if (this.original.length !== 0) {
while (start < 0) start += this.original.length;
while (end < 0) end += this.original.length;
}

if (start === end) return this;

Expand All @@ -497,8 +501,10 @@ export default class MagicString {
}

reset(start, end) {
while (start < 0) start += this.original.length;
while (end < 0) end += this.original.length;
if (this.original.length !== 0) {
while (start < 0) start += this.original.length;
while (end < 0) end += this.original.length;
}

if (start === end) return this;

Expand Down Expand Up @@ -564,8 +570,10 @@ export default class MagicString {
}

slice(start = 0, end = this.original.length) {
while (start < 0) start += this.original.length;
while (end < 0) end += this.original.length;
if (this.original.length !== 0) {
while (start < 0) start += this.original.length;
while (end < 0) end += this.original.length;
}

let result = '';

Expand Down
13 changes: 13 additions & 0 deletions test/MagicString.js
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,19 @@ describe('MagicString', () => {

assert.equal(s.toString(), 'abchidejkl');
});

it('should accept negative indices', () => {
const s = new MagicString('abcde');
// "abcde"
// ^
s.remove(-2, -1);
assert.equal(s.toString(), 'abce');
});

it('should throw error when using negative indices with empty string', () => {
const s = new MagicString('');
assert.throws(() => s.remove(-2, -1), /Error: Character is out of bounds/);
});
});

describe('reset', () => {
Expand Down

0 comments on commit 5c1cba0

Please sign in to comment.