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

Method hasChanged

src/MagicString.ts:1323–1358  ·  view source on GitHub ↗

* Indicates if the string has been changed.

()

Source from the content-addressed store, hash-verified

1321 * Indicates if the string has been changed.
1322 */
1323 hasChanged(): boolean {
1324 if (this.intro || this.outro)
1325 return true
1326
1327 // How much of `original` the chunks visited so far reproduce, which is not
1328 // the same as how much of it they span: an overwrite covering several
1329 // chunks stores the whole replacement on the first of them and empties the
1330 // rest, so a chunk's content has to be compared against the original text
1331 // at the offset it is emitted at rather than against its own start and end
1332 let outputIndex = 0
1333 let chunk: Chunk | null = this.firstChunk
1334
1335 while (chunk) {
1336 // Any intro/outro on a chunk means content was inserted
1337 if (chunk.intro || chunk.outro)
1338 return true
1339
1340 if (!chunk.edited && chunk.start === outputIndex) {
1341 // An untouched chunk still at its original offset reproduces the
1342 // original exactly, so it needs no comparison
1343 outputIndex = chunk.end
1344 }
1345 else {
1346 // `startsWith` with an offset compares in place, where slicing the
1347 // original first would allocate on every edited chunk
1348 if (!this.original.startsWith(chunk.content, outputIndex))
1349 return true
1350
1351 outputIndex += chunk.content.length
1352 }
1353
1354 chunk = chunk.next
1355 }
1356
1357 return outputIndex !== this.original.length
1358 }
1359
1360 /** @internal */
1361 _replaceRegexp(searchValue: RegExp, replacement: string | ReplacementFunction): this {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected