Skip to content

fix: correct source map columns for reused code that gets dedented#1433

Open
Kal-Aster wants to merge 2 commits into
benjamn:masterfrom
Kal-Aster:master
Open

fix: correct source map columns for reused code that gets dedented#1433
Kal-Aster wants to merge 2 commits into
benjamn:masterfrom
Kal-Aster:master

Conversation

@Kal-Aster

Copy link
Copy Markdown

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#getIndentAt never reports a negative indentation, so a line already at column zero
stays put while the lines around it are dedented. Mapping#indent shifted every mapping by
the full requested amount anyway, leaving those mappings pointing left of the text they
describe, which failed the sourceChar === targetChar invariant in Lines#getSourceMap.

It now derives the shift by comparing the indentation the old and new Lines actually
render, so each mapping moves as far as its own line moved. skipFirstLine and
noNegativeColumns fall out of that comparison, making Mapping#indent 27 lines shorter.

Added a regression test that throws Invariant failed without the fix.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invariant Failed - Sourcemaps

1 participant