This repository has been archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from hostnet/feature/empty-lines
Added empty line sniffer
- Loading branch information
Showing
4 changed files
with
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @copyright 2017 Hostnet B.V. | ||
*/ | ||
namespace Hostnet\Component\CssSniff\Sniff; | ||
|
||
use Hostnet\Component\CssSniff\File; | ||
use Hostnet\Component\CssSniff\SniffInterface; | ||
use Yannickl88\Component\CSS\Token; | ||
|
||
/** | ||
* Checks that there are not more than two empty lines after each other. Only | ||
* one is allowed. | ||
*/ | ||
final class EmptyLinesSniff implements SniffInterface | ||
{ | ||
private $newline_char; | ||
|
||
public function __construct(string $newline_char = "\n") | ||
{ | ||
$this->newline_char = $newline_char; | ||
} | ||
|
||
public function register(): array | ||
{ | ||
return [ | ||
Token::T_WHITESPACE, | ||
]; | ||
} | ||
|
||
public function process(File $file, int $stack_ptr): void | ||
{ | ||
$token = $file->get($stack_ptr); | ||
|
||
if (substr_count($token->chars, $this->newline_char) > 2) { | ||
$file->addViolation( | ||
'More than two new lines found.', | ||
$token->lines[0] | ||
); | ||
} | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
/** | ||
* @copyright 2017 Hostnet B.V. | ||
*/ | ||
declare(strict_types=1); | ||
namespace Hostnet\Component\CssSniff\Sniff; | ||
|
||
use Hostnet\Component\CssSniff\File; | ||
use Hostnet\Component\CssSniff\Sniffer; | ||
use Hostnet\Component\CssSniff\Violation; | ||
use PHPUnit\Framework\TestCase; | ||
use Yannickl88\Component\CSS\Tokenizer; | ||
|
||
/** | ||
* @covers \Hostnet\Component\CssSniff\Sniff\EmptyLinesSniff | ||
*/ | ||
class EmptyLinesSniffTest extends TestCase | ||
{ | ||
/** | ||
* @var Sniffer | ||
*/ | ||
private $sniffer; | ||
|
||
protected function setUp() | ||
{ | ||
$this->sniffer = new Sniffer(); | ||
$this->sniffer->addSniff(new EmptyLinesSniff()); | ||
} | ||
|
||
public function testSniff() | ||
{ | ||
$file = new File((new Tokenizer())->tokenize(file_get_contents(__DIR__ . '/fixtures/newlines.less'))); | ||
|
||
$this->sniffer->process($file); | ||
|
||
self::assertEquals([ | ||
new Violation('More than two new lines found.', 6), | ||
], $file->getViolations()); | ||
} | ||
} |
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,6 @@ | ||
// one line | ||
|
||
// two lines | ||
|
||
|
||
// end |