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

Method clone

src/MagicString.ts:279–322  ·  view source on GitHub ↗

* Returns an independent copy: the chunk list, string-level intro/outro, * sourcemap locations, stored names, indent exclusion ranges, `offset`, * `filename` and `ignoreList` are all copied; later edits to either string * do not affect the other.

()

Source from the content-addressed store, hash-verified

277 * do not affect the other.
278 */
279 clone(): this {
280 const cloned = new MagicString(this.original, {
281 filename: this.filename,
282 ignoreList: this.ignoreList,
283 offset: this.offset,
284 })
285
286 let originalChunk: Chunk | null = this.firstChunk
287 let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone())
288
289 while (originalChunk) {
290 cloned.byStart.set(clonedChunk.start, clonedChunk)
291 cloned.byEnd.set(clonedChunk.end, clonedChunk)
292
293 const nextOriginalChunk: Chunk | null = originalChunk.next
294 const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone()
295
296 if (nextClonedChunk) {
297 clonedChunk.next = nextClonedChunk
298 nextClonedChunk.previous = clonedChunk
299
300 clonedChunk = nextClonedChunk
301 }
302
303 originalChunk = nextOriginalChunk
304 }
305
306 cloned.lastChunk = clonedChunk
307
308 if (this.indentExclusionRanges) {
309 cloned.indentExclusionRanges = this.indentExclusionRanges.slice() as
310 | ExclusionRange
311 | ExclusionRange[]
312 }
313
314 cloned.sourcemapLocations = new BitSet(this.sourcemapLocations)
315 cloned.storedNames = new Set(this.storedNames)
316
317 cloned.intro = this.intro
318 cloned.outro = this.outro
319 cloned.hasMovedChunks = this.hasMovedChunks
320
321 return cloned as this
322 }
323
324 /**
325 * Generates a sourcemap object with raw mappings in array form, rather than encoded as a string.

Callers 1

snipMethod · 0.95

Calls 1

sliceMethod · 0.45

Tested by

no test coverage detected