-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathAvoidIdSniff.php
101 lines (93 loc) · 2.24 KB
/
AvoidIdSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Copyright 2021 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento2\Sniffs\Less;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Class AvoidIdSniff
*
* Ensure that id selector is not used
*
* @link https://devdocs.magento.com/guides/v2.4/coding-standards/code-standard-less.html#types
*/
class AvoidIdSniff implements Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS];
/**
* @var array
*/
private $selectorTokens = [
T_HASH,
T_WHITESPACE,
T_STRING_CONCAT,
T_OPEN_PARENTHESIS,
T_CLOSE_PARENTHESIS,
T_OPEN_SQUARE_BRACKET,
T_CLOSE_SQUARE_BRACKET,
T_DOUBLE_QUOTED_STRING,
T_CONSTANT_ENCAPSED_STRING,
T_DOUBLE_COLON,
T_COLON,
T_EQUAL,
T_MUL_EQUAL,
T_OR_EQUAL,
T_STRING,
T_NONE,
T_DOLLAR,
T_GREATER_THAN,
T_PLUS,
T_NS_SEPARATOR,
T_LNUMBER,
T_BITWISE_NOT
];
/**
* @inheritdoc
*/
public function register()
{
return [T_HASH];
}
/**
* @inheritdoc
*
* Will flag any selector that looks like the following:
* #foo[bar],
* #foo[bar=bash],
* #foo[bar~=bash],
* #foo[bar$=bash],
* #foo[bar*=bash],
* #foo[bar|=bash],
* #foo[bar='bash'],
* #foo:hover,
* #foo:nth-last-of-type(n),
* #foo::before,
* #foo + div,
* #foo > div,
* #foo ~ div,
* #foo\3Abar ~ div,
* #foo\:bar ~ div,
* #foo.bar .baz,
* div#foo {
* blah: 'abc';
* }
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Find the next non-selector token
$nextToken = $phpcsFile->findNext($this->selectorTokens, $stackPtr + 1, null, true);
// Anything except a { or a , means this is not a selector
if ($nextToken !== false && in_array($tokens[$nextToken]['code'], [T_OPEN_CURLY_BRACKET, T_COMMA])) {
$phpcsFile->addError('Id selector is used', $stackPtr, 'IdSelectorUsage');
}
}
}