Skip to content

Conversation

@nathanmarks
Copy link
Contributor

@nathanmarks nathanmarks commented Nov 13, 2025

What

Fixes source locations for VariableDeclarator in the generated AST. Fixes a number of the errors in the snapshot I added yesterday in the source loc validator PR #35109

I'm not entirely sure why, but a side effect of the fix has resulted in a ton of snaps needing updating, with some empty lines no longer present in the generated output. I broke the change up into 2 separate commits. The first commit has the core change and the update to the missing source locations test expectation, and the second commit has the rest of the snapshot updates.

How

  • Add location for variable declarators in ast codegen.
    • We don't actually have the location preserved in HIR, since when we lower the declarations we pass through the location for the VariableDeclaration. Since VariableDeclarator is just a container for each of the assignments, the start of the id and end of the init can be used to accurately reconstruct it when generating the AST.
  • Add source locations for object/array patterns for destructuring assignment source location support

@meta-cla meta-cla bot added the CLA Signed label Nov 13, 2025
@nathanmarks nathanmarks force-pushed the nathanmarks/compiler-fix-var-dec-source-loc branch 2 times, most recently from 3de492d to cd4d9e9 Compare November 13, 2025 13:51
@nathanmarks nathanmarks marked this pull request as ready for review November 13, 2025 14:24
Copy link
Member

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

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

Awesome! One question to see if we can get even more coverage for variable declarations w/o an initial value - see comment - otherwise this looks good to go.

Re the whitespace changes in fixtures: we've seen this pretty consistently as we change source locations. The source location info influences whether Babel's code generator adds a newline or not, and prettier preserves single empty lines between blocks of code. It's fine!

@nathanmarks
Copy link
Contributor Author

Awesome! One question to see if we can get even more coverage for variable declarations w/o an initial value - see comment - otherwise this looks good to go.

I'm glad you asked.... I ran into some stuff looking through this. These declarations are also missing source locations for the VariableDeclarations. I wasn't tracking that before, but if we're wanting to have as much completeness as possible (rather than just surgically fixing things I care about the most for to fix my issues) we might want to address this.

Basically, the declarations hoisted to reactive scopes don't have anywhere for us to track these locations. We only track the location for the identifier. I can fix this by having ReactiveScopeDeclaration track the location for the declaration through the pipeline. What do you think?

@josephsavona
Copy link
Member

Hmm let me look into how onerous that would be

@nathanmarks
Copy link
Contributor Author

I have a commit where I gave it a go. I can push it to another branch when I'm at my laptop for you to have a gander.

@josephsavona
Copy link
Member

Sounds good!

@nathanmarks nathanmarks force-pushed the nathanmarks/compiler-fix-var-dec-source-loc branch from 069738d to 14ea8dd Compare November 17, 2025 16:11
@nathanmarks
Copy link
Contributor Author

nathanmarks commented Nov 17, 2025

@josephsavona see fc833f6 for the changes to the pipeline to address the VariableDeclaration location preservation.

note: the changes to BuildHIR.ts (id.node.loc -> stmt.node.loc) make this consistent with StoreLocal. The id loc is already preserved (pretty sure via place.identifier).

--

separately, I pushed a few commits to this PR just adding the change you had requested (had to rebase as well), along with a number of changes to the test fixture + validator, including checking source location for VariableDeclaration & Identifier nodes so that we don't accidentally regress those as we implement other fixes. I think there's a couple of cases where you could accidentally clobber something else trying to find an appropriate place to pass something through, so thought it was worthwhile to add. And I think a more full picture can help guide more "correct" decisions.

If you'd like to see it all in action, I can add the commit to this branch (or on a separate tmp branch) with the pipeline changes from the commit I linked above, and in the test fixture we would expect to see the new VariableDeclaration errors removed without any other regressions

@nathanmarks
Copy link
Contributor Author

@josephsavona just following up here. Let me know if you want to take more time to think about that commit/approach before doing anything else here, or if you want we could also just merge this PR as-is and continue discussing that after, or <insert some option I didn't think of>.

@nathanmarks
Copy link
Contributor Author

friendly bump here again, I'd love to push on with this!

Copy link
Member

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

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

Nice, this looks great. I think we'll have to wait to merge until we can re-enable CI (currently disabled out an abundance of caution given the npm vulnerability), but makes sense to ship after CI is restored.

@nathanmarks
Copy link
Contributor Author

Nice, this looks great. I think we'll have to wait to merge until we can re-enable CI (currently disabled out an abundance of caution given the npm vulnerability), but makes sense to ship after CI is restored.

Yup. No worries!

Let me know what you want me to do about that other commit. I can open a PR with it to discuss further as a next step.

