Support for optional chaining#3547
Conversation
|
Awesome Nils, I love optional chaining. I'll review your PR coming week. |
…or function calls. Added more tests for parsing.
…ests for optional chaining with function nodes.
josdejong
left a comment
There was a problem hiding this comment.
Awesome job Nils, this is a great addition! The code is very readable and the comments here and there are helpful. Thanks for working out the documentation and tests.
I made a couple of inline comments, can you have a look at those?
|
I see some tests are failing on Firefox. Can you have a look at that? See https://github.com/josdejong/mathjs/actions/runs/18316821660/job/52338944661 |
|
I will have a look for the given points at the weekend or next week. I don't like the duplications of the eval funtions either, but I wanted to achieve the best possible eval-performance and don't do any comparisons there, which can be done at compile time. But I will look if there is a better solution and will run some benchmark to check the impact . |
|
Thanks! |
|
@NilsDietrich did you manage to have a look at the last open ends? I'm looking forward to have this PR merged :). Please let me know if you need help. |
AccessorNode-constructor optionalChaining = false as default. Used object == null instead of (object === null || object === undefined)
…es dot-notation. Improved tests of AccessorNode.
…t/optional-chaining
…surable differences.
|
I fixed the things you mentioned. I've also added a benchmark for the AccessorNode. I was unable to measure any performance benefits from having different callback-variants, so I simplified the code to only have the original callbacks and check the optional-chaining paths inside. |
|
Thanks Nils! That sounds good. I'll review your PR next week (time for weekend now 😅). |
|
Thanks @NilsDietrich , all looks good! I'm glad you where able to simplify the code in AccessorNode without performance penalty. |
|
Published now in |
This pull request adds support for optional chaining '.?' in the expression parser. Till now it's hard to securely access paths of objects which may be undefined:
Scope: { a: { b: { c: 3 } } }
Expression: 'result = (a==undefined ? undefined : (a.b==undefined ? undefined: a.b.c ))'
With optional chaining this can be simplified to:
'result = a?.b?.c'
This feature helps to make the expression language more handy (after support for the nullish coalescing operator (??) was added in #3497).
The new functionalty is fully provided by changes to the AccessorNode.