VincentLanglet / Twig-CS-Fixer

A tool to automatically fix Twig Coding Standards issues
MIT License
202 stars 18 forks source link

Make it possible to turn off `OptionalDependencies` in `StubbedEnvironment` #246

Open ruudk opened 6 days ago

ruudk commented 6 days ago

First of all, thanks for this amazing tool! 👏

In our Symfony project we use a custom translation mechanism. We don't want people to use the default trans blocks and functions.

But these are automatically added because the classes exist:

https://github.com/VincentLanglet/Twig-CS-Fixer/blob/1f1bcc5b7a608c64603e8d3a879508316bfc9919/src/Environment/StubbedEnvironment.php#L128-L133

It would be great if I could turn off this behavior. What would be the best way?

Maybe introduce an option on the Config that's enabled by default, that we can disable?

Then I can manually add all the required token parsers myself.

I could create the PR if you tell me how you want it to be done 😊

VincentLanglet commented 6 days ago

In our Symfony project we use a custom translation mechanism. We don't want people to use the default trans blocks and functions. But these are automatically added because the classes exist:

Hi, I'm not sure about your use case.

If the classes exists, it seems natural that the linter are using them, since the twig/html is valid.

Shouldn't the job of

For the special rule, trans block will be a Token::BLOCK_NAME_TYPE and function will be Token::NAME_TYPE.

ruudk commented 2 days ago

Interesting.

It would be great if I could create a rule for Twig CS Fixer that produces a non fixable error when using a forbidden block/function/filter. We could create a generic disallow rule that you can configure yourself. WDYT?

VincentLanglet commented 2 days ago

It should be possible. I'm interested.

Maybe the best would be to have three rules: Forbiddenblockrule Forbiddenfunctionrule Forbiddenfilterrule

VincentLanglet commented 1 day ago

The ForbiddenBlockRule will be easier since there is a special token "block name" for this.

But for function and filter, it could be interesting to improve the Tokenizer/Lexer because the "Token::Name_Type" is used for at least variable, function and filter...