Skip to content

Commit 3c0dc1b

Browse files
Forked rule
1 parent 954dd96 commit 3c0dc1b

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

rules/tag-req-attr/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# htmlacademy/tag-req-attr
2+
3+
Если установлено, указанные атрибуты должны присутствовать в указанном теге.
4+
5+
Форк: https://linthtml.vercel.app/user-guide/rules/list/tag-req-attr
6+
7+
С учетом:
8+
9+
```json
10+
'htmlacademy/tag-req-attr': [
11+
true, {
12+
'input': [
13+
{
14+
name: 'name',
15+
ignore: {
16+
'type': 'hidden'
17+
}
18+
},
19+
],
20+
// Другие элементы...
21+
},
22+
]
23+
```
24+
25+
```json
26+
{
27+
"tag-req-attr": [
28+
true,
29+
{
30+
"img": [
31+
{
32+
"name": "src"
33+
},
34+
{
35+
"name": "alt"
36+
}
37+
]
38+
}
39+
]
40+
}
41+
```
42+
43+
Нарушениями считаются следующие модели:
44+
45+
```html
46+
<img/>
47+
```
48+
49+
```html
50+
<img src="link"/>
51+
```
52+
53+
```html
54+
<img alt="No image">
55+
```
56+
57+
Следующие детали не считаются нарушениями:
58+
59+
```html
60+
<img alt="Picture of a cute cat" src="https://www.google.com/url?sa=i&source=images&cd=&cad=rja&uact=8&ved=2ahUKEwiHzdu5n4ThAhXOxYUKHebmDXoQjRx6BAgBEAU&url=https%3A%2F%2Fimgur.com%2Fgallery%2FHzG2YW8&psig=AOvVaw3w5Zu0oMuDZy83zsfn0NMU&ust=1552742695628256">
61+
```

rules/tag-req-attr/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const { is_tag_node, has_non_empty_attribute, has_attribute } = require('@linthtml/dom-utils');
4+
5+
module.exports = {
6+
name: 'htmlacademy/tag-req-attr',
7+
// eslint-disable-next-line camelcase
8+
lint(node, rule_config, { report }) {
9+
if (is_tag_node(node)) {
10+
// eslint-disable-next-line camelcase
11+
for (const tagName in rule_config) {
12+
if (Object.hasOwnProperty.call(rule_config, tagName) && tagName === node.name) { // Ensured property belongs to object
13+
// eslint-disable-next-line camelcase
14+
const requiredAttributes = rule_config[tagName];
15+
16+
requiredAttributes.forEach(({ name, allowEmpty }) => {
17+
allowEmpty = typeof allowEmpty === 'undefined' ? false : allowEmpty;
18+
19+
if (!has_attribute(node, name) || !has_non_empty_attribute(node, name, allowEmpty)) {
20+
report({
21+
code: 'E057',
22+
position: node.open.loc,
23+
meta: {
24+
data: {
25+
attribute: name,
26+
tag: node.name
27+
}
28+
}
29+
});
30+
}
31+
});
32+
}
33+
}
34+
}
35+
},
36+
};

0 commit comments

Comments
 (0)