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

Method replaceMatch

src/MagicString.ts:1454–1476  ·  view source on GitHub ↗
(match: RegExpMatchArray)

Source from the content-addressed store, hash-verified

1452 // Returns true once a surviving match has been handled, so a non-global
1453 // search can stop at the first match still present in the output.
1454 const replaceMatch = (match: RegExpMatchArray): boolean => {
1455 /* v8 ignore next 2 -- `match.index` is always defined for matches from `matchAll` */
1456 if (match.index == null)
1457 return false
1458
1459 if (this._hasRemovedContent(match.index, match.index + match[0].length))
1460 return false
1461
1462 const replacement = getReplacement(match, match.index, this.original)
1463 if (replacement === match[0])
1464 return true
1465
1466 if (match[0].length === 0) {
1467 // a zero-length match spans no characters, so there is no range to
1468 // overwrite - the replacement is an insertion at the matched position,
1469 // which is what `String.prototype.replace` does for an empty match
1470 this.appendRight(match.index - this.offset, replacement)
1471 }
1472 else {
1473 this.overwrite(match.index - this.offset, match.index + match[0].length - this.offset, replacement)
1474 }
1475 return true
1476 }
1477
1478 if (searchValue.global) {
1479 // `String.prototype.replace` starts a global regexp from the beginning of

Callers

nothing calls this directly

Calls 3

_hasRemovedContentMethod · 0.95
appendRightMethod · 0.95
overwriteMethod · 0.95

Tested by

no test coverage detected