Repository navigation
Preserve this in downleveled optional chains wrapped in erased type syntax or used as template tags - #65610
Draft
Ryan Cavanaugh (RyanCavanaugh) wants to merge 3 commits into
Draft
Preserve this in downleveled optional chains wrapped in erased type syntax or used as template tags#65610Ryan Cavanaugh (RyanCavanaugh) wants to merge 3 commits into
this in downleveled optional chains wrapped in erased type syntax or used as template tags#65610Ryan Cavanaugh (RyanCavanaugh) wants to merge 3 commits into
Conversation
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
…ed as template tags Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
this for parenthesized calleesthis in downleveled optional chains wrapped in erased type syntax or used as template tags
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.
When optional chaining is downleveled (target < ES2020), some calls and template tags lose their receiver, so the method runs with
this === undefined. Affected callees:!,as,satisfies,<T>or type arguments;super.fn!andsuper.fn as any, as in(super.fn!)?.();Cause
The type eraser runs first and leaves
PartiallyEmittedExpression(for!/as/satisfies/<T>) andExpressionWithTypeArguments(for instantiation expressions) where the source had parentheses. The optional-chain transform only looked throughParenthesizedExpressionwhen capturingthis, and it did not handle tagged templates at all.Changes (
estransforms/optionalchain.go)visitParenthesizedExpressionbecomesvisitOuterExpression. It looks through parentheses,PartiallyEmittedExpressionandExpressionWithTypeArgumentsand re-wraps the capturedthisreference.OEKAll) now emits.call(thisArg). Previously only bare parentheses did. This also fixes the unparenthesizedobj.gen<string>?.().target.bind(thisArg), which is how the class-fields transform handles private-name tags. Using.bindkeeps the template-strings object as the first argument, so its identity is preserved.Not changed
delete (obj?.x!)and similar wrapped forms keep their current emit. The checker already rejects them with TS2703, and 6.0 emits them the same way.Tests
New compiler test
optionalChainThisArgWithOuterExpressions.ts, run at es2019 and es2020. It covers every shape from the issue plus element access, nested chains andsuper. The first commit adds the test with the old output, so the es2019 baseline diff shows the change; es2020 output is unchanged.thisfor parenthesized callees with!,asor type arguments, and for parenthesized template tags #65597