From 3a58fc74e5f691f5561109832f4d8abbbedfc225 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 3 Jul 2019 13:33:28 +0200 Subject: [PATCH] :bug: Fix --- .../Ruleset/Generic/DelimiterSpacingSniff.php | 6 ++- TwigCS/Sniff/AbstractSniff.php | 38 +++++++++++++++++++ .../Fixtures/DelimiterSpacingTest.fixed.twig | 6 +++ .../Tests/Fixtures/DelimiterSpacingTest.twig | 6 +++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php b/TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php index 605e297..40c9d47 100644 --- a/TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php +++ b/TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php @@ -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; } @@ -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; } diff --git a/TwigCS/Sniff/AbstractSniff.php b/TwigCS/Sniff/AbstractSniff.php index df65a60..3045b2f 100644 --- a/TwigCS/Sniff/AbstractSniff.php +++ b/TwigCS/Sniff/AbstractSniff.php @@ -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. * diff --git a/TwigCS/Tests/Fixtures/DelimiterSpacingTest.fixed.twig b/TwigCS/Tests/Fixtures/DelimiterSpacingTest.fixed.twig index 06d9394..692a539 100644 --- a/TwigCS/Tests/Fixtures/DelimiterSpacingTest.fixed.twig +++ b/TwigCS/Tests/Fixtures/DelimiterSpacingTest.fixed.twig @@ -12,3 +12,9 @@ {%- if foo -%}{%- endif -%} {{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }} + +
+ {{ + shouldNotCareAboutNewLine + }} +
diff --git a/TwigCS/Tests/Fixtures/DelimiterSpacingTest.twig b/TwigCS/Tests/Fixtures/DelimiterSpacingTest.twig index 273ea3d..0175972 100644 --- a/TwigCS/Tests/Fixtures/DelimiterSpacingTest.twig +++ b/TwigCS/Tests/Fixtures/DelimiterSpacingTest.twig @@ -12,3 +12,9 @@ {%-if foo -%}{%- endif-%} {{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }} + +
+ {{ + shouldNotCareAboutNewLine + }} +