You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The body of a macro is a fully isolated scope. So an import done inside a macro must be checked for usage only inside the macro. And an import done outside the macro must look for usages outside the macro too (finding a usage inside the macro is not valid, as it cannot reference the macro).
However, the current Twig documentation says:
Imported macros are always local to the current template. It means that macros are available in all blocks and other macros defined in the current template, but they are not available in included templates or child templates; you need to explicitly re-import macros in each template.
the foo macro would be accessible from bar macro context, but Twig runtime says that foo macro does not exist.
I think that the devil lies in detail. The Twig documentation says (emphasis is mine):
When calling import or from from a block tag, the imported macros are only defined in the current block and they override macros defined at the template level with the same names.
This probably means that macro imported inside block tag is only defined in the block itself and not in sub-scopes. Or in another words: the visibility of imported macro in block or macro tags depends on whether the import is done in top-level scope or inside block scope. Not very intuitive I would say.
If I import the macro outside the body block, the code works:
Issue #27 says:
However, the current Twig documentation says:
https://twig.symfony.com/doc/3.x/tags/macro.html#macros-scoping
This means that the following code is valid use of
foo
macro:However, the
UnusedMacro
thinks thatfoo
macro is not used and issues the following violation:Additional notes:
I found this comment in the
Scope
class:twigcs/src/Scope/Scope.php
Line 53 in 2634cc2
However this should not be true when looking for usages of macros.
Steps to reproduce
I created a failing test case (that should be passing) here: https://github.com/sustmi/twigcs/blob/6965bad2494b811c54030eed8a7b3682beb8851c/tests/Twig3FunctionalTest.php#L258
git clone https://github.com/sustmi/twigcs/
cd twigcs
git checkout unused-macro-issue-180
composer install
phpunit tests/Twig3FunctionalTest.php
The text was updated successfully, but these errors were encountered: