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

Method hasChanged

src/MagicString.ts:1168–1203  ·  view source on GitHub ↗

* Indicates if the string has been changed.

()

Source from the content-addressed store, hash-verified

1166 * Indicates if the string has been changed.
1167 */
1168 hasChanged(): boolean {
1169 if (this.intro || this.outro)
1170 return true
1171
1172 // How much of `original` the chunks visited so far reproduce, which is not
1173 // the same as how much of it they span: an overwrite covering several
1174 // chunks stores the whole replacement on the first of them and empties the
1175 // rest, so a chunk's content has to be compared against the original text
1176 // at the offset it is emitted at rather than against its own start and end
1177 let outputIndex = 0
1178 let chunk: Chunk | null = this.firstChunk
1179
1180 while (chunk) {
1181 // Any intro/outro on a chunk means content was inserted
1182 if (chunk.intro || chunk.outro)
1183 return true
1184
1185 if (!chunk.edited && chunk.start === outputIndex) {
1186 // An untouched chunk still at its original offset reproduces the
1187 // original exactly, so it needs no comparison
1188 outputIndex = chunk.end
1189 }
1190 else {
1191 // `startsWith` with an offset compares in place, where slicing the
1192 // original first would allocate on every edited chunk
1193 if (!this.original.startsWith(chunk.content, outputIndex))
1194 return true
1195
1196 outputIndex += chunk.content.length
1197 }
1198
1199 chunk = chunk.next
1200 }
1201
1202 return outputIndex !== this.original.length
1203 }
1204
1205 /** @internal */
1206 _replaceRegexp(searchValue: RegExp, replacement: string | ReplacementFunction): this {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected