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

Method update

src/MagicString.ts:602–686  ·  view source on GitHub ↗

* Replaces the characters from `start` to `end` with `content`. The same restrictions as `s.remove()` apply. * * The fourth argument is optional. It can have a storeName property - if true, the original name will be stored * for later inclusion in a sourcemap's names array - and an overwrit

(start: number, end: number, content: string, options?: boolean | UpdateOptions)

Source from the content-addressed store, hash-verified

600 * the content is overwritten, or anything that was appended/prepended to the range as well.
601 */
602 update(start: number, end: number, content: string, options?: boolean | UpdateOptions): this {
603 start = start + this.offset
604 end = end + this.offset
605
606 if (typeof content !== 'string') {
607 throw new MagicStringError(`content must be a string, got ${typeof content}`)
608 }
609
610 if (this.original.length !== 0) {
611 while (start < 0) start += this.original.length
612 while (end < 0) end += this.original.length
613 }
614
615 if (start < 0) {
616 throw new MagicStringError(`start ${start} is out of bounds`)
617 }
618 if (end > this.original.length) {
619 throw new MagicStringError(`end ${end} is out of bounds`)
620 }
621 if (start === end) {
622 throw new MagicStringError(
623 `cannot overwrite a zero-length range at ${start}, use appendLeft() or prependRight()`,
624 )
625 }
626 if (start > end) {
627 throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`)
628 }
629
630 if (DEBUG)
631 this.stats.time('overwrite')
632
633 this._split(start)
634 this._split(end)
635
636 if (options === true) {
637 if (!warned.storeName) {
638 console.warn(
639 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
640 )
641 warned.storeName = true
642 }
643
644 options = { storeName: true }
645 }
646 const optionObject = typeof options === 'object' && options ? options : {}
647 const storeName = optionObject.storeName || false
648 const overwrite = optionObject.overwrite || false
649
650 if (storeName) {
651 const original = this.original.slice(start, end)
652 Object.defineProperty(this.storedNames, original, {
653 writable: true,
654 value: true,
655 enumerable: true,
656 })
657 }
658
659 const first = this.byStart.get(start)

Callers 2

overwriteMethod · 0.95

Calls 5

_splitMethod · 0.95
timeMethod · 0.80
timeEndMethod · 0.80
sliceMethod · 0.45
editMethod · 0.45

Tested by

no test coverage detected