Preserve comments when downleveling arrow expression bodies - #4723
Open
Jake Bailey (jakebailey) with Copilot wants to merge 2 commits into
Open
Preserve comments when downleveling arrow expression bodies#4723Jake Bailey (jakebailey) with Copilot wants to merge 2 commits into
Jake Bailey (jakebailey) with Copilot wants to merge 2 commits into
Conversation
Copilot
AI
changed the title
[WIP] Fix nested nullish coalescing with comments in ES2018 transpilation
Preserve comments when downleveling arrow expression bodies
Jul 23, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Preserves leading comments when arrow expression bodies are downleveled into blocks, preventing ASI from skipping returned expressions.
Changes:
- Copies the expression source range to synthesized
returnstatements. - Adds compiler regression coverage and baselines.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
internal/printer/emitcontext.go |
Preserves the expression range on synthesized returns. |
testdata/tests/cases/compiler/optionalChainingArrowFunctionCommentEs2018.ts |
Adds the regression case. |
testdata/baselines/reference/compiler/optionalChainingArrowFunctionCommentEs2018.js |
Verifies correct comment placement in emitted JavaScript. |
testdata/baselines/reference/compiler/optionalChainingArrowFunctionCommentEs2018.types |
Records type output. |
testdata/baselines/reference/compiler/optionalChainingArrowFunctionCommentEs2018.symbols |
Records symbol output. |
Jake Bailey (jakebailey)
marked this pull request as ready for review
July 23, 2026 21:01
Jake Bailey (jakebailey)
approved these changes
Jul 23, 2026
Wesley Wigham (weswigham)
requested changes
Jul 23, 2026
Member
|
also consider this test case from #4722 (comment) |
Jake Bailey (jakebailey)
force-pushed
the
copilot/fix-esi2018-nested-coalescing
branch
from
August 14, 2026 19:57
04f4c1f to
b0e85fb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4722
Analysis
Downleveling optional chains can introduce temporary variable declarations into an arrow function with an expression body.
VisitFunctionBodythen rewrites the expression body into a block containing those declarations and a synthesizedreturn.The synthesized
returnpreviously had no source range. Leading comments were therefore emitted with the returned expression, after thereturnkeyword:The newline triggers ASI, so the function returns
undefinedwithout evaluating the expression.Fix
Apply the original expression range to both the synthesized
returnand block, matching Strada'sconvertToFunctionBlock. Suppress comment emission on the rewritten expression so comments are owned by the enclosing synthesized statement instead of being emitted afterreturn.The compiler regression covers:
The first commit contains the test and known-bad baselines. The second contains the implementation and corrected JavaScript baseline. Inline comments retain Strada's existing duplicated emit behavior.
Copilot Checklist
I successfully ran these commands at the end of my session, and they completed without error: