-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from VincentLanglet/wipTwigSniff
✨ Add PunctuationSpacingSniff
- Loading branch information
Showing
6 changed files
with
118 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,6 @@ | |
|
||
Untouch +-/*%==: | ||
|
||
{{ [1, 2, 3] }} | ||
{{ {'foo': 'bar'} }} | ||
{{ 1 ?: 2 }} | ||
{{ 1 ?? 2 }} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,6 @@ | |
|
||
Untouch +-/*%==: | ||
|
||
{{ [1, 2, 3] }} | ||
{{ {'foo': 'bar'} }} | ||
{{ 1?:2 }} | ||
{{ 1??2 }} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |