asm89 / twig-lint

Standalone twig linter.
MIT License
118 stars 32 forks source link

feature/sniffs_system - twigcs #35

Open adrienrn opened 7 years ago

adrienrn commented 7 years ago

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 a twigcs.yml file at the root of your twig project.

ruleset:
  -
    class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\DumpSniff'
    options:
      severity: 3
  -
    class: 'Asm89\Twig\Lint\Standards\Generic\Sniffs\WhitespaceBeforeAfterExpression'
    # no options means default severity eg. = 5

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:

./bin/twig-lint twigcs --working-dir tests/Asm89/Twig/Lint/Test/Fixtures/config ./tests/Asm89/Twig/Lint/Test/Fixtures/ --exclude hash_4.twig

image

Check the many PHPUnit tests I've made to dig further!

Todos

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. :)

asm89 commented 6 years ago

@adrienrn Woa, this is pretty cool! I need to properly sit down to go through this though. :)

adrienrn commented 6 years ago

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'

image

image

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.

seyfer commented 5 years ago

@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