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

Method update

src/MagicString.js:373–442  ·  view source on GitHub ↗
(start, end, content, options)

Source from the content-addressed store, hash-verified

371 }
372
373 update(start, end, content, options) {
374 start = start + this.offset;
375 end = end + this.offset;
376
377 if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
378
379 if (this.original.length !== 0) {
380 while (start < 0) start += this.original.length;
381 while (end < 0) end += this.original.length;
382 }
383
384 if (end > this.original.length) throw new Error('end is out of bounds');
385 if (start === end)
386 throw new Error(
387 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
388 );
389
390 if (DEBUG) this.stats.time('overwrite');
391
392 this._split(start);
393 this._split(end);
394
395 if (options === true) {
396 if (!warned.storeName) {
397 console.warn(
398 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
399 );
400 warned.storeName = true;
401 }
402
403 options = { storeName: true };
404 }
405 const storeName = options !== undefined ? options.storeName : false;
406 const overwrite = options !== undefined ? options.overwrite : false;
407
408 if (storeName) {
409 const original = this.original.slice(start, end);
410 Object.defineProperty(this.storedNames, original, {
411 writable: true,
412 value: true,
413 enumerable: true,
414 });
415 }
416
417 const first = this.byStart[start];
418 const last = this.byEnd[end];
419
420 if (first) {
421 let chunk = first;
422 while (chunk !== last) {
423 if (chunk.next !== this.byStart[chunk.end]) {
424 throw new Error('Cannot overwrite across a split point');
425 }
426 chunk = chunk.next;
427 chunk.edit('', false);
428 }
429
430 first.edit(content, storeName, !overwrite);

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