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

Method update

src/MagicString.ts:602–688  ·  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 if (start < 0)
612 start = Math.max(0, start + this.original.length)
613 if (end < 0)
614 end = Math.max(0, end + this.original.length)
615 }
616
617 if (start < 0) {
618 throw new MagicStringError(`start ${start} is out of bounds`)
619 }
620 if (end > this.original.length) {
621 throw new MagicStringError(`end ${end} is out of bounds`)
622 }
623 if (start === end) {
624 throw new MagicStringError(
625 `cannot overwrite a zero-length range at ${start}, use appendLeft() or prependRight()`,
626 )
627 }
628 if (start > end) {
629 throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`)
630 }
631
632 if (DEBUG)
633 this.stats.time('overwrite')
634
635 this._split(start)
636 this._split(end)
637
638 if (options === true) {
639 if (!warned.storeName) {
640 console.warn(
641 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
642 )
643 warned.storeName = true
644 }
645
646 options = { storeName: true }
647 }
648 const optionObject = typeof options === 'object' && options ? options : {}
649 const storeName = optionObject.storeName || false
650 const overwrite = optionObject.overwrite || false
651
652 if (storeName) {
653 const original = this.original.slice(start, end)
654 Object.defineProperty(this.storedNames, original, {
655 writable: true,
656 value: true,
657 enumerable: true,
658 })
659 }

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