Skip to content

Commit

Permalink
Don't validate embed tags for with-only rule
Browse files Browse the repository at this point in the history
  • Loading branch information
d34dman committed May 20, 2021
1 parent 0c66bd2 commit 3b5c33f
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/WithOnlyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,42 @@ public function check(TokenStream $tokens) {
$violations = [];

while (!$tokens->isEOF()) {
// We don't check for "with" rule inside "embed" block.
$this->escapeEmbedBlock($tokens);
$token = $tokens->getCurrent();
$violations = array_merge($violations, $this->validateWithUsedWithOnly($token, $tokens));
if ($tokens->isEOF()) {
break;
}
// Check for with rule inside "include" block.
if ($new_violations = $this->validateWithUsedWithOnly($token, $tokens)) {
$violations = array_merge($violations, $new_violations);
}
$tokens->next();
}

return $violations;
}

/**
* @param Token $token
* @param TokenStream $tokens
* @return array
* @throws \FriendsOfTwig\Twigcs\TwigPort\SyntaxError
* Escape embed block.
*/
protected function escapeEmbedBlock(TokenStream &$tokens) {
$token = $tokens->getCurrent();
if (Token::NAME_TYPE === $token->getType() && $token->getValue() == 'embed') {
while (!$tokens->isEOF()) {
$token = $tokens->getCurrent();
if (Token::BLOCK_END_TYPE === $token->getType()) {
break;
}
$tokens->next();
}
}
}

/**
* Validator for with followed by only.
*/
public function validateWithUsedWithOnly(Token $token, TokenStream $tokens): array
public function validateWithUsedWithOnly(Token $token, TokenStream &$tokens): array
{
$violations = [];
if (Token::NAME_TYPE === $token->getType() && $token->getValue() == 'with') {
Expand Down

0 comments on commit 3b5c33f

Please sign in to comment.