Skip to content
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
6 changes: 6 additions & 0 deletions src/MagicString.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,18 @@ export default class MagicString {
if (DEBUG) this.stats.time('_split');

let chunk = this.lastSearchedChunk;
let previousChunk = chunk;
const searchForward = index > chunk.end;

while (chunk) {
if (chunk.contains(index)) return this._splitChunk(chunk, index);

chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];

// Prevent infinite loop (e.g. via empty chunks, where start === end)
if (chunk === previousChunk) return;

previousChunk = chunk;
}
}

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

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

it('should append/prepend at end of string when index is out of upper bound', () => {
const s = new MagicString('x');
s.prependLeft(6, 'A');
s.appendLeft(6, 'B');
s.prependRight(6, 'C');
s.appendRight(6, 'D');

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

it('should append/prepend on empty string when index is out of upper bound', () => {
const s = new MagicString('');
s.prependLeft(6, 'A');
s.appendLeft(6, 'B');
s.prependRight(6, 'C');
s.appendRight(6, 'D');

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

describe('appendLeft', () => {
Expand Down
Loading