fix(es/react-compiler): recover source map spans from locations - #12104
fix(es/react-compiler): recover source map spans from locations#12104Matthew Costabile (mattcosta7) wants to merge 4 commits into
Conversation
Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 022c7adc36
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "Codex (@codex) address that feedback".
| ConvertResult { | ||
| file, | ||
| preserved_ast: ctx.preserved_ast.into_inner(), | ||
| source_file_start_pos: program.span().lo, |
There was a problem hiding this comment.
Use the file start for loc-index recovery
This value is later added to Babel loc.index values to recover SWC spans, but program.span().lo is the first parsed token rather than the beginning of the source file. For any input with leading whitespace or comments before the first token, a compiler-created node that has only loc will be shifted forward by that leading length (for example, loc.index == 0 maps to the first token instead of BytePos(1)), so emitted sourcemaps point at the wrong original text. Pass the actual file start, or derive it from the program base location, instead.
Useful? React with 👍 / 👎.
| let start = loc | ||
| .start | ||
| .index | ||
| .map(|index| self.source_file_start_pos + BytePos(index)); |
There was a problem hiding this comment.
Normalize loc.index before using it for shared SourceMaps
When callers parse multiple files with one SourceMap, this fallback still produces wrong spans because the forward converter builds loc.index from the absolute SWC BytePos and clamps it to source_text.len() instead of subtracting the source file's start. For every file after the first, React-compiler-created nodes that only have loc can therefore recover EOF-ish or out-of-file positions when this line adds the source-file base, so the sourcemap entries are skipped or point at the wrong text. Make the forward loc.index file-relative before relying on it here.
Useful? React with 👍 / 👎.
|
duplicate #11979 |
Merging this PR will not alter performance
Comparing Footnotes
|
React Compiler reverse conversion discarded source locations when Babel
BaseNode.start/endwere absent, producing valid but severely sparse source maps.BaseNode.locretains the necessary offsets.start/endbehavior.loc.start.index/loc.end.index.DUMMY_SPonly when neither position representation exists.Correct offset basis
start/endas absolute SWCBytePosvalues.loc.indexis file-relative, so the original source-file start position is threaded intoReverseCtxbefore rebuilding spans. This avoids incorrect mappings for later files in a sharedSourceMap.Regression coverage
start/endandlocfallback branches.useMemo/useCallback-heavy source-map density test that asserts mapping and generated-line counts.Nodes synthesized by React Compiler with neither
start/endnorlocstill receiveDUMMY_SP; mapping those requires separate source-span inheritance work.