Skip to content

Commit

Permalink
Merge pull request #122 from VincentLanglet/wipTwigSniff
Browse files Browse the repository at this point in the history
✨ Add PunctuationSpacingSniff
  • Loading branch information
VincentLanglet authored Apr 26, 2020
2 parents 9b00eab + daf2edb commit fae0a11
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 25 deletions.
32 changes: 20 additions & 12 deletions TwigCS/src/Ruleset/Generic/OperatorSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use TwigCS\Token\Token;

/**
* Ensure there is one space before and after an operator
* Ensure there is one space before and after an operator except for '..'
*/
class OperatorSpacingSniff extends AbstractSpacingSniff
{
Expand All @@ -21,35 +21,43 @@ class OperatorSpacingSniff extends AbstractSpacingSniff
protected function shouldHaveSpaceBefore(int $tokenPosition, array $tokens): ?int
{
$token = $tokens[$tokenPosition];
if (!$this->isTokenMatching($token, Token::OPERATOR_TYPE)) {
return null;
}

$isMinus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '-');
$isPlus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '+');

if ($isMinus || $isPlus) {
if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, ['-', '+'])) {
return $this->isUnary($tokenPosition, $tokens) ? null : 1;
}

return $this->isTokenMatching($token, Token::OPERATOR_TYPE) && '..' !== $token->getValue() ? 1 : null;
if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, '..')) {
return 0;
}

return 1;
}

/**
* @param int $tokenPosition
* @param Token[] $tokens
*
* @return bool
* @return int|null
*/
protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int
{
$token = $tokens[$tokenPosition];
if (!$this->isTokenMatching($token, Token::OPERATOR_TYPE)) {
return null;
}

$isMinus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '-');
$isPlus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '+');

if ($isMinus || $isPlus) {
if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, ['-', '+'])) {
return $this->isUnary($tokenPosition, $tokens) ? 0 : 1;
}

return $this->isTokenMatching($token, Token::OPERATOR_TYPE) && '..' !== $token->getValue() ? 1 : null;
if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, '..')) {
return 0;
}

return 1;
}

/**
Expand Down
50 changes: 50 additions & 0 deletions TwigCS/src/Ruleset/Generic/PunctuationSpacingSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace TwigCS\Ruleset\Generic;

use TwigCS\Sniff\AbstractSpacingSniff;
use TwigCS\Token\Token;

/**
* Ensure there is no space before and after a punctuation except for ':' and ','
*/
class PunctuationSpacingSniff extends AbstractSpacingSniff
{
/**
* @param int $tokenPosition
* @param Token[] $tokens
*
* @return int|null
*/
protected function shouldHaveSpaceBefore(int $tokenPosition, array $tokens): ?int
{
$token = $tokens[$tokenPosition];
if ($this->isTokenMatching($token, Token::PUNCTUATION_TYPE, [')', ']', '}', ':', '.', ',', '|'])) {
return 0;
}

return null;
}

/**
* @param int $tokenPosition
* @param Token[] $tokens
*
* @return int|null
*/
protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int
{
$token = $tokens[$tokenPosition];
if ($this->isTokenMatching($token, Token::PUNCTUATION_TYPE, [':', ','])) {
return 1;
}

if ($this->isTokenMatching($token, Token::PUNCTUATION_TYPE, ['(', '[', '{', '.', '|'])) {
return 0;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

Untouch +-/*%==:

{{ [1, 2, 3] }}
{{ {'foo': 'bar'} }}
{{ 1 ?: 2 }}
{{ 1 ?? 2 }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public function testSniff(): void
[14 => 7],
[15 => 7],
[15 => 7],
[21 => 5],
[21 => 5],
[22 => 5],
[22 => 5],
[24 => 6],
[35 => 10],
[35 => 10],
[19 => 5],
[19 => 5],
[20 => 5],
[20 => 5],
[22 => 6],
[33 => 10],
[33 => 10],
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

Untouch +-/*%==:

{{ [1, 2, 3] }}
{{ {'foo': 'bar'} }}
{{ 1?:2 }}
{{ 1??2 }}

Expand Down
43 changes: 41 additions & 2 deletions docs/twig.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
# Twig Coding Standard Rules

In construction...
From the [official one](http://twig.sensiolabs.org/doc/coding_standards.html).

See [official one](http://twig.sensiolabs.org/doc/coding_standards.html).
### Delimiter spacing

Put one (and only one) space after the start of a delimiter (`{{`, `{%`, and `{#`)
and before the end of a delimiter (`}}`, `%}`, and `#}`).

When using the whitespace control character, do not put any spaces between it and the delimiter

### Operator spacing

Put one (and only one) space before and after the following operators:
comparison operators (`==`, `!=`, `<`, `>`, `>=`, `<=`), math operators (`+`, `-`, `/`, `*`, `%`, `//`, `**`),
logic operators (`not`, `and`, `or`), `~`, `is`, `in`, and the ternary operator (`?:`)

Do not put any spaces before and after the operator `..`.

### Punctuation spacing

Put one (and only one) space after the `:` sign in hashes and `,` in arrays and hashes

Do not put any spaces after an opening parenthesis and before a closing parenthesis in expressions

Do not put any spaces before and after the following operators: `|`, `.`, `[]`

Do not put any spaces before and after the opening and the closing of arrays and hashes

### Todo

Do not put any spaces before and after string delimiters

### Todo

Do not put any spaces before and after the parenthesis used for filter and function calls

### Todo

Use lower cased and underscored variable names

### Todo

Indent your code inside tags (use the same indentation as the one used for the target language of the rendered template)

0 comments on commit fae0a11

Please sign in to comment.