Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not use negative indices for remove in empty string #281

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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