* 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)
| 713 | * the content is overwritten, or anything that was appended/prepended to the range as well. |
| 714 | */ |
| 715 | update(start: number, end: number, content: string, options?: boolean | UpdateOptions): this { |
| 716 | start = start + this.offset |
| 717 | end = end + this.offset |
| 718 | |
| 719 | if (typeof content !== 'string') { |
| 720 | throw new MagicStringError(`content must be a string, got ${typeof content}`) |
| 721 | } |
| 722 | |
| 723 | if (this.original.length !== 0) { |
| 724 | if (start < 0) |
| 725 | start = Math.max(0, start + this.original.length) |
| 726 | if (end < 0) |
| 727 | end = Math.max(0, end + this.original.length) |
| 728 | } |
| 729 | |
| 730 | if (start < 0) { |
| 731 | throw new MagicStringError(`start ${start} is out of bounds`) |
| 732 | } |
| 733 | if (end > this.original.length) { |
| 734 | throw new MagicStringError(`end ${end} is out of bounds`) |
| 735 | } |
| 736 | if (start === end) { |
| 737 | throw new MagicStringError( |
| 738 | `cannot overwrite a zero-length range at ${start}, use appendLeft() or prependRight()`, |
| 739 | ) |
| 740 | } |
| 741 | if (start > end) { |
| 742 | throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`) |
| 743 | } |
| 744 | |
| 745 | /* v8 ignore next 2 -- DEBUG is always true in tests */ |
| 746 | if (DEBUG) |
| 747 | this.stats.time('overwrite') |
| 748 | |
| 749 | this._split(start) |
| 750 | this._split(end) |
| 751 | |
| 752 | if (options === true) { |
| 753 | if (!warned.storeName) { |
| 754 | console.warn( |
| 755 | 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string', |
| 756 | ) |
| 757 | warned.storeName = true |
| 758 | } |
| 759 | |
| 760 | options = { storeName: true } |
| 761 | } |
| 762 | const optionObject = typeof options === 'object' && options ? options : {} |
| 763 | const storeName = optionObject.storeName || false |
| 764 | const overwrite = optionObject.overwrite || false |
| 765 | |
| 766 | if (storeName) { |
| 767 | const original = this.original.slice(start, end) |
| 768 | Object.defineProperty(this.storedNames, original, { |
| 769 | writable: true, |
| 770 | value: true, |
| 771 | enumerable: true, |
| 772 | }) |