Repository navigation
Lower wrapped #private destructuring targets and ++/-- operands like bare ones - #65688
Open
Kaique da Silva (ktfth) wants to merge 2 commits into
Open
Kaique da Silva (ktfth) wants to merge 2 commits into
Kaique da Silva (ktfth) wants to merge 2 commits into
Conversation
The es2015 and es2021 outputs read the field with __classPrivateFieldGet in destructuring targets and ++/-- operands that are wrapped in parentheses, `!`, `as`, `satisfies` or a type assertion, which is invalid JS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7
A private name target wrapped in parentheses, `!`, `as`, `satisfies` or a type assertion is still a reference. visitDestructuringAssignmentTarget and visitPreOrPostfixUnaryExpression now skip parentheses and partially emitted expressions before matching, as visitBinaryExpression already does, and visitArrayAssignmentElement lets wrapped elements reach the target lowering. Fixes microsoft#65642 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the wrapped private-member target handling change.
No open issues 🎉
What changed in this PR
Fixes #65642 by making the class-fields transformer lower wrapped private-member destructuring targets and update operands like bare references, while preserving native ES2022 output.
Changes:
- Recognizes parentheses and erased TypeScript wrappers in destructuring targets and
++/--operands. - Adds regression coverage for wrapped targets, static fields, accessors, and side-effecting receivers.
- Records expected output for ES2015, ES2021, and ES2022.
| File | Description |
|---|---|
tsc/internal/transformers/estransforms/classfields.go |
Skips wrappers when matching private assignment targets and update operands. |
tsc/testdata/tests/cases/compiler/privateNameWrappedAssignmentTarget.ts |
Adds regression cases across three compilation targets. |
tsc/testdata/baselines/reference/compiler/privateNameWrappedAssignmentTarget(target=es2015).js |
Records downlevel private-member and object-rest output. |
tsc/testdata/baselines/reference/compiler/privateNameWrappedAssignmentTarget(target=es2021).js |
Records downlevel private-member output with native destructuring. |
tsc/testdata/baselines/reference/compiler/privateNameWrappedAssignmentTarget(target=es2022).js |
Records preserved native private-member syntax. |
Review effort: Balanced
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.
Fixes #65642
This is an automated PR authored by Claude Code using Claude Opus 5.5 with high reasoning level.
Motivation & Guidance
Motivation:
The user reported this issue and asked me to open PRs for their own issues once they were milestoned (Backlog).
Guidance:
The prompt was "Crie os PR's para as issues livres" ("Create the PRs for the free issues", meaning the user's milestoned issues with no PR yet). The user also reminded me to fix at the depth of the root cause: cover every position that shares the cause, not only the reported repro.
Analysis
When
#privatenames are lowered (ES2021 and below), a target wrapped in parentheses,!,as,satisfiesor<T>is still the same reference. By the timeclassfields.goruns, the type eraser has turned!/as/satisfies/<T>intoPartiallyEmittedExpression, so a wrapped target isParenthesizedExpressionand/orPartiallyEmittedExpressionaroundthis.#x.Plain assignment already handles this:
visitBinaryExpressionskips those wrappers before matching ((this.#x) = 1works). Three other matchers did not:visitDestructuringAssignmentTargettestedast.IsPropertyAccessExpression(node)on the unskipped target. Object-property, default, and rest targets ({ a: (this.#x) },[(this.#x) = 7],[...(this.#x)],{ ...(this.#x) }) therefore fell through to the plain visitor and read the field with__classPrivateFieldGet.visitArrayAssignmentElementgated onast.IsArrayBindingOrAssignmentElement, which has no case for parenthesized or partially emitted nodes. A wrapped array element ([(this.#x)],[this.#x!]) never reached the target lowering at all.visitPreOrPostfixUnaryExpressionusedast.SkipParentheses, so(this.#x)++worked but(this.#x as any)++andthis.#x!++did not.The output is invalid JS (
SyntaxError: Invalid destructuring assignment target, orReferenceError: Invalid left-hand sidefor updates).Fix
All three matchers now skip the same wrappers the assignment path skips (
OEKPartiallyEmittedExpressions|OEKParentheses). The skipped target is lowered exactly like the bare one. As in the assignment path, the wrappers are dropped from the lowered target. The private-name branch is taken only whenaccessPrivateIdentifierfinds a transformed name, so output where private names are not lowered (ES2022+) is unchanged.An alternative was to add the wrapper kinds to
ast.IsArrayBindingOrAssignmentElement. I didn't, because that predicate is shared with other transforms.The new test
privateNameWrappedAssignmentTarget.ts(es2015, es2021, es2022) covers:++/--with each wrapper;other().#x).The first commit records the broken output; the second shows the fix. The es2022 baseline does not change.
To check runtime behaviour, a runtime differential compiled 768 variants at es2015 and es2021, ran them in node, and compared the output with native
esnext. The variants cover 16 positions × 8 wrappers × 2 receivers × field/static/accessor. Before the change, every wrapped destructuring and update case diverged. After it, the only divergences left arefor (… of/in …)heads. Those fail bare as well, and #65413 / #65427 cover them.On
main(e0fce98),go test ./internal/testrunner/ ./internal/transformers/...passes, and no other baseline changes.gofmtandgo vet ./internal/transformers/estransforms/are clean.The
++/--operand and array-element changes are identical to the ones in #65687 (for #65640), which touches the same functions forsuper. Either PR merges cleanly after the other.🤖 Generated with Claude Code
https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7