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