Skip to content

Commit

Permalink
Allow to disable every rules
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 9, 2024
1 parent fe14f9d commit 7a94d88
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/Report/ViolationId.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
final class ViolationId
{
public function __construct(
private string $ruleShortName,
private ?string $ruleShortName = null,
private ?string $identifier = null,
private ?string $tokenName = null,
private ?int $line = null,
Expand All @@ -19,13 +19,13 @@ public static function fromString(string $string, ?int $line = null): self
{
$exploded = explode(':', $string);
$name = $exploded[0];
$explodedName = explode('.', $name);
$explodedName = '' !== $name ? explode('.', $name) : null;

$line ??= isset($exploded[1]) && '' !== $exploded[1] ? (int) $exploded[1] : null;
$position = isset($exploded[2]) && '' !== $exploded[2] ? (int) $exploded[2] : null;

return new self(
$explodedName[0],
$explodedName[0] ?? null,
$explodedName[1] ?? null,
$explodedName[2] ?? null,
$line,
Expand All @@ -37,7 +37,7 @@ public function toString(): string
{
$name = rtrim(sprintf(
'%s.%s.%s',
$this->ruleShortName,
$this->ruleShortName ?? '',
$this->identifier ?? '',
$this->tokenName ?? ''
), '.');
Expand Down
21 changes: 15 additions & 6 deletions src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ final class Tokenizer implements TokenizerInterface
private array $expressionStarters = [];

/**
* @var array<array{int<0, 5>, array<string, int|string>}>
* @var array<array{int<0, 5>, array<string, int|string|bool>}>
*/
private array $state = [];

Expand Down Expand Up @@ -216,14 +216,14 @@ private function pushState(int $state, array $data = []): void
*
* @see https://github.com/vimeo/psalm/issues/8989
*/
private function setStateParam(string $name, int|string $value): void
private function setStateParam(string $name, int|string|bool $value): void
{
Assert::notEmpty($this->state, 'Cannot set state params without a current state.');

$this->state[\count($this->state) - 1][1][$name] = $value;
}

private function getStateParam(string $name): int|string|null
private function getStateParam(string $name): int|string|bool|null
{
Assert::notEmpty($this->state, 'Cannot get state params without a current state.');

Expand Down Expand Up @@ -689,24 +689,33 @@ private function getOperatorRegex(Environment $env): string
private function extractIgnoredViolations(string $comment): void
{
$comment = trim($comment);
if (1 === preg_match('/^twig-cs-fixer-disable(|-line|-next-line) ([\s\w,.:]+)/i', $comment, $match)) {
if (1 === preg_match('/^twig-cs-fixer-disable(|-line|-next-line)\s+([\s\w,.:]*)/i', $comment, $match)) {
$this->setStateParam('ignoredViolations', preg_replace('/\s+/', ',', $match[2]) ?? '');
$this->setStateParam('ignoredType', trim($match[1], '-'));
} else {
$this->setStateParam('ignoredViolations', '');
$this->setStateParam('ignoredViolations', false);
}
}

private function processIgnoredViolations(): void
{
$ignoredViolations = $this->getStateParam('ignoredViolations');
if (!\is_string($ignoredViolations)) {
return;
}
$line = match ($this->getStateParam('ignoredType')) {
'line' => (int) $this->getStateParam('startLine'),
'next-line' => $this->line + 1,
default => null,
};

$ignoredViolationsExploded = explode(',', (string) $ignoredViolations);
if ('' === $ignoredViolations) {
$this->ignoredViolations[] = ViolationId::fromString($ignoredViolations, $line);

return;
}

$ignoredViolationsExploded = explode(',', $ignoredViolations);
foreach ($ignoredViolationsExploded as $ignoredViolation) {
if ('' === $ignoredViolation) {
continue;
Expand Down
7 changes: 5 additions & 2 deletions tests/Report/ViolationIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ViolationIdTest extends TestCase
* @dataProvider toStringDataProvider
*/
public function testToString(
string $ruleShortName,
?string $ruleShortName,
?string $identifier,
?string $tokenName,
?int $line,
Expand All @@ -35,10 +35,11 @@ public function testToString(
}

/**
* @return iterable<array-key, array{string, string|null, string|null, int|null, int|null, string}>
* @return iterable<array-key, array{string|null, string|null, string|null, int|null, int|null, string}>
*/
public static function toStringDataProvider(): iterable
{
yield [null, null, null, null, null, ''];
yield ['short', null, null, null, null, 'short'];
yield ['short', 'id', null, null, null, 'short.id'];
yield ['short', null, 'token', null, null, 'short..token'];
Expand All @@ -64,6 +65,8 @@ public function testMatch(string $string1, string $string2, bool $expected): voi
*/
public static function matchDataProvider(): iterable
{
yield ['', 'short', true];
yield ['', 'short.id.token:1:1', true];
yield ['short', 'short', true];
yield ['short', 'short.id.token:1:1', true];
yield ['short.id', 'short.id.token:1:1', true];
Expand Down
1 change: 1 addition & 0 deletions tests/Rules/Fixtures/disable2.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
twig-cs-fixer-disable-line Fake.Error #} This comment disable for the line with "{ #" => ERROR ON THIS LINE
{# twig-cs-fixer-disable-line Fake.Error::1 #} This comment disable for the first token of the line.
{# twig-cs-fixer-disable-line Fake.Error::2 #} This comment disable for the second token of the line. => ERROR ON THIS LINE
{# twig-cs-fixer-disable-line #} This comment disable for the line for every rule
{# twig-cs-fixer-disable-next-line Fake.Error #} This comment disable for the first token of the next-line. => ERROR ON THIS LINE
{# twig-cs-fixer-disable-next-line Fake.Error
#} This comment disable for the next line. => ERROR ON THIS LINE
2 changes: 1 addition & 1 deletion tests/Rules/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function ignoredViolationsDataProvider(): iterable
];
yield [
__DIR__.'/Fixtures/disable2.twig',
[3, 6, 8, 9, 11],
[3, 6, 8, 10, 12],
];
}
}
1 change: 1 addition & 0 deletions tests/Token/Tokenizer/Fixtures/ignored_violations.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
{# Twig-CS-Fixer-disable Foo.Bar.BazInsensitive #}
{# twig-cs-fixer-disable-line Foo.Bar #}
{# twig-cs-fixer-disable-next-line Foo.Bar Bar.Foo #}
{# twig-cs-fixer-disable-next-line #}
1 change: 1 addition & 0 deletions tests/Token/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function testTokenizeIgnoredViolations(): void
'Foo.Bar:3',
'Foo.Bar:5',
'Bar.Foo:5',
':6',
],
array_map(
static fn (ViolationId $validationId) => $validationId->toString(),
Expand Down

0 comments on commit 7a94d88

Please sign in to comment.