&& incorrectly is displayed as a syntax error in Lightning Component
In lightning, you can use an expression boolean function "and" OR the corresponding logical operator:
<aura:if isTrue="{! and(this, that) }">
is functionally equivalent to:
<aura:if isTrue="{! this && that }">
However, the 2nd is listed as a syntax error and breaks things like syntax completion after the ampersands.
Comments (13)
-
reporter -
repo owner Thanks for filing, Alex. Should be a very minor change to the grammar for Lightning expressions. I'll try to work it in very soon.
-
repo owner Alex, when I looked at the grammar it does in fact handle
condition1 && condition2
andcondition1 || condition2
. Then I tried to reproduce the issue and am unable to do so:Can you provide a standalone reproducible source file that demonstrates the issue you're seeing? That will help me get to the bottom of it.
Thanks!
-
reporter Good morning Scott,
I'll try to whip up a reproduction. However, I thought it'd be pretty easy to replicate :)...
One final clarification before i send a snippet: I do not think "&&" (note: w/o the character encodings) is "legal" in a *.cmp file (whereas it's ok in a VF .page). you'd need to use the encoded character entities instead.
So I believe it to NOT be working correctly if this doesn't throw a grammar error since SF won't allow this to be deployed (returning a ParseError: Message: The entity name must immediately follow the '&' in the entity reference.):
<aura:if isTrue="{! this && that}"/>
similarly this shouldn't throw a "grammar error", since SF allows this to be deployed:
<aura:if isTrue="{! this && that}"/>
Also, I've found this documentation: See the "description" column for "&&" logical operator that notes that you need to escape the &'s: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/expr_operators.htm
-
repo owner Ah, okay. Sounds like this is my own mistake. I typed in
&&
but didn't save it to the server. I didn't realize that wasn't valid in Lightning expressions and I assumed that the expansion to&&
was a side-effect of transcribing into Markdown. I should've verified that. Let me go back to the drawing board and see if I can field this a little more accurately this time! -
reporter <aura:component description="ic"> <aura:attribute name="this" type="Boolean" default="false"/> <aura:attribute name="that" type="Boolean" default="false"/> <aura:if isTrue="{! and(this, that) }"> This is valid </aura:if> <aura:if isTrue="{! this && that }"> So is this (however, note the syntax warning on the semi-colons) </aura:if> <!--<aura:if isTrue="{! this && that }">--> <!--Uncommenting this will prevent component from being deployed (but note, if uncommented, no syntax warnings/errors)--> <!--</aura:if>--> </aura:component>
-
repo owner Yup...makes total sense. This is totally my bad. I'll include the fix in a build later today. Thanks!
-
reporter Thank you! And sorry, I should have been clearer in my original issue submission to note that the char entities were "intentional" and provided that add'l info.
-
repo owner Just submitted the fix. It'll be in the next build. I'm going to add a code inspection/intention to 2.0 to highlight Lightning usages of
&&
as an error and auto-correct to&&
for you. Seems useful... -
repo owner - changed version to 1.8.1.1
-
repo owner - changed status to resolved
Delivered in 1.8.1.1.
-
repo owner - changed component to Lightning (Aura)
-
repo owner - changed component to Aura
- Log in to comment
Interestingly,
is not listed as a syntax error, but fails compilation