Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normative: Support optional chain in LHS of assignment #4

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

nicolo-ribaudo
Copy link
Owner

@nicolo-ribaudo nicolo-ribaudo commented Jun 16, 2023

Motivation: https://gist.github.com/nicolo-ribaudo/d264e424b618e7deaeca1d6e4f16a7c0

Description of the changes:

  1. expr1?.b.c = expr2 is equivalent to expr1 == null ? undefined : (expr1.b.c = expr2)
    • short-circuiting the RHS matches how people currently write that code, i.e. if (expr1) expr1.b.c = expr2;.
  2. expr1?.b.c += expr2 is equivalent to expr1 == null ? undefined : (expr1.b.c += expr2)
  3. expr1?.b.c ??= expr2 is equivalent to expr1 == null ? undefined : (expr1.b.c ??= expr2)
  4. (a?.b) = c throws for nullish a, similarly to how (a?.b).c throws for nullish a.
  5. ++expr1?.b.c, equivalent to expr1 == null ? undefined : ++expr1.c.d, would be very confusing, because the ++ operator can be skipped depending on an optional chain that appears after it. As such, it is still disallowed.
    1. For consistency also expr1?.c.b++ is still disallowed, even if it doesn't have the same problem.
  6. optional chains are still disallowed inside destructuring expressions: in { x: expr1?.b.c } = obj it's not clear if the obj.x getter should be triggered or not.
  7. optional chains are still disallowed in the head of for-in/of loops: for (expr1?.c.d of arr), because it wouldn't short-circuit the evaluation of arr

5.1, 6 and 7 are the points I'm most unsure about, open for discussion in plenary.

@nicolo-ribaudo nicolo-ribaudo force-pushed the optional-chain-assignment branch from c41b669 to f819c07 Compare June 16, 2023 13:23
@nicolo-ribaudo nicolo-ribaudo changed the title Normative: Support optional chain on LHS of assignment Normative: Support optional chain in LHS of assignment Jun 16, 2023
@nicolo-ribaudo nicolo-ribaudo force-pushed the optional-chain-assignment branch from f819c07 to e6a0fb3 Compare June 16, 2023 13:25
@michaelficarra

This comment was marked as resolved.

@nicolo-ribaudo

This comment was marked as resolved.

@michaelficarra

This comment was marked as resolved.

@nicolo-ribaudo

This comment was marked as resolved.

@nicolo-ribaudo

This comment was marked as resolved.

@nicolo-ribaudo nicolo-ribaudo force-pushed the optional-chain-assignment branch from d39ce0f to 9d47145 Compare June 23, 2023 16:50
@michaelficarra

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants