fix: correct source map columns for reused code that gets dedented#1433
Open
Kal-Aster wants to merge 2 commits into
Open
fix: correct source map columns for reused code that gets dedented#1433Kal-Aster wants to merge 2 commits into
Kal-Aster wants to merge 2 commits into
Conversation
Lines#getIndentAt never reports a negative indentation, so lines that already begin at column zero stay put when the surrounding lines are dedented. Mapping#indent nevertheless shifted every mapping by the full requested amount, which left the mappings of those lines pointing at the wrong columns, and made Lines#getSourceMap fail its sourceChar === targetChar invariant. Compare the indentation of the old and new Lines objects instead, so each mapping moves as far as its line really moved. Both skipFirstLine and noNegativeColumns become implicit in that comparison: line 1 keeps its indentation, so it never shifts, and clamping to zero is now part of computing the shift. Fixes benjamn#1402.
Reusing a parenthesized arrow function dedents it, while the lines of its body already begin at column zero and so stay where they are. Before the previous commit this threw Invariant failed instead of producing a source map.
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.
Fixes #1402.
DISCLAIMER
I don't know the opinion about AI usage in this project, however I don't know anything about how this works and this bug is sitting in here since a long time, so I used AI to produce a fix. I ensured that the code was matching the rest of the code in both style and structure, but other than that I just prompted the AI.
Follows an explanation of AI about the bug and its fix:
Lines#getIndentAtnever reports a negative indentation, so a line already at column zerostays put while the lines around it are dedented.
Mapping#indentshifted every mapping bythe full requested amount anyway, leaving those mappings pointing left of the text they
describe, which failed the
sourceChar === targetCharinvariant inLines#getSourceMap.It now derives the shift by comparing the indentation the old and new
Linesactuallyrender, so each mapping moves as far as its own line moved.
skipFirstLineandnoNegativeColumnsfall out of that comparison, makingMapping#indent27 lines shorter.Added a regression test that throws
Invariant failedwithout the fix.