Skip to content

Commit

Permalink
Introduce BlockNameSpacing
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Oct 25, 2023
1 parent 5b5ff55 commit 0d79179
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 41 additions & 0 deletions src/Sniff/BlockNameSpacingSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Sniff;

use TwigCsFixer\Token\Token;

/**
* Ensure there is one space before and after block names.
*/
final class BlockNameSpacingSniff extends AbstractSpacingSniff
{
/**
* @param array<int, Token> $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<int, Token> $tokens
*/
protected function getSpaceAfter(int $tokenPosition, array $tokens): ?int
{
$token = $tokens[$tokenPosition];

if ($this->isTokenMatching($token, Token::BLOCK_NAME_TYPE)) {
return 1;
}

return null;
}
}
2 changes: 1 addition & 1 deletion src/Sniff/OperatorSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions src/Standard/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +21,7 @@ public function getSniffs(): array
return [
...(new Twig())->getSniffs(),
new BlankEOFSniff(),
new BlockNameSpacingSniff(),
new EmptyLinesSniff(),
new IndentSniff(),
new TrailingCommaSingleLineSniff(),
Expand Down
2 changes: 1 addition & 1 deletion src/Token/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
8 changes: 4 additions & 4 deletions src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends '@Admin/...' %}
{% set test = 'test' %}
{% if test is not null %}{% endif %}
21 changes: 21 additions & 0 deletions tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Tests\Sniff\BlockNameSpacingSniff;

use TwigCsFixer\Sniff\BlockNameSpacingSniff;
use TwigCsFixer\Tests\Sniff\AbstractSniffTestCase;

final class BlockNameSpacingTest extends AbstractSniffTestCase
{
public function testSniff(): void
{
$this->checkSniff(new BlockNameSpacingSniff(), [
[1 => 5],
[1 => 5],
[3 => 3],
[3 => 3],
]);
}
}
3 changes: 3 additions & 0 deletions tests/Sniff/BlockNameSpacingSniff/BlockNameSpacingTest.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends'@Admin/...' %}
{% set test = 'test' %}
{%if test is not null %}{% endif %}
2 changes: 2 additions & 0 deletions tests/Standard/GenericTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,7 @@ public function testGetSniffs(): void
new OperatorSpacingSniff(),
new PunctuationSpacingSniff(),
new BlankEOFSniff(),
new BlockNameSpacingSniff(),
new EmptyLinesSniff(),
new IndentSniff(),
new TrailingCommaSingleLineSniff(),
Expand Down
24 changes: 12 additions & 12 deletions tests/Token/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -336,15 +336,15 @@ 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,
6 => Token::BLOCK_END_TYPE,
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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0d79179

Please sign in to comment.