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

Method hasChanged

src/MagicString.ts:1160–1195  ·  view source on GitHub ↗

* Indicates if the string has been changed.

()

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected