Skip to content

Commit aed5675

Browse files
theRizwanantfu
andauthored
fix: don't treat chunks emptied by an overwrite as removed in replace (#367)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 5aab3bd commit aed5675

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎src/Chunk.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export class Chunk {
99
declare content: string
1010
declare storeName: boolean | undefined
1111
declare edited: boolean
12+
// An overwrite that spans several chunks stores its replacement on the first
13+
// of them and empties the rest. The first records where the overwritten range
14+
// ends in `replacesUntil`, and the rest record where it starts in
15+
// `replacedFrom`. Both are -1 otherwise
16+
declare replacesUntil: number
17+
declare replacedFrom: number
1218
declare previous: Chunk | null
1319
declare next: Chunk | null
1420

@@ -23,6 +29,8 @@ export class Chunk {
2329
this.content = content
2430
this.storeName = false
2531
this.edited = false
32+
this.replacesUntil = -1
33+
this.replacedFrom = -1
2634

2735
/* v8 ignore next 8 -- DEBUG is always true in tests */
2836
if (DEBUG) {
@@ -54,6 +62,8 @@ export class Chunk {
5462
chunk.content = this.content
5563
chunk.storeName = this.storeName
5664
chunk.edited = this.edited
65+
chunk.replacesUntil = this.replacesUntil
66+
chunk.replacedFrom = this.replacedFrom
5767

5868
return chunk
5969
}
@@ -106,6 +116,8 @@ export class Chunk {
106116
reset(): void {
107117
this.intro = ''
108118
this.outro = ''
119+
this.replacesUntil = -1
120+
this.replacedFrom = -1
109121
if (this.edited) {
110122
this.content = this.original
111123
this.storeName = false
@@ -138,6 +150,8 @@ export class Chunk {
138150
// TODO is this block necessary?...
139151
// content only, so the outro moved over above is kept
140152
newChunk.edit('', false, true)
153+
// if an overwrite emptied this chunk, its replacement stands for both halves
154+
newChunk.replacedFrom = this.replacedFrom
141155
this.content = ''
142156
}
143157
else {

‎src/MagicString.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,14 @@ export class MagicString {
858858
// Unless overwriting, inserts on these chunks are kept as they are on
859859
// `first`, so the result does not depend on where the range was split
860860
chunk.edit('', false, !overwrite)
861+
// emptied, not removed: `first` holds the replacement for this chunk too
862+
chunk.replacesUntil = -1
863+
chunk.replacedFrom = start
861864
}
862865

863866
first.edit(content, storeName, !overwrite)
867+
first.replacesUntil = end
868+
first.replacedFrom = -1
864869
}
865870
else {
866871
// must be inserting at the end
@@ -996,6 +1001,8 @@ export class MagicString {
9961001
if (chunk.end < end)
9971002
chunk.outro = ''
9981003
chunk.edit('', false, true)
1004+
chunk.replacesUntil = -1
1005+
chunk.replacedFrom = -1
9991006

10001007
chunk = end > chunk.end ? this.byStart.get(chunk.end) : null
10011008
}
@@ -1456,8 +1463,17 @@ export class MagicString {
14561463
this.lastSearchedChunk = chunk
14571464
}
14581465
while (chunk && chunk.start < end) {
1459-
if (chunk.content === '')
1466+
// A chunk emptied by an overwrite spanning several chunks is not removed:
1467+
// the replacement on the first of them stands for it. So it counts as
1468+
// present, unless the match leaves that first chunk out (overwriting the
1469+
// match would add a second replacement) or the first chunk has been
1470+
// edited since and its replacement no longer reaches this far
1471+
if (
1472+
chunk.content === ''
1473+
&& (chunk.replacedFrom < start || this.byStart.get(chunk.replacedFrom)!.replacesUntil < chunk.end)
1474+
) {
14601475
return true
1476+
}
14611477
chunk = this.byStart.get(chunk.end)
14621478
}
14631479
return false

‎tests/MagicString/replace-all.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,17 @@ describe('magicString', () => {
207207
assert.strictEqual(create().replaceAll(/0/g, '1').toString(), '--!--')
208208
assert.strictEqual(create().replace('0', '1').toString(), '--!--')
209209
})
210+
211+
it('replaces matches over a range that one overwrite spread across several chunks', () => {
212+
// the first pass overwrites 'aa', which the insert of nothing has split,
213+
// and the second pass matches the same range again
214+
const s = new MagicString('aa').appendLeft(1, '')
215+
s.replaceAll(/a+/g, 'P')
216+
assert.strictEqual(s.replaceAll(/a+/g, 'Q').toString(), 'Q')
217+
218+
assert.strictEqual(new MagicString('aa').appendLeft(1, '').overwrite(0, 2, 'Q').replaceAll('aa', 'P').toString(), 'P')
219+
// any earlier edit can split the range, here the removal
220+
assert.strictEqual(new MagicString('abcd').remove(0, 1).update(0, 2, 'Q').replaceAll('ab', 'P').toString(), 'Pcd')
221+
})
210222
})
211223
})

‎tests/MagicString/replace.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,33 @@ describe('magicString', () => {
340340
assert.strictEqual(new MagicString('00').remove(0, 2).replace(/0/, '1').toString(), '')
341341
})
342342

343+
it('replaces a match over a range that one overwrite spread across several chunks', () => {
344+
// `appendLeft(1, '')` inserts nothing but splits the range, so the
345+
// replacement is stored on the first chunk and the second is emptied,
346+
// which must not count as removed
347+
assert.strictEqual(new MagicString('aa').update(0, 2, 'Q').replace('aa', 'P').toString(), 'P')
348+
const s = new MagicString('aa').appendLeft(1, '').update(0, 2, 'Q')
349+
assert.strictEqual(s.clone().replace('aa', 'P').toString(), 'P')
350+
assert.strictEqual(s.clone().replace(/a+/, 'P').toString(), 'P')
351+
352+
// the emptied chunk may itself be split afterwards
353+
const t = new MagicString('aaa').appendLeft(1, '').overwrite(0, 3, 'Q').appendLeft(2, '')
354+
assert.strictEqual(t.replace('aaa', 'P').toString(), 'P')
355+
})
356+
357+
it('skips a match over the rest of an overwritten range when the replacement is left out or gone', () => {
358+
const create = () => new MagicString('abc').appendLeft(1, '').update(0, 3, 'Q')
359+
360+
// the match leaves out the first chunk, so the replacement would be doubled
361+
assert.strictEqual(create().replace('bc', 'P').toString(), 'Q')
362+
// editing the first chunk on its own drops the replacement for the rest
363+
assert.strictEqual(create().update(0, 1, 'R').replace('abc', 'P').toString(), 'R')
364+
assert.strictEqual(create().reset(0, 1).replace('abc', 'P').toString(), 'a')
365+
// and removed content stays removed
366+
assert.strictEqual(create().remove(1, 3).replace('abc', 'P').toString(), 'Q')
367+
assert.strictEqual(create().update(1, 3, '').replace('abc', 'P').toString(), 'Q')
368+
})
369+
343370
it('skips an empty match between two removed ranges', () => {
344371
// the lookahead matches at 2, which is removed whether [1, 3) went in one
345372
// call or two

0 commit comments

Comments
 (0)