Skip to content

Rebase React Compiler loc.index on real source-file start to fix span inversion across shared SourceMaps - #7

Draft
mattcosta7 with Copilot wants to merge 2 commits into
copilot/update-source-map-emissionfrom
copilot/address-review-comments
Draft

Rebase React Compiler loc.index on real source-file start to fix span inversion across shared SourceMaps#7
mattcosta7 with Copilot wants to merge 2 commits into
copilot/update-source-map-emissionfrom
copilot/address-review-comments

Conversation

Copilot AI commented Aug 6, 2026

Copy link
Copy Markdown

This addresses incorrect span reconstruction when React-Compiler-mutated nodes fall back to loc: forward conversion treated offsets as if every file started at BytePos(1), and reverse conversion rebased using a file start derived from program.span().lo. In multi-file SourceMaps and files with leading trivia/shebang, this broke source-map fidelity and could push recovered spans out of file bounds.

  • Forward conversion basis is now explicit and file-correct

    • Added source_file_start_pos: BytePos to ConvertCtx and ConvertResult, passed explicitly into convert_program.
    • ConvertCtx::position now computes file-relative indices via:
      let offset = offset
          .saturating_sub(self.source_file_start_pos.0)
          .min(self.source_text.len() as u32);
    • Clarified invariant in docs/comments:
      • BaseNode.start/end/node_id = absolute SWC BytePos
      • loc.line/column/index = Babel-style file-relative location
  • Reverse fallback keeps exact inverse semantics

    • Kept span_from_base fallback logic (source_file_start_pos + BytePos(index)) unchanged.
    • Expanded comments to document the inversion contract between absolute start/end and file-relative loc.index.
  • API and caller plumbing now use real SourceFile.start_pos

    • transform/lint now accept source_file_start_pos: BytePos.
    • Threaded through transform_source/lint_source and parser helpers.
    • Updated in-tree call sites (crates/swc, fixture harness, example binary) to pass actual file start, avoiding silent BytePos(1) assumptions.
  • Regression coverage for failure modes

    • Added tests for:
      • leading-comment and shebang inputs (loc-only index == 0 recovers file start)
      • shared SourceMap second-file transform (recovered mappings stay within that file’s range)
      • non-BytePos(1) round-trip invariant (absolute == start_pos + loc.index)
    • Updated one fixture expectation to match corrected round-trip behavior for JSX type arguments.

Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix span calculation in ReverseCtx from review comments Rebase React Compiler loc.index on real source-file start to fix span inversion across shared SourceMaps Aug 6, 2026
Copilot AI requested a review from mattcosta7 August 6, 2026 16:38
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.

2 participants