Skip to content

Commit 3f14147

Browse files
Adds ignore field
1 parent 3c0dc1b commit 3f14147

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

rules/tag-req-attr/index.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
'use strict';
22
// eslint-disable-next-line camelcase
3-
const { is_tag_node, has_non_empty_attribute, has_attribute } = require('@linthtml/dom-utils');
3+
const { is_tag_node, has_non_empty_attribute, has_attribute, attribute_value } = require('@linthtml/dom-utils');
4+
const checkAttributes = (node, requiredAttributes, report) => {
5+
requiredAttributes.forEach(({ name, allowEmpty, ignore }) => {
6+
allowEmpty = typeof allowEmpty === 'undefined' ? false : allowEmpty;
7+
if (ignore) {
8+
let shouldIgnore = false;
9+
for (const key in ignore) {
10+
if (has_attribute(node, key) && attribute_value(node, key).chars === ignore[key]) {
11+
shouldIgnore = true;
12+
break;
13+
}
14+
}
15+
if (shouldIgnore) {
16+
return;
17+
}
18+
}
19+
20+
if (!has_attribute(node, name) || !has_non_empty_attribute(node, name, allowEmpty)) {
21+
report({
22+
code: 'E057',
23+
position: node.open.loc,
24+
meta: {
25+
data: {
26+
attribute: name,
27+
tag: node.name
28+
}
29+
}
30+
});
31+
}
32+
});
33+
};
34+
435

536
module.exports = {
637
name: 'htmlacademy/tag-req-attr',
@@ -12,23 +43,7 @@ module.exports = {
1243
if (Object.hasOwnProperty.call(rule_config, tagName) && tagName === node.name) { // Ensured property belongs to object
1344
// eslint-disable-next-line camelcase
1445
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-
});
46+
checkAttributes(node, requiredAttributes, report);
3247
}
3348
}
3449
}

0 commit comments

Comments
 (0)