Repository navigation
Fix new-expression emit parentheses after type erasure (#64981) - #65015
Open
timurzvural wants to merge 2 commits into
Open
timurzvural wants to merge 2 commits into
timurzvural wants to merge 2 commits into
Conversation
Parenthesize new-expression operands whose leftmost expression is a call, including after as/satisfies/non-null erasure and through parentheses, so `new (f() as T).C()` emits as `new (f().C)()`. AI assistance disclosure: this change was authored with Cursor Agent.
1 of 2 tasks
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Traversing retained parentheses causes emitConciseBody to double-wrap already-parenthesized object literals, breaking two unchanged arrow-function output baselines.
1 open issue
What changed in this PR
This PR addresses #64981 in the native TypeScript compiler by preserving constructor grouping when type erasure exposes a call expression.
Changes:
- Detects calls along the leftmost constructor-expression chain and adds parentheses during emit.
- Extends shared expression traversal to look through parentheses.
- Adds printer and type-erasure regression tests for property-access constructors.
| File | Description |
|---|---|
tsc/internal/transformers/tstransforms/typeeraser_test.go |
Adds constructor cases covering assertions, satisfies, and non-null expressions. |
tsc/internal/printer/printer.go |
Parenthesizes constructor expressions with leftmost calls. |
tsc/internal/printer/printer_test.go |
Tests printing a constructor accessed from a call result. |
tsc/internal/ast/precedence.go |
Extends leftmost-expression traversal through parentheses. |
Review effort: Balanced
| KindPropertyAccessExpression, | ||
| KindNonNullExpression, | ||
| KindPartiallyEmittedExpression, | ||
| KindParenthesizedExpression, |
Author
There was a problem hiding this comment.
Thanks — addressed in 9391b36.
ast.GetLeftmostExpressionno longer walksKindParenthesizedExpression(shared helper keeps its parentheses boundary soemitConciseBodydoes not double-wrap() => ({...})).- New-expression call detection is printer-local via
newExpressionOperandNeedsParens, which walks parentheses / property-element access / as / satisfies / non-null / partially-emitted only foremitNewExpression. NewExpression6still expectsnew ((f()).C)()because non-null does not elide the original parentheses the wayas/satisfiesdo.- Arrow object-literal baselines remain correct with this approach.
…s in GetLeftmostExpression Address Copilot review on microsoft#65015: do not teach GetLeftmostExpression to traverse ParenthesizedExpression (that double-wrapped arrow concise bodies like `() => ({...})`). Detect leftmost calls for `new` in a printer-local helper that may walk parentheses, preserving emitConciseBody behavior while still emitting `new (f().C)()` after type erasure. AI assistance disclosure: authored with Cursor Agent.
This branch has not been deployed
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.

Summary
Fixes #64981 —
new (f() as T).C()drops required parentheses on emit.new (getPlugins() as any).Linker()was emitted asnew getPlugins().Linker()(i.e.(new getPlugins()).Linker()), causing a runtime TypeError.Change:
emitNewExpression, parenthesize when the leftmost expression (stopping at calls) is aCallExpression.KindParenthesizedExpressioninGetLeftmostExpressionso parentheses / assertion wrappers are handled consistently with classic TS6 emit:new (getPlugins().Linker)().Test plan
TestParenthesizeNew4inprinter_test.goNewExpression3–NewExpression6coveringas/satisfies/ non-nullRelates to
This focused PR replaces the emit portion of the combined #65013 (which is being closed in favor of two single-issue PRs).
AI assistance disclosure
This change was authored with Cursor Agent (an LLM-based coding agent). A human operator directed the investigation at this specific open issue, reviewed the resulting patch, and is submitting it for review. Per CONTRIBUTING.md, AI tool use is disclosed here.