Skip to content

Commit

Permalink
feat: report actual column of error
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrsskls committed Jun 17, 2022
1 parent bf46cc5 commit 7eaf12c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/WithOnlyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public function validateWithUsedWithOnly(Token $token, TokenStream &$tokens): ar
{
$violations = [];
if (Token::NAME_TYPE === $token->getType() && $token->getValue() == 'with') {
$column = $token->getColumn() + 1;
if (Token::WHITESPACE_TYPE === $tokens->look(Lexer::NEXT_TOKEN)->getType()) {
if (Token::NAME_TYPE === $tokens->look(2)->getType()) {
if (Token::WHITESPACE_TYPE === $tokens->look(3)->getType() && $tokens->look(4)->getValue() !== 'only') {
$violations[] = $this->createViolation($tokens->getSourceContext()->getPath(), $token->getLine(), '0', '"with" should be used along with "only".');
$violations[] = $this->createViolation($tokens->getSourceContext()->getPath(), $token->getLine(), $column, '"with" should be used along with "only".');
}
}
// If it starts "with" and is "{" count till matching "}"
Expand All @@ -83,7 +84,7 @@ public function validateWithUsedWithOnly(Token $token, TokenStream &$tokens): ar
}
}
if (!($tokens->look($look_ahead_position)->getValue() == 'only' || $tokens->look($look_ahead_position + 1)->getValue() == 'only')) {
$violations[] = $this->createViolation($tokens->getSourceContext()->getPath(), $token->getLine(), '0', '"with" should be used along with "only".');
$violations[] = $this->createViolation($tokens->getSourceContext()->getPath(), $token->getLine(), $column, '"with" should be used along with "only".');
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/WithOnlyRuleFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function testExpressions($expression, $violationCount)
public function getData()
{
return [
// NOTE: We expect exactly 9 violations in the fail scenario.
// NOTE: We expect exactly 15 violations in the fail scenario.
// In case we add more, don't forget to update violation count.
[$this->getFailScenarios(), 9],
[$this->getFailScenarios(), 15],
[$this->getPassScenarios(), 0],
];
}
Expand Down
18 changes: 18 additions & 0 deletions tests/assets/fail.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,22 @@
FAIL: {% include "@elements/icon/icon.twig" with {} %}
FAIL: {% include "@elements/icon/icon.twig" with {with} %}
FAIL: {% include "@elements/icon/icon.twig" with {only} %}
FAIL: {% include "@elements/icon/icon.twig" with {
with: {}
} %}
FAIL: {% include "@elements/icon/icon.twig" with {
only: {}
} %}
FAIL: {% include "@elements/icon/icon.twig" with {
with: {}
} %}
FAIL: {% include "@elements/icon/icon.twig" with {
with: {}
} %}
FAIL: {% include "@elements/icon/icon.twig" with {
with
} %}
FAIL: {% include "@elements/icon/icon.twig" with {
only
} %}
</header>
21 changes: 21 additions & 0 deletions tests/assets/pass.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,25 @@
PASS: {% include "@elements/icon/icon.twig" with {only} only %}
PASS: {% embed "@elements/icon/icon.twig" with {} %}{% endembed %}
PASS: {% embed "@elements/icon/icon.twig" with {foo} %}{% endembed %}
PASS: {% include "@elements/icon/icon.twig" with {
with: {}
} only %}
PASS: {% include "@elements/icon/icon.twig" with {
only: {}
} only %}
PASS: {% include "@elements/icon/icon.twig" with {
with: {}
} only %}
PASS: {% include "@elements/icon/icon.twig" with {
with: {}
} only %}
PASS: {% include "@elements/icon/icon.twig" with {

} only %}
PASS: {% include "@elements/icon/icon.twig" with {
with
} only %}
PASS: {% include "@elements/icon/icon.twig" with {
only
} only %}
</header>

0 comments on commit 7eaf12c

Please sign in to comment.