@nathanmarks nathanmarks force-pushed the nathanmarks/compiler-fix-var-dec-source-loc branch from 14ea8dd to fe927c0 Compare December 8, 2025 15:36
@nathanmarks nathanmarks force-pushed the nathanmarks/compiler-fix-var-dec-source-loc branch from fe927c0 to ee3492c Compare December 8, 2025 16:16
@nathanmarks
Copy link
Contributor Author

nathanmarks commented Dec 8, 2025

@josephsavona

Sorry for the delay, I was on holiday. I just rebased on latest main and had to update a couple of snaps. Good to merge? need you to whack that button!

@nathanmarks
Copy link
Contributor Author

@josephsavona sorry to keep bugging you -- just a reminder to get this merged

cc @poteto if joseph is busy/away and you're ok to merge it!

Copy link
Contributor

@mofeiZ mofeiZ left a comment

Choose a reason for hiding this comment

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

Thanks for the fix, this looks great!

@mofeiZ mofeiZ merged commit d3eb566 into facebook:main Dec 11, 2025
18 checks passed
github-actions bot pushed a commit that referenced this pull request Dec 11, 2025
### What
Fixes source locations for VariableDeclarator in the generated AST.
Fixes a number of the errors in the snapshot I added yesterday in the
source loc validator PR #35109

I'm not entirely sure why, but a side effect of the fix has resulted in
a ton of snaps needing updating, with some empty lines no longer present
in the generated output. I broke the change up into 2 separate commits.
The [first
commit](f4e4dc0)
has the core change and the update to the missing source locations test
expectation, and the [second
commit](cd4d9e9)
has the rest of the snapshot updates.

### How
- Add location for variable declarators in ast codegen.
- We don't actually have the location preserved in HIR, since when we
lower the declarations we pass through the location for the
VariableDeclaration. Since VariableDeclarator is just a container for
each of the assignments, the start of the `id` and end of the `init` can
be used to accurately reconstruct it when generating the AST.
- Add source locations for object/array patterns for destructuring
assignment source location support

DiffTrain build for [d3eb566](d3eb566)
github-actions bot pushed a commit that referenced this pull request Dec 11, 2025
### What
Fixes source locations for VariableDeclarator in the generated AST.
Fixes a number of the errors in the snapshot I added yesterday in the
source loc validator PR #35109

I'm not entirely sure why, but a side effect of the fix has resulted in
a ton of snaps needing updating, with some empty lines no longer present
in the generated output. I broke the change up into 2 separate commits.
The [first
commit](f4e4dc0)
has the core change and the update to the missing source locations test
expectation, and the [second
commit](cd4d9e9)
has the rest of the snapshot updates.

### How
- Add location for variable declarators in ast codegen.
- We don't actually have the location preserved in HIR, since when we
lower the declarations we pass through the location for the
VariableDeclaration. Since VariableDeclarator is just a container for
each of the assignments, the start of the `id` and end of the `init` can
be used to accurately reconstruct it when generating the AST.
- Add source locations for object/array patterns for destructuring
assignment source location support

DiffTrain build for [d3eb566](d3eb566)
sebmarkbage added a commit to sebmarkbage/react that referenced this pull request Dec 11, 2025
@sebmarkbage
Copy link
Collaborator

Had to revert since this broke main and we're publishing security patch.

github-actions bot pushed a commit that referenced this pull request Dec 11, 2025
github-actions bot pushed a commit that referenced this pull request Dec 11, 2025
@nathanmarks
Copy link
Contributor Author

@sebmarkbage it's because it causes a formatting change in lots of snaps, so when i rebase it needs to get merged pretty quick not to cause breakages. I'll put another PR up?

@nathanmarks
Copy link
Contributor Author

put it up again here for whenever we're ready #35348

mofeiZ pushed a commit that referenced this pull request Dec 11, 2025
Putting up #35129 again
Reverted in #35346 after breaking
main before security patch

This change impacts output formatting in a lot of snaps, so is very
sensitive to additions in main to the fixtures resulting in broken tests
after merging, so we should try merge quickly after rebasing or do a
fast follow to the merge with a snap update.
github-actions bot pushed a commit that referenced this pull request Dec 11, 2025
Putting up #35129 again
Reverted in #35346 after breaking
main before security patch

This change impacts output formatting in a lot of snaps, so is very
sensitive to additions in main to the fixtures resulting in broken tests
after merging, so we should try merge quickly after rebasing or do a
fast follow to the merge with a snap update.

DiffTrain build for [b85cf6a](b85cf6a)
github-actions bot pushed a commit that referenced this pull request Dec 11, 2025
Putting up #35129 again
Reverted in #35346 after breaking
main before security patch

This change impacts output formatting in a lot of snaps, so is very
sensitive to additions in main to the fixtures resulting in broken tests
after merging, so we should try merge quickly after rebasing or do a
fast follow to the merge with a snap update.

DiffTrain build for [b85cf6a](b85cf6a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants