Skip to content

Optional chaining#6973

Open
ShortDevelopment wants to merge 40 commits into
chakra-core:masterfrom
ShortDevelopment:feat-optional-chaining
Open

Optional chaining#6973
ShortDevelopment wants to merge 40 commits into
chakra-core:masterfrom
ShortDevelopment:feat-optional-chaining

Conversation

@ShortDevelopment
Copy link
Copy Markdown
Contributor

@ShortDevelopment ShortDevelopment commented Apr 15, 2024

This PR aims to add support for the stage-4 proposal optional-chaining.
It's inspired by the work of @rhuanjl but uses a more hacky approach to parsing.

Goals

  • Minimize amount of changes
  • (Hopefully) Performance

ToDo

  • Add tests
    • Unused expression result
    • Return expression
    • Root optional-call (e.g. eval?.('a'))
    • Scoped method load (e.g. inside eval)
    • delete
  • implement optional-deletion
  • optional call invoked on eval function should be indirect eval
  • Simple function calls
  • Preserve this
    (a.b)().c should be equivalent to a.b().c
  • short circuit embedded expressions like a?.[++x]
    ++x should not be evaluated if a is null or undefined
  • Don't tokenize a?.3:0 (ternary) as tkOptChain (?.)
  • Tagged templates are not allowed in optional chain
  • fix tmp-renaming for eval result
    eval("foo?.()") or eval("foo?.property")
  • Works with optimizations
    • What about the apply call optimization?
    • Loop inversion (AST cloning)
      Only triggered for 2 nested for loops with assignment
      for (var j = 0; j < 1; ++j) {
      for (var i = 0; i < 1; ++i) {
      1;
      c = 2;
      1;
      }
      };

Out of scope

  • Remove EmitInvoke (seems unused)
  • Better error message for a call on a non-function

Spec: Optional Chains
Fixes #6349

Loading
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.

Implement optional chaining

4 participants