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

Method update

src/MagicString.ts:571–655  ·  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

569 * the content is overwritten, or anything that was appended/prepended to the range as well.
570 */
571 update(start: number, end: number, content: string, options?: boolean | UpdateOptions): this {
572 start = start + this.offset
573 end = end + this.offset
574
575 if (typeof content !== 'string') {
576 throw new MagicStringError(`content must be a string, got ${typeof content}`)
577 }
578
579 if (this.original.length !== 0) {
580 while (start < 0) start += this.original.length
581 while (end < 0) end += this.original.length
582 }
583
584 if (start < 0) {
585 throw new MagicStringError(`start ${start} is out of bounds`)
586 }
587 if (end > this.original.length) {
588 throw new MagicStringError(`end ${end} is out of bounds`)
589 }
590 if (start === end) {
591 throw new MagicStringError(
592 `cannot overwrite a zero-length range at ${start}, use appendLeft() or prependRight()`,
593 )
594 }
595 if (start > end) {
596 throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`)
597 }
598
599 if (DEBUG)
600 this.stats.time('overwrite')
601
602 this._split(start)
603 this._split(end)
604
605 if (options === true) {
606 if (!warned.storeName) {
607 console.warn(
608 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
609 )
610 warned.storeName = true
611 }
612
613 options = { storeName: true }
614 }
615 const optionObject = typeof options === 'object' && options ? options : {}
616 const storeName = optionObject.storeName || false
617 const overwrite = optionObject.overwrite || false
618
619 if (storeName) {
620 const original = this.original.slice(start, end)
621 Object.defineProperty(this.storedNames, original, {
622 writable: true,
623 value: true,
624 enumerable: true,
625 })
626 }
627
628 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