diff --git a/README.md b/README.md index e566f881..96278015 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ Removes any space before and after opening and closing of arrays and hashes. By default, the generic standard is enabled with the twig coding standard rules and the following sniffs: - `BlankEOFSniff`: ensures that files end with one blank line. + - `BlockNameSpacingSniff`: ensure there is one space before and after block names. - `EmptyLinesSniff`: ensures that 2 empty lines do not follow each other. - `IndentSniff`: ensures that files are not indented with tabs. - `TrailingCommaSingleLineSniff`: ensures that single-line arrays, objects and argument lists do not have a trailing comma. diff --git a/src/Sniff/BlockNameSpacingSniff.php b/src/Sniff/BlockNameSpacingSniff.php new file mode 100644 index 00000000..ef71d532 --- /dev/null +++ b/src/Sniff/BlockNameSpacingSniff.php @@ -0,0 +1,41 @@ + $tokens + */ + protected function getSpaceBefore(int $tokenPosition, array $tokens): ?int + { + $token = $tokens[$tokenPosition]; + + if ($this->isTokenMatching($token, Token::BLOCK_NAME_TYPE)) { + return 1; + } + + return null; + } + + /** + * @param array $tokens + */ + protected function getSpaceAfter(int $tokenPosition, array $tokens): ?int + { + $token = $tokens[$tokenPosition]; + + if ($this->isTokenMatching($token, Token::BLOCK_NAME_TYPE)) { + return 1; + } + + return null; + } +} diff --git a/src/Sniff/OperatorSpacingSniff.php b/src/Sniff/OperatorSpacingSniff.php index 6ca3b416..57171e08 100644 --- a/src/Sniff/OperatorSpacingSniff.php +++ b/src/Sniff/OperatorSpacingSniff.php @@ -83,6 +83,6 @@ private function isUnary(int $tokenPosition, array $tokens): bool // {{ 1 + (-2) }} || $this->isTokenMatching($previousToken, Token::PUNCTUATION_TYPE, ['(', '[', ':', ',']) // {% if -2 ... %} - || $this->isTokenMatching($previousToken, Token::BLOCK_TAG_TYPE); + || $this->isTokenMatching($previousToken, Token::BLOCK_NAME_TYPE); } } diff --git a/src/Standard/Generic.php b/src/Standard/Generic.php index 97eedb27..756de0f3 100644 --- a/src/Standard/Generic.php +++ b/src/Standard/Generic.php @@ -5,6 +5,7 @@ namespace TwigCsFixer\Standard; use TwigCsFixer\Sniff\BlankEOFSniff; +use TwigCsFixer\Sniff\BlockNameSpacingSniff; use TwigCsFixer\Sniff\EmptyLinesSniff; use TwigCsFixer\Sniff\IndentSniff; use TwigCsFixer\Sniff\TrailingCommaSingleLineSniff; @@ -20,6 +21,7 @@ public function getSniffs(): array return [ ...(new Twig())->getSniffs(), new BlankEOFSniff(), + new BlockNameSpacingSniff(), new EmptyLinesSniff(), new IndentSniff(), new TrailingCommaSingleLineSniff(), diff --git a/src/Token/Token.php b/src/Token/Token.php index 30b5915e..418d90e9 100644 --- a/src/Token/Token.php +++ b/src/Token/Token.php @@ -28,7 +28,7 @@ final class Token // New constants public const DQ_STRING_START_TYPE = 'DQ_STRING_START_TYPE'; public const DQ_STRING_END_TYPE = 'DQ_STRING_END_TYPE'; - public const BLOCK_TAG_TYPE = 'BLOCK_TAG_TYPE'; + public const BLOCK_NAME_TYPE = 'BLOCK_NAME_TYPE'; public const WHITESPACE_TYPE = 'WHITESPACE_TYPE'; public const TAB_TYPE = 'TAB_TYPE'; public const EOL_TYPE = 'EOL_TYPE'; diff --git a/src/Token/Tokenizer.php b/src/Token/Tokenizer.php index 56d7a89f..56d23f6f 100644 --- a/src/Token/Tokenizer.php +++ b/src/Token/Tokenizer.php @@ -334,7 +334,7 @@ private function lexBlock(): void $this->bracketsAndTernary = []; // To reset ternary $this->pushToken(Token::BLOCK_END_TYPE, $match[0][0]); - $this->isVerbatim = 'verbatim' === $this->getStateParam('tag'); + $this->isVerbatim = 'verbatim' === $this->getStateParam('blockName'); $this->popState(); } else { $this->lexExpression(); @@ -559,9 +559,9 @@ private function lexOperator(string $operator): void private function lexName(string $name): void { - if (self::STATE_BLOCK === $this->getState() && null === $this->getStateParam('tag')) { - $this->pushToken(Token::BLOCK_TAG_TYPE, $name); - $this->setStateParam('tag', $name); + if (self::STATE_BLOCK === $this->getState() && null === $this->getStateParam('blockName')) { + $this->pushToken(Token::BLOCK_NAME_TYPE, $name); + $this->setStateParam('blockName', $name); } else { $this->pushToken(Token::NAME_TYPE, $name); } diff --git a/tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.fixed.twig b/tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.fixed.twig new file mode 100644 index 00000000..c95a7ffb --- /dev/null +++ b/tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.fixed.twig @@ -0,0 +1,3 @@ +{% extends '@Admin/...' %} +{% set test = 'test' %} +{% if test is not null %}{% endif %} diff --git a/tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.php b/tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.php new file mode 100644 index 00000000..35c07d1a --- /dev/null +++ b/tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.php @@ -0,0 +1,21 @@ +checkSniff(new BlockNameSpacingSniff(), [ + [1 => 5], + [1 => 5], + [3 => 3], + [3 => 3], + ]); + } +} diff --git a/tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.twig b/tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.twig new file mode 100644 index 00000000..0c7e35a9 --- /dev/null +++ b/tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.twig @@ -0,0 +1,3 @@ +{% extends'@Admin/...' %} +{% set test = 'test' %} +{%if test is not null %}{% endif %} diff --git a/tests/Standard/GenericTest.php b/tests/Standard/GenericTest.php index b13cc137..57da848c 100644 --- a/tests/Standard/GenericTest.php +++ b/tests/Standard/GenericTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use TwigCsFixer\Sniff\BlankEOFSniff; +use TwigCsFixer\Sniff\BlockNameSpacingSniff; use TwigCsFixer\Sniff\DelimiterSpacingSniff; use TwigCsFixer\Sniff\EmptyLinesSniff; use TwigCsFixer\Sniff\IndentSniff; @@ -26,6 +27,7 @@ public function testGetSniffs(): void new OperatorSpacingSniff(), new PunctuationSpacingSniff(), new BlankEOFSniff(), + new BlockNameSpacingSniff(), new EmptyLinesSniff(), new IndentSniff(), new TrailingCommaSingleLineSniff(), diff --git a/tests/Token/Tokenizer/TokenizerTest.php b/tests/Token/Tokenizer/TokenizerTest.php index 675d46ed..2a098fa4 100644 --- a/tests/Token/Tokenizer/TokenizerTest.php +++ b/tests/Token/Tokenizer/TokenizerTest.php @@ -126,14 +126,14 @@ public static function tokenizeDataProvider(): iterable 11 => Token::EOL_TYPE, 12 => Token::BLOCK_START_TYPE, 13 => Token::WHITESPACE_TYPE, - 14 => Token::BLOCK_TAG_TYPE, + 14 => Token::BLOCK_NAME_TYPE, 15 => Token::WHITESPACE_TYPE, 16 => Token::NAME_TYPE, 17 => Token::WHITESPACE_TYPE, 18 => Token::BLOCK_END_TYPE, 19 => Token::BLOCK_START_TYPE, 20 => Token::WHITESPACE_TYPE, - 21 => Token::BLOCK_TAG_TYPE, + 21 => Token::BLOCK_NAME_TYPE, 22 => Token::WHITESPACE_TYPE, 23 => Token::BLOCK_END_TYPE, 24 => Token::EOL_TYPE, @@ -199,7 +199,7 @@ public static function tokenizeDataProvider(): iterable 52 => Token::EOL_TYPE, 53 => Token::BLOCK_START_TYPE, 54 => Token::WHITESPACE_TYPE, - 55 => Token::BLOCK_TAG_TYPE, + 55 => Token::BLOCK_NAME_TYPE, 56 => Token::WHITESPACE_TYPE, 57 => Token::NAME_TYPE, 58 => Token::OPERATOR_TYPE, @@ -285,7 +285,7 @@ public static function tokenizeDataProvider(): iterable [ 0 => Token::BLOCK_START_TYPE, 1 => Token::WHITESPACE_TYPE, - 2 => Token::BLOCK_TAG_TYPE, + 2 => Token::BLOCK_NAME_TYPE, 3 => Token::WHITESPACE_TYPE, 4 => Token::NAME_TYPE, 5 => Token::WHITESPACE_TYPE, @@ -301,13 +301,13 @@ public static function tokenizeDataProvider(): iterable 15 => Token::BLOCK_END_TYPE, 16 => Token::BLOCK_START_TYPE, 17 => Token::WHITESPACE_TYPE, - 18 => Token::BLOCK_TAG_TYPE, + 18 => Token::BLOCK_NAME_TYPE, 19 => Token::WHITESPACE_TYPE, 20 => Token::BLOCK_END_TYPE, 21 => Token::EOL_TYPE, 22 => Token::BLOCK_START_TYPE, 23 => Token::WHITESPACE_TYPE, - 24 => Token::BLOCK_TAG_TYPE, + 24 => Token::BLOCK_NAME_TYPE, 25 => Token::WHITESPACE_TYPE, 26 => Token::NAME_TYPE, 27 => Token::WHITESPACE_TYPE, @@ -323,7 +323,7 @@ public static function tokenizeDataProvider(): iterable 37 => Token::BLOCK_END_TYPE, 38 => Token::BLOCK_START_TYPE, 39 => Token::WHITESPACE_TYPE, - 40 => Token::BLOCK_TAG_TYPE, + 40 => Token::BLOCK_NAME_TYPE, 41 => Token::WHITESPACE_TYPE, 42 => Token::BLOCK_END_TYPE, 43 => Token::EOL_TYPE, @@ -336,7 +336,7 @@ public static function tokenizeDataProvider(): iterable [ 0 => Token::BLOCK_START_TYPE, 1 => Token::WHITESPACE_TYPE, - 2 => Token::BLOCK_TAG_TYPE, + 2 => Token::BLOCK_NAME_TYPE, 3 => Token::WHITESPACE_TYPE, 4 => Token::NAME_TYPE, 5 => Token::WHITESPACE_TYPE, @@ -344,7 +344,7 @@ public static function tokenizeDataProvider(): iterable 7 => Token::TEXT_TYPE, 8 => Token::BLOCK_START_TYPE, 9 => Token::WHITESPACE_TYPE, - 10 => Token::BLOCK_TAG_TYPE, + 10 => Token::BLOCK_NAME_TYPE, 11 => Token::WHITESPACE_TYPE, 12 => Token::BLOCK_END_TYPE, 13 => Token::EOL_TYPE, @@ -477,7 +477,7 @@ public static function tokenizeDataProvider(): iterable [ 0 => Token::BLOCK_START_TYPE, 1 => Token::WHITESPACE_TYPE, - 2 => Token::BLOCK_TAG_TYPE, + 2 => Token::BLOCK_NAME_TYPE, 3 => Token::WHITESPACE_TYPE, 4 => Token::BLOCK_END_TYPE, 5 => Token::EOL_TYPE, @@ -517,13 +517,13 @@ public static function tokenizeDataProvider(): iterable 39 => Token::EOL_TYPE, 40 => Token::BLOCK_START_TYPE, 41 => Token::WHITESPACE_TYPE, - 42 => Token::BLOCK_TAG_TYPE, + 42 => Token::BLOCK_NAME_TYPE, 43 => Token::WHITESPACE_TYPE, 44 => Token::BLOCK_END_TYPE, 45 => Token::EOL_TYPE, 46 => Token::BLOCK_START_TYPE, 47 => Token::WHITESPACE_TYPE, - 48 => Token::BLOCK_TAG_TYPE, + 48 => Token::BLOCK_NAME_TYPE, 49 => Token::WHITESPACE_TYPE, 50 => Token::NAME_TYPE, 51 => Token::WHITESPACE_TYPE,