We have no automated tool to make sure Twig template codestyle is correct.
For example https://github.com/PrestaShop/PrestaShop/blob/develop/src/PrestaShopBundle/Resources/views/Admin/Common/recommendedModules.html.twig contains 4-space indents although the devdocs says "HTML, CSS (Sass), Twig and Smarty files MUST follow the Mark Otto鈥檚 coding standards" and Mark Otto coding standard and says
"Use soft tabs with two spaces鈥攖hey're the only way to guarantee code renders the same in any environment."
=> we need a lint tool for templates
Ping @PierreRambaud @eternoendless @sarjon
Should we put smarty templates in the scope too ?
I think no, since these files will be removed in the future.
@PierreRambaud Any recommandation for a tool to do it automatically for us (we might plug it on PrettyCI later btw) ?
Else I found https://squizlabs.github.io/HTML_CodeSniffer/
there's this linter that seems nice: https://github.com/asm89/twig-lint
Unfortunately it shows some files as broken in our twig views when it shouldn't, some legit twig features are not recognized:
https://github.com/asm89/twig-lint/issues/33
I'm sad that it look like we can't create rules on this tool :/
I found this one which seems great:
https://github.com/allocine/twigcs
It allows to make our own rule set, this library mainly cares about twig code formatting:
// vendor/allocine/twigcs/src/Ruleset/Official.php
// We can easily removes the rules we don't need, our create our own:
// (for example keeping the LowerCaseVariable rule would flag a lot of our twig templates)
return [
new Rule\DelimiterSpacing(Violation::SEVERITY_ERROR, 1),
new Rule\ParenthesisSpacing(Violation::SEVERITY_ERROR, 0, 1),
new Rule\ArraySeparatorSpacing(Violation::SEVERITY_ERROR, 0, 1),
new Rule\HashSeparatorSpacing(Violation::SEVERITY_ERROR, 0, 1),
new Rule\OperatorSpacing(Violation::SEVERITY_ERROR, [
'==', '!=', '<', '>', '>=', '<=',
'+', '-', '/', '*', '%', '//', '**',
'not', 'and', 'or',
'~',
'is', 'in'
], 1),
new Rule\PunctuationSpacing(
Violation::SEVERITY_ERROR,
['|', '.', '..', '[', ']'],
0,
new TokenWhitelist([
')',
\Twig_Token::NAME_TYPE,
\Twig_Token::NUMBER_TYPE,
\Twig_Token::STRING_TYPE
], [2])
),
new Rule\TernarySpacing(Violation::SEVERITY_ERROR, 1),
new Rule\LowerCaseVariable(Violation::SEVERITY_ERROR),
new Rule\UnusedVariable(Violation::SEVERITY_WARNING),
new Rule\UnusedMacro(Violation::SEVERITY_WARNING),
new Rule\SliceShorthandSpacing(Violation::SEVERITY_ERROR),
new Rule\TrailingSpace(Violation::SEVERITY_ERROR),
];
And for the indentation issue, there is this TWIG prettifier, which is to be installed as a github application:
https://unibeautify.com/docs/language-twig.html#docsNav
https://github.com/apps/unibeautify-ci
Let's try with this is you want. You can create a pull request here: https://github.com/PrestaShop/php-coding-standards
I tried https://github.com/allocine/twigcs/
Let's upgrade to twig 2 :roll_eyes:
Let's upgrade to twig 2 馃檮
This is currently being discussed in https://github.com/PrestaShop/PrestaShop/pull/13761
We added a Twig check https://github.com/PrestaShop/PrestaShop/pull/17094
but it only checks whether the file is correct. It does not check codestyle.
ping @NeOMakinG
Well I don't know at all myself... if we need to upgrade to twig 2 to get a proper linter, then that's probably the moove we've to do
I started to build https://github.com/matks/php-template-linter because I did not find something suitable for my (our ?) need
@NeOMakinG has tried https://github.com/trivago/prettier-plugin-twig-melody but he has a lot of errors on our Twig code that do look relevant, so another tool that does not fit our needs 馃槩
We should test https://github.com/friendsoftwig/twigcs also!