Open adrienrn opened 7 years ago
@adrienrn Woa, this is pretty cool! I need to properly sit down to go through this though. :)
Update:
Added some major things (more powerful config, external sniffs) and some minor ones (time / memory metrics, better output).
An example of the twigcs.yml of a project could be:
paths:
- 'Resources/views'
exclude:
- 'node_modules'
- 'Resources/public'
ruleset:
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\DeprecatedTemplateNotationSniff'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\DisallowCommentedCodeSniff'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\DisallowDumpSniff'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\DisallowIncludeTagSniff'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\DisallowTabIndentSniff'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\EnsureBlankAtEOFSniff'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\EnsureHashKeyQuotesSniff'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\EnsureHashSpacingSniff'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\EnsureHashTrailingCommaSniff'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\EnsureQuotesStyleSniff'
options:
style: 'TYPE_SINGLE_QUOTES'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\EnsureTranslationArgumentsSniff'
-
class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\EnsureWhitespaceExpressionSniff'
I also added the possibility to load custom sniffs; using the standardPaths
configuration key.
You can add it to your twigcs.yml
but it makes more sense to create a file ~/.twigcs/twigcs_global
. Everything in that file will be merged with the one from your project.
ruleset: []
standardPaths:
'\Acme\TwigCS':
- '/home/vagrant/work/acme-twigcs/src'
Working on some proper "standard" next to easy things up.
@adrienrn see my comment here https://github.com/asm89/twig-lint/pull/36
you better make your repo https://github.com/adrienrn/twig-lint as a continuation :) then people can merge their PRs in your repo and you will maintain it, if you want
Here's
twigcs
- Twig Code Sniffer - which intend to be a "sniff engine" to check more thoroughly twig templates including whitespaces, trailing commas or missing parameters to functions/filters, etc.If it sounds familiar, it's because it's heavily inspired on PHP_CodeSniffer.
I tried to not change anything to the current
lint
command. Everything should work as it was before.Internals
There's two kind of sniffs:
You define a set of rules (
src/Twig/Lint/Ruleset.php
) with options in atwigcs.yml
file at the root of your twig project.Once loaded, the Linter class (
src/Twig/Lint/Linter.php
) will process every requested files against that set of rules and produce a Report object (src/Twig/Lint/Report.php
) with rule violations, context and statistics.Running
twigcs
against the test fixtures files would produce:Check the many PHPUnit tests I've made to dig further!
Todos
twigcs.yml
:set_error_handler
. The StubbedCore makes it difficult but https://github.com/asm89/twig-lint/pull/24 should make it easier.twigcs.yml
will be a clutter; and it would be nice to have a "standard" syntax like PSR-2 for twig, share by everyone.Why?
In the end, I did it to integrate this to my CI process, using PHPstan or Codacy.
All of my many twig projects are maintained by many people (including freelance developers) and with the activity growing, I don't have time anymore to spend that much time reading pull requests 😔.
I created my own tokenizer / preprocessor out of the official lexer because it was not possible to detect space and all punctuations otherwise, see those issues:
Final notes!
If it's too much, I'll continue this on my fork or create a separate repository. :)