Support optional chaining and nullish coalescing in the CSP evaluator - #4914
Open
m-develops wants to merge 1 commit into
Open
m-develops wants to merge 1 commit into
m-develops wants to merge 1 commit into
Conversation
Adds `?.` and `??` to the CSP build's expression parser, the two operators templates reach for when data may be missing. Both follow JavaScript's semantics. A chain whose base is null or undefined evaluates to undefined as a whole, so `user?.profile.name` does not throw on the second link, and the arguments of a call that short-circuits are not evaluated. `??` only falls through for null and undefined, evaluates its right side lazily, and sits between `||` and the ternary in precedence. Based on the approach of alpinejs#4810 by dasm.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
?.and??are the two operators a template reaches for when data may be missing:user?.name,data?.color ?? ''. The CSP evaluator rejects both, so under the CSP build every such attribute has to be rewritten to(user || {}).nameor moved into a component method.Why now. Livewire's
csp_safemode uses this build, and Filament is working on supporting it. Its templates use these two operators in dozens of attributes, and Dan Harrin asked for them to be supported here rather than rewritten in every template: filamentphp/filament#7032 (reply in thread)What this does. Adds
?.and??with JavaScript's semantics, and nothing beyond a member access, a call or a fallback that the evaluator already allows:nullorundefinedevaluates toundefinedas a whole, souser?.profile.namedoes not throw on the second link, and(user?.profile).namestill does. The arguments of a call that short-circuits are not evaluated.??only falls through fornullandundefined(0,''andfalsestay), evaluates its right side lazily like&&and||do since Fix&&and||not short-circuiting in CSP evaluator #4823, and sits between||and the ternary in precedence.The approach follows #4810 by @dasm, with the whole-chain short-circuit and the lazy evaluation added. Unit tests for all of the above, a Cypress test, and a docs entry.