Skip to content

fix(es/react-compiler): recover source map spans from locations - #12104

Closed
Matthew Costabile (mattcosta7) wants to merge 4 commits into
swc-project:mainfrom
mattcosta7:copilot/update-source-map-emission
Closed

fix(es/react-compiler): recover source map spans from locations#12104
Matthew Costabile (mattcosta7) wants to merge 4 commits into
swc-project:mainfrom
mattcosta7:copilot/update-source-map-emission

Conversation

@mattcosta7

@mattcosta7 Matthew Costabile (mattcosta7) commented Aug 6, 2026

Copy link
Copy Markdown

React Compiler reverse conversion discarded source locations when Babel BaseNode.start/end were absent, producing valid but severely sparse source maps. BaseNode.loc retains the necessary offsets.

  • Span recovery
    • Preserve existing absolute start/end behavior.
    • Fall back to loc.start.index / loc.end.index.
    • Collapse a missing end index to a zero-width span.
    • Keep DUMMY_SP only when neither position representation exists.
let start = loc.start.index.map(|index| source_file_start_pos + BytePos(index));
let end = loc.end.index.or(loc.start.index)
    .map(|index| source_file_start_pos + BytePos(index));
  • Correct offset basis

    • Forward conversion stores start/end as absolute SWC BytePos values.
    • loc.index is file-relative, so the original source-file start position is threaded into ReverseCtx before rebuilding spans. This avoids incorrect mappings for later files in a shared SourceMap.
  • Regression coverage

    • Cover all start/end and loc fallback branches.
    • Add a useMemo/useCallback-heavy source-map density test that asserts mapping and generated-line counts.

Nodes synthesized by React Compiler with neither start/end nor loc still receive DUMMY_SP; mapping those requires separate source-span inheritance work.

Copilot AI and others added 4 commits August 6, 2026 15:14
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>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ mattcosta7
❌ Copilot
You have signed the CLA already but the status is still pending? Let us recheck it.

@mattcosta7 Matthew Costabile (mattcosta7) changed the title Copilot/update source map emission fix(es/react-compiler): recover source map spans from locations Aug 6, 2026
@mattcosta7
Matthew Costabile (mattcosta7) marked this pull request as ready for review August 6, 2026 16:02
@mattcosta7
Matthew Costabile (mattcosta7) requested a review from a team as a code owner August 6, 2026 16:02

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@magic-akari

Copy link
Copy Markdown
Member

duplicate #11979

@magic-akari magic-akari closed this Aug 6, 2026
@github-actions github-actions Bot added this to the Planned milestone Aug 6, 2026
@codspeed-hq

codspeed-hq Bot commented Aug 6, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 200 skipped benchmarks1


Comparing mattcosta7:copilot/update-source-map-emission (022c7ad) with main (3c4f404)

Open in CodSpeed

Footnotes

  1. 200 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants