Skip to content

feat(csp): support optional chaining and nullish coalescing in evaluator#4810

Closed
dasm wants to merge 1 commit intoalpinejs:mainfrom
dasm:feat/csp-optional-chaining-nullish-coalescing
Closed

feat(csp): support optional chaining and nullish coalescing in evaluator#4810
dasm wants to merge 1 commit intoalpinejs:mainfrom
dasm:feat/csp-optional-chaining-nullish-coalescing

Conversation

@dasm
Copy link
Copy Markdown
Contributor

@dasm dasm commented Apr 11, 2026

Add obj?.prop, obj?.[key], fn?.(), and a ?? b to the CSP evaluator, avoiding verbose ternary workarounds in templates. Route all optional paths through the existing checkForDangerousKeywords and checkForDangerousValues guards.

Like && and ||, ?? eagerly evaluates both operands rather than short-circuiting per spec. This is consistent with the existing evaluator pattern.

a?.b.c where a is null will throw rather than short-circuit the entire chain. Use a?.b?.c instead. Fixing this would require an OptionalExpression wrapper node to propagate short-circuit context.

Ref: ECMA-262 11.7: OptionalChainingPunctuator digit lookahead
Ref: ECMA-262 12.3.9: Optional Chains
Ref: ECMA-262 12.13: CoalesceExpression

Add obj?.prop, obj?.[key], fn?.(), and a ?? b to the CSP evaluator,
avoiding verbose ternary workarounds in templates. Route all optional
paths through the existing checkForDangerousKeywords and
checkForDangerousValues guards.

Like && and ||, ?? eagerly evaluates both operands rather than
short-circuiting per spec. This is consistent with the existing
evaluator pattern.

a?.b.c where a is null will throw rather than short-circuit the
entire chain. Use a?.b?.c instead. Fixing this would require an
OptionalExpression wrapper node to propagate short-circuit context.

Ref: ECMA-262 11.7: OptionalChainingPunctuator digit lookahead
Ref: ECMA-262 12.3.9: Optional Chains
Ref: ECMA-262 12.13: CoalesceExpression
@calebporzio
Copy link
Copy Markdown
Collaborator

thanks for PRing these additions to the CSP evaluator. However, I think I want to just wait a bit while we at least just release the property assignment stuff. Then go off demand - as users have real demand for things to be supported we add just that. I don't want to the parser to become an attempt at a full JS parser (at least not right now). thanks!

@dasm
Copy link
Copy Markdown
Contributor Author

dasm commented Apr 11, 2026

Thanks @calebporzio for consideration.
When I worked on my pet project, I noticed it was missing. I considered it would be nice addition. However, I understand that it might be something out of scope for alpine!
Thanks for the project, I was able to simplify a lot of my own code in the app.

@calebporzio
Copy link
Copy Markdown
Collaborator

love to hear it! Yeah...I see the use for it, just want to make totally sure it's universally demanded before expanding the parser to stuff like that. all the best!

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.

2 participants