-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule: Ensure link are at the bottom of the file (#1542)
- Loading branch information
1 parent
fe70dd2
commit d30f700
Showing
3 changed files
with
167 additions
and
0 deletions.
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
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of DOCtor-RST. | ||
* | ||
* (c) Oskar Stark <oskarstark@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Rule; | ||
|
||
use App\Attribute\Rule\Description; | ||
use App\Rst\RstParser; | ||
use App\Value\Lines; | ||
use App\Value\NullViolation; | ||
use App\Value\RuleGroup; | ||
use App\Value\Violation; | ||
use App\Value\ViolationInterface; | ||
|
||
#[Description('Ensure link lines are at the bottom of the file.')] | ||
class EnsureLinkBottom extends AbstractRule implements LineContentRule | ||
{ | ||
public static function getGroups(): array | ||
{ | ||
return [RuleGroup::Symfony()]; | ||
} | ||
|
||
public function check(Lines $lines, int $number, string $filename): ViolationInterface | ||
{ | ||
$lines->seek($number); | ||
$line = $lines->current(); | ||
|
||
if (!RstParser::isLinkDefinition($line)) { | ||
return NullViolation::create(); | ||
} | ||
|
||
while ($lines->valid()) { | ||
$lines->next(); | ||
|
||
if (!$lines->valid()) { | ||
break; | ||
} | ||
|
||
$current = $lines->current(); | ||
|
||
if ($current->isBlank()) { | ||
continue; | ||
} | ||
|
||
if (!RstParser::isLinkDefinition($current)) { | ||
return Violation::from( | ||
'Please move link definition to the bottom of the page', | ||
$filename, | ||
$number + 1, | ||
$line, | ||
); | ||
} | ||
} | ||
|
||
return NullViolation::create(); | ||
} | ||
} |
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,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of DOCtor-RST. | ||
* | ||
* (c) Oskar Stark <oskarstark@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Tests\Rule; | ||
|
||
use App\Rule\EnsureLinkBottom; | ||
use App\Tests\RstSample; | ||
use App\Tests\UnitTestCase; | ||
use App\Value\NullViolation; | ||
use App\Value\Violation; | ||
use App\Value\ViolationInterface; | ||
|
||
final class EnsureLinkBottomTest extends UnitTestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @dataProvider checkProvider | ||
*/ | ||
public function check(ViolationInterface $expected, RstSample $sample): void | ||
{ | ||
self::assertEquals( | ||
$expected, | ||
(new EnsureLinkBottom())->check($sample->lines, $sample->lineNumber, 'filename'), | ||
); | ||
} | ||
|
||
/** | ||
* @return \Generator<array{0: ViolationInterface, 1: RstSample}> | ||
*/ | ||
public static function checkProvider(): iterable | ||
{ | ||
yield [ | ||
NullViolation::create(), | ||
new RstSample('temp'), | ||
]; | ||
|
||
yield [ | ||
NullViolation::create(), | ||
new RstSample([ | ||
'', | ||
'.. _`first-link`: https://foo.bar', | ||
], 1), | ||
]; | ||
|
||
yield [ | ||
NullViolation::create(), | ||
new RstSample([ | ||
'', | ||
'.. _`first-link`: https://foo.bar', | ||
'.. _`second-link`: https://foo.baz', | ||
], 1), | ||
]; | ||
|
||
yield [ | ||
Violation::from( | ||
'Please move link definition to the bottom of the page', | ||
'filename', | ||
2, | ||
'.. _`first-link`: https://foo.bar', | ||
), | ||
new RstSample([ | ||
'', | ||
'.. _`first-link`: https://foo.bar', | ||
'text after link', | ||
], 1), | ||
]; | ||
|
||
yield [ | ||
Violation::from( | ||
'Please move link definition to the bottom of the page', | ||
'filename', | ||
2, | ||
'.. _`first-link`: https://foo.bar', | ||
), | ||
new RstSample([ | ||
'', | ||
'.. _`first-link`: https://foo.bar', | ||
'', | ||
'text after link', | ||
], 1), | ||
]; | ||
} | ||
} |