MCPcopy Create free account
hub / github.com/Rich-Harris/magic-string / _hasRemovedContent

Method _hasRemovedContent

src/MagicString.ts:1399–1428  ·  view source on GitHub ↗

* Whether the original range [start, end) has had any of its characters * removed. `replace`/`replaceAll` search `original`, so a match can land on * text that is no longer in the output - overwriting it would resurrect the * removed characters, so such matches are skipped instead. *

(start: number, end: number)

Source from the content-addressed store, hash-verified

1397 * @internal
1398 */
1399 _hasRemovedContent(start: number, end: number): boolean {
1400 // Chunks tile `original`, so only those overlapping [start, end) matter: find
1401 // the one holding `start`, searching from the last visited chunk as _split
1402 // does, and walk forward through byStart. replace/replaceAll call this once
1403 // per match, so scanning the whole list from firstChunk made them quadratic.
1404 const atBoundary = this.byStart.get(start)
1405 if (atBoundary && start === end) {
1406 // An empty range at a chunk boundary overlaps neither chunk, but it lies
1407 // inside removed content when the characters on both sides are removed,
1408 // just as it does when no split separates them
1409 const previous = this.byEnd.get(start)
1410 return !!previous && previous.content === '' && atBoundary.content === ''
1411 }
1412 let chunk = this._chunkAt(start)
1413 while (chunk && chunk.start < end) {
1414 // A chunk emptied by an overwrite spanning several chunks is not removed:
1415 // the replacement on the first of them stands for it. So it counts as
1416 // present, unless the match leaves that first chunk out (overwriting the
1417 // match would add a second replacement) or the first chunk has been
1418 // edited since and its replacement no longer reaches this far
1419 if (
1420 chunk.content === ''
1421 && (chunk.replacedFrom < start || this.byStart.get(chunk.replacedFrom)!.replacesUntil < chunk.end)
1422 ) {
1423 return true
1424 }
1425 chunk = this.byStart.get(chunk.end)
1426 }
1427 return false
1428 }
1429
1430 /** @internal */
1431 _replaceRegexp(searchValue: RegExp, replacement: string | ReplacementFunction): this {

Callers 3

replaceMatchMethod · 0.95
_replaceStringMethod · 0.95
_replaceAllStringMethod · 0.95

Calls 1

_chunkAtMethod · 0.95

Tested by

no test coverage detected