fix: keep parentheses around an optional chain used as a member/call/new target#739
Open
sarathfrancis90 wants to merge 1 commit into
Open
fix: keep parentheses around an optional chain used as a member/call/new target#739sarathfrancis90 wants to merge 1 commit into
sarathfrancis90 wants to merge 1 commit into
Conversation
…new target A ChainExpression shares precedence with MemberExpression, CallExpression and NewExpression, so generating one as the object or callee dropped the parentheses that terminate the optional chain. `(a?.b).c` was emitted as `a?.b.c`, which extends the chain over `.c` and changes the meaning: the parenthesized form throws when `a` is nullish, the unparenthesized form short-circuits to undefined. `new (a?.b)()` was emitted as `new a?.b()`, which is not even valid syntax. Wrap the chain in parentheses when it is the object/callee of a member access, call or new expression.
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.
generatedrops the parentheses that terminate an optional chain when the chain is the object/callee of a member access, call, ornew:(a?.b).canda?.b.caren't equivalent: the parenthesized form ends the chain ata?.b, so.cruns unconditionally and throws whenais nullish; the unparenthesized form extends the chain and short-circuits toundefined.The cause is that
ChainExpressionshares precedence (19) withMemberExpression/CallExpression/NewExpression, so the precedence-only check never parenthesizes it. This wraps the chain when it's the object/callee of those three nodes.a?.b.c(no terminating parens) is unaffected. Added the cases to theobjectsyntax fixture.