forked from wotnak/twig-cs-fixer-drupal
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRequireComponentAttributesRule.php
More file actions
132 lines (117 loc) · 3.61 KB
/
RequireComponentAttributesRule.php
File metadata and controls
132 lines (117 loc) · 3.61 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
namespace TwigCsFixerDrupal\Rules\Component;
use TwigCsFixer\Rules\AbstractRule;
use TwigCsFixer\Token\Token;
use TwigCsFixer\Token\Tokens;
use TwigCsFixerDrupal\Utils;
/**
* Ensures that main html tag of a component has attributes set using attributes prop.
*/
final class RequireComponentAttributesRule extends AbstractRule {
/**
* {@inheritdoc}
*/
protected function process(int $tokenIndex, Tokens $tokens): void {
// Run this check only once per file.
if (0 !== $tokenIndex) {
return;
}
// Run this check only for SDC templates.
if (!Utils::isInComponentTemplate($tokens->get($tokenIndex))) {
return;
}
// Find the first html tag opening.
$hasHtmlTag = FALSE;
$tokens = $tokens->toArray();
$skip = FALSE;
$inBlockDef = FALSE;
foreach ($tokens as $i => $token) {
// Skip comments.
if ($token->isMatching(Token::COMMENT_START_TYPE)) {
$skip = TRUE;
continue;
}
if ($skip) {
if ($token->isMatching(Token::COMMENT_END_TYPE)) {
$skip = FALSE;
}
continue;
}
// Skip macros.
if ($token->isMatching(Token::BLOCK_START_TYPE)) {
$inBlockDef = TRUE;
}
if ($inBlockDef && $token->isMatching(Token::BLOCK_NAME_TYPE, 'macro')) {
$skip = TRUE;
continue;
}
if ($inBlockDef && $token->isMatching(Token::BLOCK_NAME_TYPE, 'endmacro')) {
$skip = FALSE;
continue;
}
if ($inBlockDef && $token->isMatching(Token::BLOCK_END_TYPE)) {
$inBlockDef = FALSE;
continue;
}
if ($token->isMatching(Token::BLOCK_END_TYPE)) {
$inBlockDef = TRUE;
continue;
}
if ($this->isHtmlTagOpeningStart($token)) {
$tokenIndex = $i;
$hasHtmlTag = TRUE;
break;
}
}
if (!$hasHtmlTag) {
return;
}
// Find tokens of the first html tag opening.
$htmlTagOpeningStart = $tokenIndex;
$htmlTagOpeningEnd = NULL;
$htmlTagOpeningTokens = array_slice($tokens, $htmlTagOpeningStart);
foreach ($htmlTagOpeningTokens as $index => $token) {
if ($this->isHtmlTagOpeningEnd($token, isAlsoOpeningStart: $index === 0)) {
$htmlTagOpeningEnd = $index;
break;
}
}
if ($htmlTagOpeningEnd !== NULL) {
$htmlTagOpeningTokens = array_slice($htmlTagOpeningTokens, 0, $htmlTagOpeningEnd + 1);
}
// Check if html tag has attributes set using attributes prop.
$hasAttributes = FALSE;
foreach ($htmlTagOpeningTokens as $token) {
if ($token->isMatching(Token::NAME_TYPE, "attributes")) {
$hasAttributes = TRUE;
break;
}
}
if (!$hasAttributes) {
$this->addError('Component\'s main html tag must have attributes set using attributes prop.', $tokens[$tokenIndex]);
}
}
/**
* Check if token is a html tag opening.
*/
private function isHtmlTagOpeningStart(Token $token): bool {
return $token->getType() === Token::TEXT_TYPE
&& str_contains($token->getValue(), '<');
}
/**
* Check if token is a html tag opening.
*/
private function isHtmlTagOpeningEnd(Token $token, bool $isAlsoOpeningStart = FALSE): bool {
if ($token->getType() !== Token::TEXT_TYPE) {
return FALSE;
}
$value = $token->getValue();
if (!$isAlsoOpeningStart) {
// If not the same token as opening start then check only part up to the first html tag opening start,
// since tag opening end should always be before next tag opening start.
$value = explode('<', $value)[0];
}
return str_contains($value, '>');
}
}