njs icon indicating copy to clipboard operation
njs copied to clipboard

Support optional chaining

Open dalbani opened this issue 1 year ago • 4 comments

It would be great if njs could support optional chaining.

Looking at the codebase, I actually found this function pertaining to optional chaining: https://github.com/nginx/njs/blob/855aa4c9fac01bd9fbdb1602b523edc00117ff09/src/njs_parser.c#L2962

But, unless I'm doing something silly, it doesn't seem to work as I expected.
For example, a simple call like the following produces an error:

>> Object.xxx?.();
Thrown:
SyntaxError: Unexpected token "(" in shell:1

Whereas the same code in, say, Node.js, Firefox or Chrome will succeed:

undefined

So am I missing something obvious?

dalbani avatar Jan 07 '25 20:01 dalbani

Hi @dalbani,

Consider using QuickJS engine if you do not use built-in modules not implemented yet.

xeioex avatar Jan 07 '25 21:01 xeioex

Thanks for your answer, @xeioex.
I wasn't aware of the existence of the QuickJS alternative. Unfortunately, given that I happen to use ngx.fetch() and querystring in my code, I guess QuickJS is not (yet) an option for me.

But by the way, what does this njs_parser_optional_chain() function do then?

dalbani avatar Jan 07 '25 21:01 dalbani

hi @dalbani,

yes this code was intended to support optional chaining in njs, and this looks like a bug. I will look into it as time permits. No ETA though, as we have QuickJS engine integration as a priority.

xeioex avatar Jan 09 '25 20:01 xeioex

@dalbani since 0.9.1 qjs engine supports ngx.fetch() and querystring module.

xeioex avatar Jul 10 '25 20:07 xeioex