trivago / prettier-plugin-twig-melody

Code formatting plugin for Prettier which can handle Twig/Melody templates
Apache License 2.0
157 stars 35 forks source link

Error processing inline-if twig statement #37

Open rbrv opened 4 years ago

rbrv commented 4 years ago

Got this error while testing:

Error

[error] templates/shop/products/_item.twig: Error: ERROR: Invalid token
[error]   2 |                         <select name="purchasableId" id="purchasableId" class="purchasableId">
[error]   3 |                             {%- for purchasable in product.getVariants() -%}
[error] > 4 |                                 <option {% if not purchasable.isAvailable %}disabled{% endif %}>
[error]     |                                         ^
[error]   5 |                                     {{ purchasable.description }}
[error]   6 |                                     {{
[error]   7 |                                         purchasable.salePrice|commerceCurrency(
[error] 
[error] A tag must consist of attributes or expressions. Twig Tags are not allowed.
[error]     at TokenStream.error (/mytestproject/node_modules/melody-parser/lib/index.js:1359:22)
[error]     at Parser.error (/mytestproject/node_modules/melody-parser/lib/index.js:830:21)
[error]     at Parser.matchAttributes (/mytestproject/node_modules/melody-parser/lib/index.js:818:22)
[error]     at Parser.matchElement (/mytestproject/node_modules/melody-parser/lib/index.js:726:14)
[error]     at Parser.parse (/mytestproject/node_modules/melody-parser/lib/index.js:627:32)
[error]     at Object.parse (/mytestproject/node_modules/melody-extension-core/lib/index.js:1187:36)
[error]     at Parser.matchTag (/mytestproject/node_modules/melody-parser/lib/index.js:857:29)
[error]     at Parser.parse (/mytestproject/node_modules/melody-parser/lib/index.js:610:32)
[error]     at Parser.matchElement (/mytestproject/node_modules/melody-parser/lib/index.js:736:41)
[error]     at Parser.parse (/mytestproject/node_modules/melody-parser/lib/index.js:627:32)

Source file


                        <select name="purchasableId" id="purchasableId" class="purchasableId">
                            {%- for purchasable in product.getVariants() -%}
                                <option {% if not purchasable.isAvailable %}disabled{% endif %}>
                                    {{ purchasable.description }}
                                    {{
                                        purchasable.salePrice|commerceCurrency(
                                            cart.currency
                                        )
                                    }}
                                </option>
                            {%- endfor -%}
                        </select>

.prettierrc

{
    "tabWidth": 4,
    "plugins": ["./node_modules/prettier-plugin-twig-melody"],
    "twigMultiTags": [
        "nav,endnav",
        "switch,case,default,endswitch",
        "ifchildren,endifchildren",
        "cache,endcache"
    ]
}
twbartel commented 4 years ago

Hi @rbrv, this is a limitation in the Melody parser (separate project). I cannot give an ETA for a fix, but issues like that can be worked around by doing something like

{%- for purchasable in product.getVariants() -%}
    {% set optionDisabled = not purchasable.isAvailable ? 'disabled' : '' %}
    <option {{ optionDisabled }}>
        ...
    </option>
{%- endfor -%}

Does this help you for now?

rbrv commented 4 years ago

I understand. We can change to code as you proposed. Thanks for the follow-up!

eelke commented 4 years ago

Ran into the same issue, confirming that the work around does its job for now.

NeOMakinG commented 4 years ago

Trying to implement it inside the PrestaShop repository for our twig files, since we've a bunch of stuff already done, we can't use a workaround like setting the var before :/

spoeken commented 4 years ago

What would it take to fix this?

Khartir commented 4 years ago

@twbartel Is there an issue open for this in the parser project? I looked for one and couldn't find it. Did I miss it or has no issue been opened yet?

rubas commented 3 years ago

@twbartel Do you have any update on this? This makes porting an existing code base over pretty annoying ...

rubas commented 3 years ago

Update The ternary operator works.

<li class="page-item {{ 'current' in page.class ? ' active' }}">