-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: add rule to make sure that raw filter is justified by a comment #5
Open
simonbaese
wants to merge
2
commits into
master
Choose a base branch
from
feature/validate-raw-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Factorial\twigcs; | ||
|
||
use FriendsOfTwig\Twigcs\Lexer; | ||
use FriendsOfTwig\Twigcs\Rule\AbstractRule; | ||
use FriendsOfTwig\Twigcs\Rule\RuleInterface; | ||
use FriendsOfTwig\Twigcs\TwigPort\Token; | ||
use FriendsOfTwig\Twigcs\TwigPort\TokenStream; | ||
|
||
/** | ||
* Custom Twigcs rule to make sure that "raw" filter is justified by a comment. | ||
*/ | ||
class RawFilterRule extends AbstractRule implements RuleInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function check(TokenStream $tokens): array { | ||
|
||
$violations = []; | ||
|
||
while (!$tokens->isEOF()) { | ||
if ($tokens->isEOF()) { | ||
break; | ||
} | ||
$token = $tokens->getCurrent(); | ||
if ($new_violations = $this->validateRawFilterComment($token, $tokens)) { | ||
$violations = array_merge($violations, $new_violations); | ||
} | ||
$tokens->next(); | ||
} | ||
|
||
return $violations; | ||
} | ||
|
||
/** | ||
* Validates that any raw filter is preceded by a comment. | ||
*/ | ||
protected function validateRawFilterComment(Token $token, TokenStream &$tokens): array { | ||
|
||
$violations = []; | ||
|
||
// Consider "|raw" filter. | ||
if ($token->getValue() === 'raw' && | ||
Token::NAME_TYPE === $token->getType() && | ||
Token::PUNCTUATION_TYPE === $tokens->look(Lexer::PREVIOUS_TOKEN)->getType() | ||
) { | ||
|
||
$line = $token->getLine(); | ||
$column = $token->getColumn() + 1; | ||
$reason = 'usage of the "raw" filter should be justified in a preceding comment.'; | ||
|
||
// If the "raw" filter appears on the first line there can not be a | ||
// preceding comment. | ||
if ($line === 1) { | ||
$violations[] = $this->createViolation($tokens->getSourceContext()->getPath(), $line, $column, $reason); | ||
return $violations; | ||
} | ||
|
||
// Look behind until the previous line is reached. | ||
$look_behind = Lexer::PREVIOUS_TOKEN; | ||
while ($previous_token = $tokens->look($look_behind)) { | ||
if ($previous_token->getLine() !== $line) { | ||
break; | ||
} | ||
$look_behind--; | ||
} | ||
|
||
// Check whether the previous line is a comment. | ||
if ($previous_token->getType() !== Token::COMMENT_TYPE) { | ||
$violations[] = $this->createViolation($tokens->getSourceContext()->getPath(), $line, $column, $reason); | ||
} | ||
} | ||
|
||
return $violations; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace FriendsOfTwig\Twigcs\Tests; | ||
|
||
use Factorial\twigcs\TwigCsRuleset; | ||
use FriendsOfTwig\Twigcs\Lexer; | ||
use FriendsOfTwig\Twigcs\Rule\RegEngineRule; | ||
use FriendsOfTwig\Twigcs\TwigPort\Source; | ||
use FriendsOfTwig\Twigcs\Validator\Validator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Provides tests for the custom Twigcs ruleset. | ||
* | ||
* @author D34dMan <shibinkidd@gmail.com> | ||
*/ | ||
class RulesetFunctionalTest extends TestCase { | ||
|
||
/** | ||
* @dataProvider getData | ||
*/ | ||
public function testExpressions($expression, $violationCount): void { | ||
$lexer = new Lexer(); | ||
$validator = new Validator(); | ||
|
||
$violations = $validator->validate(new TwigCsRuleset(2), $lexer->tokenize(new Source($expression, 'src', 'src.html.twig'))); | ||
$this->assertCount(0, $validator->getCollectedData()[RegEngineRule::class]['unrecognized_expressions'] ?? []); | ||
|
||
if ($violationCount) { | ||
$this->assertCount($violationCount, $violations, sprintf("There should be %d violation in:\n %s", $violationCount, $expression)); | ||
} | ||
else { | ||
$this->assertCount(0, $violations, sprintf("There should be no violations in:\n %s", $expression)); | ||
} | ||
} | ||
|
||
/** | ||
* Provides test scenarios. | ||
*/ | ||
public function getData(): array { | ||
return [ | ||
// NOTE: We expect exactly 17 violations in the fail scenario. | ||
// In case we add more, don't forget to update violation count. | ||
[$this->getFailScenarios(), 17], | ||
[$this->getPassScenarios(), 0], | ||
]; | ||
} | ||
|
||
/** | ||
* Gets failure scenarios. | ||
*/ | ||
public function getFailScenarios() { | ||
return file_get_contents(__DIR__ . '/assets/fail.twig'); | ||
} | ||
|
||
/** | ||
* Gets pass scenarios. | ||
*/ | ||
public function getPassScenarios() { | ||
return file_get_contents(__DIR__ . '/assets/pass.twig'); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@simonbaese ,
Do we have a bug due to this aggregation? E.g. false postive and false negative combination from different scenarios that nullify each other when aggregated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so because the lexer goes through the file token by token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am thinking about a scenario where a false positive is reported by the "with" rule and a false negative reported by "raw" rule which would cancel the aggregated count of each other.