Skip to content

Commit dcee37d

Browse files
Merge pull request #69 from VincentLanglet/fixDelimiterSpacing
🐛 Fix
2 parents 3c135af + 3a58fc7 commit dcee37d

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public function processStart(int $tokenPosition, array $tokens)
5151
$token = $tokens[$tokenPosition];
5252

5353
// Ignore new line
54-
if ($this->isTokenMatching($tokens[$tokenPosition + 1], Token::EOL_TYPE)) {
54+
$next = $this->findNext(Token::WHITESPACE_TYPE, $tokens, $tokenPosition + 1, true);
55+
if ($this->isTokenMatching($tokens[$next], Token::EOL_TYPE)) {
5556
return;
5657
}
5758

@@ -89,7 +90,8 @@ public function processEnd(int $tokenPosition, array $tokens)
8990
$token = $tokens[$tokenPosition];
9091

9192
// Ignore new line
92-
if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::EOL_TYPE)) {
93+
$previous = $this->findPrevious(Token::WHITESPACE_TYPE, $tokens, $tokenPosition - 1, true);
94+
if ($this->isTokenMatching($tokens[$previous], Token::EOL_TYPE)) {
9395
return;
9496
}
9597

TwigCS/Sniff/AbstractSniff.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,44 @@ public function isTokenMatching(Token $token, int $type, string $value = null)
6161
return $token->getType() === $type && (null === $value || $token->getValue() === $value);
6262
}
6363

64+
/**
65+
* @param int $type
66+
* @param array $tokens
67+
* @param int $start
68+
* @param bool $exclude
69+
*
70+
* @return int
71+
*/
72+
public function findNext(int $type, array $tokens, int $start, bool $exclude = false)
73+
{
74+
$i = 0;
75+
76+
while (isset($tokens[$start + $i]) && $exclude === $this->isTokenMatching($tokens[$start + $i], $type)) {
77+
$i++;
78+
}
79+
80+
return $start + $i;
81+
}
82+
83+
/**
84+
* @param int $type
85+
* @param array $tokens
86+
* @param int $start
87+
* @param bool $exclude
88+
*
89+
* @return int
90+
*/
91+
public function findPrevious(int $type, array $tokens, int $start, bool $exclude = false)
92+
{
93+
$i = 0;
94+
95+
while (isset($tokens[$start - $i]) && $exclude === $this->isTokenMatching($tokens[$start - $i], $type)) {
96+
$i++;
97+
}
98+
99+
return $start - $i;
100+
}
101+
64102
/**
65103
* Adds a violation to the current report for the given token.
66104
*

TwigCS/Tests/Fixtures/DelimiterSpacingTest.fixed.twig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@
1212
{%- if foo -%}{%- endif -%}
1313

1414
{{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }}
15+
16+
<div>
17+
{{
18+
shouldNotCareAboutNewLine
19+
}}
20+
</div>

TwigCS/Tests/Fixtures/DelimiterSpacingTest.twig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@
1212
{%-if foo -%}{%- endif-%}
1313

1414
{{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }}
15+
16+
<div>
17+
{{
18+
shouldNotCareAboutNewLine
19+
}}
20+
</div>

0 commit comments

Comments
 (0)