Skip to content
This repository has been archived by the owner on Nov 11, 2019. It is now read-only.

Commit

Permalink
Added Angular pseudo classes to colon sniff and a way to configure it (
Browse files Browse the repository at this point in the history
…#40)

* Added Angular pseudo classes to colon sniff and a way to configure it

* Codestyle
  • Loading branch information
yannickl88 authored and haroldiedema committed Jun 28, 2018
1 parent 753129c commit 608f565
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
45 changes: 42 additions & 3 deletions src/Sniff/ColonSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,46 @@ final class ColonSniff implements SniffInterface
'extend',
];

/**
* @var string[]
* @see https://angular.io/guide/component-styles
*/
private const PSEUDO_CLASSES_ANGULAR = [
'host',
'host-context',
];

/**
* List of options which can be configured.
*/
private const PSEUDO_CLASS_OPTIONS = [
'css' => self::PSEUDO_CLASSES,
'less' => self::PSEUDO_CLASSES_LESS,
'angular' => self::PSEUDO_CLASSES_ANGULAR,
];

/**
* @var string[]
*/
private $pseudo_classes;

public function __construct(string $pseudo_classes = 'css,less')
{
$this->pseudo_classes = array_merge([], ...array_map(function (string $class) {
$class = trim($class);

if (!isset(self::PSEUDO_CLASS_OPTIONS[$class])) {
throw new \LogicException(sprintf(
'Unknown pseudo classes for "%s", options are: "%s".',
$class,
implode('", "', array_keys(self::PSEUDO_CLASS_OPTIONS))
));
}

return self::PSEUDO_CLASS_OPTIONS[$class];
}, explode(',', $pseudo_classes)));
}

public function register(): array
{
return [
Expand Down Expand Up @@ -102,7 +142,7 @@ public function process(File $file, int $stack_ptr): void
'Colon should be followed by a single space.',
$t->lines[0],
$t->offsets[0],
$t->offsets[0] + strlen($t->chars)
$t->offsets[0] + \strlen($t->chars)
);
}

Expand Down Expand Up @@ -152,7 +192,6 @@ private function isModifier(array $tokens): bool
return false;
}

return $first->type === Token::T_WORD
&& \in_array($first->chars, array_merge(self::PSEUDO_CLASSES, self::PSEUDO_CLASSES_LESS), true);
return $first->type === Token::T_WORD && \in_array($first->chars, $this->pseudo_classes, true);
}
}
1 change: 1 addition & 0 deletions test/Configuration/StandardConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testGetFile()
dirname(__DIR__) . $ds . 'Sniff' . $ds . 'fixtures' . $ds . 'bad_colors.less',
dirname(__DIR__) . $ds . 'Sniff' . $ds . 'fixtures' . $ds . 'bad_variable.less',
dirname(__DIR__) . $ds . 'Sniff' . $ds . 'fixtures' . $ds . 'colons.less',
dirname(__DIR__) . $ds . 'Sniff' . $ds . 'fixtures' . $ds . 'colons_angular.less',
dirname(__DIR__) . $ds . 'Sniff' . $ds . 'fixtures' . $ds . 'color_variants.less',
dirname(__DIR__) . $ds . 'Sniff' . $ds . 'fixtures' . $ds . 'comments.less',
dirname(__DIR__) . $ds . 'Sniff' . $ds . 'fixtures' . $ds . 'curly.less',
Expand Down
39 changes: 39 additions & 0 deletions test/Sniff/ColonSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,43 @@ public function testSniff()
new Violation(ColonSniff::class, 'Colon should be followed by a single space.', 3, 11, 12),
], $file->getViolations());
}

public function testSniffAngular()
{
$file = new File(
'phpunit',
(new Tokenizer())->tokenize(file_get_contents(__DIR__ . '/fixtures/colons_angular.less'))
);

$sniffer = new Sniffer();
$sniffer->loadStandard(SingleStandard::load(ColonSniff::class, ['css,angular']));
$sniffer->process([$file]);

self::assertEquals([], $file->getViolations());
}

public function testSniffAngularWithoutAngularClass()
{
$file = new File(
'phpunit',
(new Tokenizer())->tokenize(file_get_contents(__DIR__ . '/fixtures/colons_angular.less'))
);

$sniffer = new Sniffer();
$sniffer->loadStandard(SingleStandard::load(ColonSniff::class, ['css']));
$sniffer->process([$file]);

self::assertEquals([
new Violation(ColonSniff::class, 'Colon should be followed by a single space.', 1, 1, 2),
], $file->getViolations());
}

public function testSniffBadClass()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Unknown pseudo classes for "foo", options are:');

$sniffer = new Sniffer();
$sniffer->loadStandard(SingleStandard::load(ColonSniff::class, ['foo']));
}
}
8 changes: 6 additions & 2 deletions test/Sniff/SingleStandard.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@

class SingleStandard
{
public static function load(string $sniff): Standard
public static function load(string $sniff, array $args = []): Standard
{
$file = tempnam(sys_get_temp_dir(), 'test_standard_');

file_put_contents($file, sprintf('<csssniffer><sniff class="%s" /></csssniffer>', $sniff));
$sniff_args = array_reduce($args, function (string $carry, string $arg) {
return $carry . '<arg>' . $arg . '</arg>';
}, '');

file_put_contents($file, sprintf('<csssniffer><sniff class="%s">%s</sniff></csssniffer>', $sniff, $sniff_args));

try {
$standard = Standard::loadFromXmlFile($file);
Expand Down
4 changes: 4 additions & 0 deletions test/Sniff/fixtures/colons_angular.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:host-context(.theme-light) h2 {
background-color: #ffffff;
color: #000000;
}

0 comments on commit 608f565

Please sign in to comment.