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
14 changes: 14 additions & 0 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export class Chunk {
declare content: string
declare storeName: boolean | undefined
declare edited: boolean
// An overwrite that spans several chunks stores its replacement on the first
// of them and empties the rest. The first records where the overwritten range
// ends in `replacesUntil`, and the rest record where it starts in
// `replacedFrom`. Both are -1 otherwise
declare replacesUntil: number
declare replacedFrom: number
declare previous: Chunk | null
declare next: Chunk | null

Expand All @@ -23,6 +29,8 @@ export class Chunk {
this.content = content
this.storeName = false
this.edited = false
this.replacesUntil = -1
this.replacedFrom = -1

/* v8 ignore next 8 -- DEBUG is always true in tests */
if (DEBUG) {
Expand Down Expand Up @@ -54,6 +62,8 @@ export class Chunk {
chunk.content = this.content
chunk.storeName = this.storeName
chunk.edited = this.edited
chunk.replacesUntil = this.replacesUntil
chunk.replacedFrom = this.replacedFrom

return chunk
}
Expand Down Expand Up @@ -106,6 +116,8 @@ export class Chunk {
reset(): void {
this.intro = ''
this.outro = ''
this.replacesUntil = -1
this.replacedFrom = -1
if (this.edited) {
this.content = this.original
this.storeName = false
Expand Down Expand Up @@ -138,6 +150,8 @@ export class Chunk {
// TODO is this block necessary?...
// content only, so the outro moved over above is kept
newChunk.edit('', false, true)
// if an overwrite emptied this chunk, its replacement stands for both halves
newChunk.replacedFrom = this.replacedFrom
this.content = ''
}
else {
Expand Down
18 changes: 17 additions & 1 deletion src/MagicString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,9 +858,14 @@ export class MagicString {
// Unless overwriting, inserts on these chunks are kept as they are on
// `first`, so the result does not depend on where the range was split
chunk.edit('', false, !overwrite)
// emptied, not removed: `first` holds the replacement for this chunk too
chunk.replacesUntil = -1
chunk.replacedFrom = start
}

first.edit(content, storeName, !overwrite)
first.replacesUntil = end
first.replacedFrom = -1
}
else {
// must be inserting at the end
Expand Down Expand Up @@ -996,6 +1001,8 @@ export class MagicString {
if (chunk.end < end)
chunk.outro = ''
chunk.edit('', false, true)
chunk.replacesUntil = -1
chunk.replacedFrom = -1

chunk = end > chunk.end ? this.byStart.get(chunk.end) : null
}
Expand Down Expand Up @@ -1456,8 +1463,17 @@ export class MagicString {
this.lastSearchedChunk = chunk
}
while (chunk && chunk.start < end) {
if (chunk.content === '')
// A chunk emptied by an overwrite spanning several chunks is not removed:
// the replacement on the first of them stands for it. So it counts as
// present, unless the match leaves that first chunk out (overwriting the
// match would add a second replacement) or the first chunk has been
// edited since and its replacement no longer reaches this far
if (
chunk.content === ''
&& (chunk.replacedFrom < start || this.byStart.get(chunk.replacedFrom)!.replacesUntil < chunk.end)
) {
return true
}
chunk = this.byStart.get(chunk.end)
}
return false
Expand Down
12 changes: 12 additions & 0 deletions tests/MagicString/replace-all.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,17 @@ describe('magicString', () => {
assert.strictEqual(create().replaceAll(/0/g, '1').toString(), '--!--')
assert.strictEqual(create().replace('0', '1').toString(), '--!--')
})

it('replaces matches over a range that one overwrite spread across several chunks', () => {
// the first pass overwrites 'aa', which the insert of nothing has split,
// and the second pass matches the same range again
const s = new MagicString('aa').appendLeft(1, '')
s.replaceAll(/a+/g, 'P')
assert.strictEqual(s.replaceAll(/a+/g, 'Q').toString(), 'Q')

assert.strictEqual(new MagicString('aa').appendLeft(1, '').overwrite(0, 2, 'Q').replaceAll('aa', 'P').toString(), 'P')
// any earlier edit can split the range, here the removal
assert.strictEqual(new MagicString('abcd').remove(0, 1).update(0, 2, 'Q').replaceAll('ab', 'P').toString(), 'Pcd')
})
})
})
27 changes: 27 additions & 0 deletions tests/MagicString/replace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,33 @@ describe('magicString', () => {
assert.strictEqual(new MagicString('00').remove(0, 2).replace(/0/, '1').toString(), '')
})

it('replaces a match over a range that one overwrite spread across several chunks', () => {
// `appendLeft(1, '')` inserts nothing but splits the range, so the
// replacement is stored on the first chunk and the second is emptied,
// which must not count as removed
assert.strictEqual(new MagicString('aa').update(0, 2, 'Q').replace('aa', 'P').toString(), 'P')
const s = new MagicString('aa').appendLeft(1, '').update(0, 2, 'Q')
assert.strictEqual(s.clone().replace('aa', 'P').toString(), 'P')
assert.strictEqual(s.clone().replace(/a+/, 'P').toString(), 'P')

// the emptied chunk may itself be split afterwards
const t = new MagicString('aaa').appendLeft(1, '').overwrite(0, 3, 'Q').appendLeft(2, '')
assert.strictEqual(t.replace('aaa', 'P').toString(), 'P')
})

it('skips a match over the rest of an overwritten range when the replacement is left out or gone', () => {
const create = () => new MagicString('abc').appendLeft(1, '').update(0, 3, 'Q')

// the match leaves out the first chunk, so the replacement would be doubled
assert.strictEqual(create().replace('bc', 'P').toString(), 'Q')
// editing the first chunk on its own drops the replacement for the rest
assert.strictEqual(create().update(0, 1, 'R').replace('abc', 'P').toString(), 'R')
assert.strictEqual(create().reset(0, 1).replace('abc', 'P').toString(), 'a')
// and removed content stays removed
assert.strictEqual(create().remove(1, 3).replace('abc', 'P').toString(), 'Q')
assert.strictEqual(create().update(1, 3, '').replace('abc', 'P').toString(), 'Q')
})

it('skips an empty match between two removed ranges', () => {
// the lookahead matches at 2, which is removed whether [1, 3) went in one
// call or two
Expand Down
Loading