&& incorrectly is displayed as a syntax error in Lightning Component

Issue #759 resolved
Alex Jung created an issue

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 &amp;&amp; that }">

However, the 2nd is listed as a syntax error and breaks things like syntax completion after the ampersands.

Comments (13)

  1. Alex Jung reporter

    Interestingly,

    <aura:if isTrue="{! this && that }">
    

    is not listed as a syntax error, but fails compilation

  2. Scott Wells 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.

  3. Scott Wells repo owner

    Alex, when I looked at the grammar it does in fact handle condition1 && condition2 and condition1 || condition2. Then I tried to reproduce the issue and am unable to do so:

    Issue_759.PNG

    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!

  4. Alex Jung 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 &amp;&amp; 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

  5. Scott Wells 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 &amp;&amp; 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!

  6. Alex Jung 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 &amp;&amp; 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>
    
  7. Scott Wells repo owner

    Yup...makes total sense. This is totally my bad. I'll include the fix in a build later today. Thanks!

  8. Alex Jung 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.

  9. Scott Wells 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 &amp;&amp; for you. Seems useful...

  10. Log in to comment