fix: trim replacements longer than the range they replace#312
Open
sarathfrancis90 wants to merge 1 commit into
Open
fix: trim replacements longer than the range they replace#312sarathfrancis90 wants to merge 1 commit into
sarathfrancis90 wants to merge 1 commit into
Conversation
Chunk.trimStart/trimEnd derived the split index from content.length, which only equals the chunk's original span while the chunk is unedited. Once a chunk has been overwritten with a longer replacement the index lands outside the chunk, producing a chunk whose end precedes its start and, for trimStart, a negative one - which getLocator resolves to source line -1, column NaN. An edited chunk's content has no positional correspondence to the original range anyway, so there is nothing to split: trim it in place.
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.
Trimming a chunk that was overwritten with a replacement longer than the range it covers corrupts that chunk's bounds, and the resulting sourcemap points outside the source:
Chunk.trimStart/trimEndpick the split index offcontent.length, which is only the chunk's original span while the chunk is unedited. Here the chunk covers[0, 1)but its content is 6 characters, sotrimStartsplits at1 - 3 = -2and leaves chunks[0, -2)and[-2, 1).getLocatorthen resolves the negative start to line -1, column NaN.trimEndhas the mirror problem — it splits pastend, sooverwrite(1, 2, 'xyz ')+trimEnd()leaves a[4, 2)chunk and makesslice(0, 2)throw "Cannot use replaced character 2 as slice end anchor".An edited chunk's content has no positional correspondence to its original range, so splitting it at a content offset isn't meaningful in the first place — both halves end up edited regardless. I made the edited case trim in place instead, and left the unedited path alone since that's where the split actually buys accurate per-character mappings.
The existing edited-chunk trim tests (from #257) still pass unchanged. One deliberate difference: a trimmed replacement now maps to the start of the range it replaces rather than to an offset computed from its own length — e.g.
overwrite(0, 6, ' abcd')+trimStart()maps at column 0 instead of column 2. Output strings are identical, and unedited trimming is untouched.I found this fuzzing random sequences of valid operations and checking the decoded mappings stay in bounds.
Verified with
pnpm run lint,pnpm test(221 passing) andpnpm run typecheck.