Skip to content

🐛 Fix #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function processStart(int $tokenPosition, array $tokens)
$token = $tokens[$tokenPosition];

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

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

// Ignore new line
if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::EOL_TYPE)) {
$previous = $this->findPrevious(Token::WHITESPACE_TYPE, $tokens, $tokenPosition - 1, true);
if ($this->isTokenMatching($tokens[$previous], Token::EOL_TYPE)) {
return;
}

Expand Down
38 changes: 38 additions & 0 deletions TwigCS/Sniff/AbstractSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,44 @@ public function isTokenMatching(Token $token, int $type, string $value = null)
return $token->getType() === $type && (null === $value || $token->getValue() === $value);
}

/**
* @param int $type
* @param array $tokens
* @param int $start
* @param bool $exclude
*
* @return int
*/
public function findNext(int $type, array $tokens, int $start, bool $exclude = false)
{
$i = 0;

while (isset($tokens[$start + $i]) && $exclude === $this->isTokenMatching($tokens[$start + $i], $type)) {
$i++;
}

return $start + $i;
}

/**
* @param int $type
* @param array $tokens
* @param int $start
* @param bool $exclude
*
* @return int
*/
public function findPrevious(int $type, array $tokens, int $start, bool $exclude = false)
{
$i = 0;

while (isset($tokens[$start - $i]) && $exclude === $this->isTokenMatching($tokens[$start - $i], $type)) {
$i++;
}

return $start - $i;
}

/**
* Adds a violation to the current report for the given token.
*
Expand Down
6 changes: 6 additions & 0 deletions TwigCS/Tests/Fixtures/DelimiterSpacingTest.fixed.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@
{%- if foo -%}{%- endif -%}

{{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }}

<div>
{{
shouldNotCareAboutNewLine
}}
</div>
6 changes: 6 additions & 0 deletions TwigCS/Tests/Fixtures/DelimiterSpacingTest.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@
{%-if foo -%}{%- endif-%}

{{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }}

<div>
{{
shouldNotCareAboutNewLine
}}
</div>