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

Method update

src/MagicString.ts:571–647  ·  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 TypeError('replacement content must be a string')
577
578 if (this.original.length !== 0) {
579 while (start < 0) start += this.original.length
580 while (end < 0) end += this.original.length
581 }
582
583 if (end > this.original.length)
584 throw new Error('end is out of bounds')
585 if (start === end) {
586 throw new Error(
587 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
588 )
589 }
590
591 if (DEBUG)
592 this.stats.time('overwrite')
593
594 this._split(start)
595 this._split(end)
596
597 if (options === true) {
598 if (!warned.storeName) {
599 console.warn(
600 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
601 )
602 warned.storeName = true
603 }
604
605 options = { storeName: true }
606 }
607 const optionObject = typeof options === 'object' && options ? options : {}
608 const storeName = optionObject.storeName || false
609 const overwrite = optionObject.overwrite || false
610
611 if (storeName) {
612 const original = this.original.slice(start, end)
613 Object.defineProperty(this.storedNames, original, {
614 writable: true,
615 value: true,
616 enumerable: true,
617 })
618 }
619
620 const first = this.byStart[start]
621 const last = this.byEnd[end]
622
623 if (first) {
624 let chunk = first
625 while (chunk !== last) {
626 if (chunk.next !== this.byStart[chunk.end]) {
627 throw new Error('Cannot overwrite across a split point')
628 }

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