Repository navigation
fix: skip empty matches at a chunk boundary inside removed content - #366
Merged
antfu merged 1 commit intoOct 26, 2026
Merged
Conversation
An empty range on a chunk boundary overlaps neither chunk, so _hasRemovedContent never looked at them and reported the match as present. Removing a range in two calls, or any split inside it, made replace and replaceAll insert there although removing it in one call did not.
This was referenced Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since #359, an empty match inside removed content is skipped by
replaceandreplaceAll. That only holds while no chunk boundary sits at the match: removing the same range in two calls gives the same output but a different result.Any split at the match does the same, including one from an earlier
appendLeft(2, '')._hasRemovedContent(start, end)walks the chunks from the one atstartwhilechunk.start < end. For an empty range on a boundary,start === end === chunk.start, so the loop never runs and the match is reported as present. Inside a chunk the search finds the chunk containingstartinstead, whose start is beforeend, which is why the unsplit case works.This handles that one case: an empty range at a chunk boundary is removed when the chunks on both sides of it are. That is the same rule the unsplit case already follows, and it keeps the existing behaviour at the edges of a removed range (
[0, 1, 3, 4]in the #359 test) and at the ends of the string.I checked it exhaustively against that rule: every string of length 1 to 6, no removal or every combination of one or two
remove()calls, and every set of no-op splits, comparing the indices passed to areplaceAll('', fn)replacer and to areplaceAll(/x*/g, fn)replacer. That is 19,761 cases; 17,070 disagree with the rule on master and none do with this change. A random fuzz of the other methods (chunk-split invariance,clone(), self-consistency oflastChar/lastLine/isEmpty/hasChanged) gives identical results before and after.Three tests are added, all failing on master. The suite goes from 454 to 457 and ESLint is clean;
tscreports the same 23 errors as master, none in the changed files.