trivago / prettier-plugin-twig-melody

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

Prettier-ignore not working #58

Open mrtronje opened 4 years ago

mrtronje commented 4 years ago

If we use a block-statement inside a html-tag the prettier-ignore is not working.

Example to reproduce:

<!doctype html>
{# prettier-ignore-start #} 
<html lang="en" {% block htmlAttributes %}{% endblock %}>
{# prettier-ignore-end #}
    <head></head>
    <body>...</body>
</html>

Output

base.html.twig: Error: ERROR: Invalid token
[error]   1 | <!doctype html>
[error]   2 | {# prettier-ignore-start #}
[error] > 3 | <html lang="en" {% block htmlAttributes %}{% endblock %}>
[error]     |                 ^
[error]   4 | {# prettier-ignore-end #}
[error] 
[error] A tag must consist of attributes or expressions. Twig Tags are not allowed.
einomi commented 1 year ago

After doing some research, I've found out that syntax errors are thrown before {# prettier-ignore #} is parsed.

We use this plugin to format Nunjucks files and had similar issue when tried to pass {% if %} block to an html attribute. The solution is using {% set %} wrapped in html comment (melody parser ignores html comments by default) like this:

<!-- {% set className = "some-class" if someExpression %} -->
<h1 class="{{ className }}">
  Test
</h1>

Maybe want suit for your case though as you're passing {% block %} to the html tag.