#1658 Implement optional chaining

Open
opened 6 months ago by wolfbeast · 2 comments
wolfbeast commented 6 months ago (Migrated from github.com)

Since apparently we must continue to reduce code readability to cater to lazy programmers who want to save a few keystrokes not “having to” check for existing parent properties on nested objects.

Description

The optional chaining operator provides a way to simplify accessing values through connected objects when it’s possible that a reference or function may be undefined or null.

For example, consider an object obj which has a nested structure. Without optional chaining, looking up a deeply-nested subproperty requires validating the references in between, such as:

let nestedProp = obj.first && obj.first.second;

The value of obj.first is confirmed to be non-null (and non-undefined) before then accessing the value of obj.first.second. This prevents the error that would occur if you simply accessed obj.first.second directly without testing obj.first.

With the optional chaining operator (?.), however, you don’t have to explicitly test and short-circuit based on the state of obj.first before trying to access obj.first.second:

let nestedProp = obj.first?.second;

By using the ?. operator instead of just ., JavaScript knows to implicitly check to be sure obj.first is not null or undefined before attempting to access obj.first.second. If obj.first is null or undefined, the expression automatically short-circuits, returning undefined.

https://bugzilla.mozilla.org/show_bug.cgi?id=1566143

Since apparently we must continue to reduce code readability to cater to lazy programmers who want to save a few keystrokes not "having to" check for existing parent properties on nested objects. ### Description The optional chaining operator provides a way to simplify accessing values through connected objects when it's possible that a reference or function may be undefined or null. For example, consider an object obj which has a nested structure. Without optional chaining, looking up a deeply-nested subproperty requires validating the references in between, such as: ```JavaScript let nestedProp = obj.first && obj.first.second; ``` The value of `obj.first` is confirmed to be non-null (and non-undefined) before then accessing the value of `obj.first.second`. This prevents the error that would occur if you simply accessed `obj.first.second` directly without testing `obj.first`. With the optional chaining operator (`?.`), however, you don't have to explicitly test and short-circuit based on the state of `obj.first` before trying to access `obj.first.second`: ```JavaScript let nestedProp = obj.first?.second; ``` By using the `?.` operator instead of just `.`, JavaScript knows to implicitly check to be sure `obj.first` is not null or undefined before attempting to access `obj.first.second`. If `obj.first` is null or undefined, the expression automatically short-circuits, returning undefined. https://bugzilla.mozilla.org/show_bug.cgi?id=1566143
Moonchild added the
Bounty
label 5 months ago
Moonchild commented 5 months ago
Owner

This issue is problematic -- our current JS node parser code simply doesn’t have the capacity to do this kind of complex conditional branching, AFAICT.

This requires to pick apart the frontend node parser and understanding how this parser actually works (also needed for nullish coalescing, most likely) to change this behavior.
It is not possible to rely on Mozilla code for this, since they completely rearchitectured all this in a total mess of refactoring.

Making this bounty material, for what good it may do (since nobody seems interested in actually getting paid for their work on tough nuts?)

This issue is problematic -- our current JS node parser code simply doesn't have the capacity to do this kind of complex conditional branching, AFAICT. This requires to pick apart the frontend node parser and understanding how this parser actually works (also needed for nullish coalescing, most likely) to change this behavior. It is **not** possible to rely on Mozilla code for this, since they completely rearchitectured all this in a total mess of refactoring. Making this bounty material, for what good it may do (since nobody seems interested in actually getting paid for their work on tough nuts?)
Moonchild commented 2 months ago
Owner

If/when implemented, take note of BZ sec bug 1685260

If/when implemented, take note of BZ sec bug 1685260
Moonchild added a new dependency 2 months ago
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Blocks
#1734 Implement ES2020 proposals
MoonchildProductions/UXP
Loading…
There is no content yet.