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

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.

2 participants