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