chakra-core / ChakraCore

ChakraCore is an open source Javascript engine with a C API.
MIT License
9.13k stars 1.2k forks source link

Implement optional chaining #6349

Open rhuanjl opened 5 years ago

rhuanjl commented 5 years ago

Optional chaining (https://github.com/tc39/proposal-optional-chaining) is now stage 4 and should be implemented; hopefully not too hard, roughly speaking:

a?.someExpression;
//is sugar for
a ? a.someExpression : undefined;

I may try and do this in the next couple of weeks if no one else is.

rhuanjl commented 4 years ago

It would be great to see this in for 1.12 - though it's a sizeable piece of work and probably lower priority than RegEx or BigInt - probably smaller than those but still non-simple.

I did some initial exploration unfortunately the complexity spiralled further than I thought it would. The dot operator is used in a lot of contexts with slightly different semantics and this matches them in many cases though is different in some with both the optional short cutting feature AND the fact that optional assignment is disallowed.

Nonetheless this should be doable in the parser + bytecode emitter by desugaring the operation into a sequence of existing actions.

Anyone keen to try it? (it should be fun but not easy).

rhuanjl commented 3 years ago

I'm taking another look at this, though parsing it correctly is really not simple - hopefully getting the parsing right will be harder than the actual implementation.

rhuanjl commented 3 years ago

I've got the bulk of an implementation ready - it's based on top of #6545 though so would like to get that merged before I open a PR.

Also not quite finished - has some oddities/specific cases to sort out e.g. if you end an optional chain with .apply() things go wrong AND still need to write tests.