Skip to content

Commit 21a8de2

Browse files
Fixes code according to eslint rules
1 parent 90feeab commit 21a8de2

File tree

25 files changed

+246
-185
lines changed

25 files changed

+246
-185
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
const fs = require('fs');
24
const path = require('path');
35

rules/a-target-rel/index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
const { is_tag_node, attribute_has_value } = require("@linthtml/dom-utils");
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const { is_tag_node, attribute_has_value } = require('@linthtml/dom-utils');
24
const requiredAttributes = {
35
// todo вынести в конфиг правила
4-
rel: ["noreferrer", "noopener"]
6+
rel: ['noreferrer', 'noopener']
57
};
68

7-
const isEveryValuePresent = (node, attr, values) => {
8-
return values.every(val => attribute_has_value(node, attr, new RegExp(val)))
9-
}
9+
const isEveryValuePresent = (node, attr, values) => values.every((val) => attribute_has_value(node, attr, new RegExp(val)));
1010

11-
const isExternalLink = (node) => {
12-
return is_tag_node(node) && node.name === "a" &&
13-
attribute_has_value(node, 'target', '_blank')
14-
}
11+
const isExternalLink = (node) => is_tag_node(node) && node.name === 'a' &&
12+
attribute_has_value(node, 'target', '_blank');
1513

1614
module.exports = {
17-
name: "htmlacademy/a-target-rel",
15+
name: 'htmlacademy/a-target-rel',
16+
// eslint-disable-next-line camelcase
1817
lint(node, rule_config, { report }) {
1918
if (isExternalLink(node)) {
2019
for (const [attr, values] of Object.entries(requiredAttributes)) {

rules/aria-label-misuse/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
const {
2-
is_tag_node,
3-
has_non_empty_attribute,
4-
} = require("@linthtml/dom-utils");
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const {is_tag_node, has_non_empty_attribute } = require('@linthtml/dom-utils');
54

65
const whitelisted = [
7-
"main", "nav", "table", "td", "th", "aside", "header", "footer", "section", "article", "summary",
6+
'main', 'nav', 'table', 'td', 'th', 'aside', 'header', 'footer', 'section', 'article', 'summary',
87
];
98

109
const interactive = [
11-
"a", "audio", "button", "details", "iframe", "input", "label", "progress", "select", "textarea", "video",
10+
'a', 'audio', 'button', 'details', 'iframe', 'input', 'label', 'progress', 'select', 'textarea', 'video',
1211
];
1312

1413
function isValidUsage(node) {
@@ -22,16 +21,17 @@ function isValidUsage(node) {
2221
}
2322

2423
/* elements with tabindex (implicit interactive) are valid */
25-
return has_non_empty_attribute(node, "tabindex");
24+
return has_non_empty_attribute(node, 'tabindex');
2625
}
2726

2827
module.exports = {
29-
name: "htmlacademy/aria-label-misuse",
28+
name: 'htmlacademy/aria-label-misuse',
29+
// eslint-disable-next-line camelcase
3030
lint(node, rule_config, { report }) {
3131
if (is_tag_node(node) && has_non_empty_attribute(node, 'aria-label') && !isValidUsage(node)) {
3232
report({
3333
position: node.loc,
34-
message: "\"aria-label\" cannot be used on this element",
34+
message: '"aria-label" cannot be used on this element',
3535
});
3636
}
3737
},

rules/attr-delimiter/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
const { is_tag_node } = require("@linthtml/dom-utils");
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const { is_tag_node } = require('@linthtml/dom-utils');
24

35
module.exports = {
4-
name: "htmlacademy/attr-delimiter",
6+
name: 'htmlacademy/attr-delimiter',
7+
// eslint-disable-next-line camelcase
58
lint(node, rule_config, { report }) {
69
if (is_tag_node(node)) {
710
node.attributes.forEach((attribute) => {
811
if (attribute.value?.chars.length > 0 && /\s/.test(attribute.equal.chars)) {
912
report({
1013
position: attribute.loc,
11-
message: "Attribute value must not be delimited by whitespace",
14+
message: 'Attribute value must not be delimited by whitespace',
1215
});
1316
}
14-
})
17+
});
1518
}
1619
}
1720
};

rules/attr-req-value/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
const { is_tag_node, has_non_empty_attribute, is_boolean_attribute} = require("@linthtml/dom-utils");
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const { is_tag_node, has_non_empty_attribute, is_boolean_attribute} = require('@linthtml/dom-utils');
24

35
module.exports = {
4-
name: "htmlacademy/attr-req-value",
6+
name: 'htmlacademy/attr-req-value',
7+
// eslint-disable-next-line camelcase
58
lint(node, rule_config, { report }) {
69
if (is_tag_node(node)) {
710
const attributes = node.attributes.filter(({ name }) => /^¤+$/.test(name.chars) === false);
811
attributes.forEach((attribute) => {
912
const name = attribute.name.chars.toLowerCase();
1013

14+
// eslint-disable-next-line camelcase
1115
if (!has_non_empty_attribute(node, name) && !is_boolean_attribute(attribute) && !rule_config.ignore?.includes(name)) {
1216
report({
13-
code: "E006",
17+
code: 'E006',
1418
position: attribute.loc,
1519
meta: {
1620
data: {
@@ -22,4 +26,4 @@ module.exports = {
2226
});
2327
}
2428
}
25-
}
29+
};
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
const { is_tag_node, has_non_empty_attribute, is_boolean_attribute } = require("@linthtml/dom-utils");
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const { is_tag_node, has_non_empty_attribute, is_boolean_attribute } = require('@linthtml/dom-utils');
24

35
module.exports = {
4-
name: "htmlacademy/attribute-allowed-values",
6+
name: 'htmlacademy/attribute-allowed-values',
7+
// eslint-disable-next-line camelcase
58
lint(node, rule_config, { report }) {
9+
// eslint-disable-next-line camelcase
610
if (is_tag_node(node) && node.name.chars in rule_config) {
11+
// eslint-disable-next-line camelcase
712
const { attributes: restrictedAttributes } = rule_config[node.name.chars];
813
const attributes = node.attributes.filter(
9-
(attribute) => /^¤+$/.test(attribute.name.chars) === false
14+
(attribute) => /^¤+$/.test(attribute.name.chars) === false
1015
&& has_non_empty_attribute(node, attribute.name.chars) && !is_boolean_attribute(attribute)
1116
&& attribute.name.chars in restrictedAttributes
1217
);
1318
attributes.forEach((attribute) => {
1419
const { enum: allowedValues } = restrictedAttributes[attribute.name.chars];
15-
const value = attribute.value.chars.toLowerCase()
20+
const value = attribute.value.chars.toLowerCase();
1621
if (!allowedValues.includes(value)) {
1722
const name = attribute.name.chars;
1823
report({
@@ -23,4 +28,4 @@ module.exports = {
2328
});
2429
}
2530
}
26-
}
31+
};

rules/ban-url-spaces/index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
const { is_tag_node, has_non_empty_attribute } = require("@linthtml/dom-utils");
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const { is_tag_node } = require('@linthtml/dom-utils');
24

35
module.exports = {
4-
name: "htmlacademy/ban-url-spaces",
6+
name: 'htmlacademy/ban-url-spaces',
7+
// eslint-disable-next-line camelcase
58
lint(node, rule_config, { report }) {
69
if (is_tag_node(node)) {
7-
const checkList = rule_config.attributes || ['href', 'src']
10+
// eslint-disable-next-line camelcase
11+
const checkList = rule_config.attributes || ['href', 'src'];
812
const attributes = node.attributes.filter(
9-
({ name }) => checkList.includes(name.chars)
10-
)
13+
({ name }) => checkList.includes(name.chars)
14+
);
1115
if (!attributes.length) {
12-
return
16+
return;
1317
}
1418
attributes.forEach((item) => {
1519
if (/\s/.test(item.value?.chars)) {
1620
report({
1721
position: item.loc,
18-
message: `Spaces in URL not allowed.`
22+
message: 'Spaces in URL not allowed.'
1923
});
2024
}
21-
})
25+
});
2226
}
2327
},
2428
};

rules/charset-position/index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
const dom_utils = require("@linthtml/dom-utils");
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const dom_utils = require('@linthtml/dom-utils');
24

35
module.exports = {
4-
name: "htmlacademy/charset-position",
6+
name: 'htmlacademy/charset-position',
7+
// eslint-disable-next-line camelcase
58
lint(node, rule_config, { report }) {
6-
if (dom_utils.is_tag_node(node) && node.name === "head") {
7-
const childrenWithoutText = node.children.filter(children => children.type !== 'text');
9+
// eslint-disable-next-line camelcase
10+
if (dom_utils.is_tag_node(node) && node.name === 'head') {
11+
const childrenWithoutText = node.children.filter((children) => children.type !== 'text');
812
const firstElement = childrenWithoutText[0];
913
const hasMeta = firstElement.name === 'meta';
10-
const hasUtf = firstElement.attributes.some(attribute => attribute.value.chars.toLowerCase() === 'utf-8');
11-
const hasCharset = firstElement.attributes.some(attribute => attribute.name.chars === "charset");
14+
const hasUtf = firstElement.attributes.some((attribute) => attribute.value.chars.toLowerCase() === 'utf-8');
15+
const hasCharset = firstElement.attributes.some((attribute) => attribute.name.chars === 'charset');
1216

1317
if (!hasMeta && !hasUtf && !hasCharset) {
1418
report({
1519
position: node.loc,
16-
message: `<meta charset=""> is not the first child element in <head>`,
20+
message: '<meta charset=""> is not the first child element in <head>',
1721
});
1822
}
1923
}
2024
}
21-
}
25+
};

rules/class-first/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
const dom_utils = require("@linthtml/dom-utils");
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const dom_utils = require('@linthtml/dom-utils');
24

35
module.exports = {
4-
name: "htmlacademy/class-first",
6+
name: 'htmlacademy/class-first',
7+
// eslint-disable-next-line camelcase
58
lint(node, rule_config, { report }) {
9+
// eslint-disable-next-line camelcase
610
if (dom_utils.is_tag_node(node) === false) {
711
return;
812
}
13+
// eslint-disable-next-line camelcase
914
const attribute = dom_utils.get_attribute(node, 'class');
1015
if (attribute && attribute !== node.attributes[0]) {
1116
report({
1217
position: attribute.loc,
13-
message: `The class attribute should be the first.`,
18+
message: 'The class attribute should be the first.',
1419
});
1520
}
1621
}

rules/form-action-attribute/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
const dom_utils = require("@linthtml/dom-utils");
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const dom_utils = require('@linthtml/dom-utils');
24

35
module.exports = {
4-
name: "htmlacademy/form-action-attribute",
6+
name: 'htmlacademy/form-action-attribute',
7+
// eslint-disable-next-line camelcase
58
lint(node, rule_config, { report }) {
6-
if (dom_utils.is_tag_node(node) && node.name === "form") {
9+
// eslint-disable-next-line camelcase
10+
if (dom_utils.is_tag_node(node) && node.name === 'form') {
711
const actionAttribute = node.attributes.find(
8-
(attr) => attr.name.chars === "action"
12+
(attr) => attr.name.chars === 'action'
913
);
1014

1115
if (!actionAttribute) {
1216
report({
1317
position: node.loc,
14-
message: `The <form> element is missing the "action" attribute.`,
18+
message: 'The <form> element is missing the "action" attribute.',
1519
});
1620
} else if (actionAttribute.value === null) {
1721
report({
1822
position: actionAttribute.loc,
19-
message: `The value of the "action" attribute for the <form> element is empty.`,
23+
message: 'The value of the "action" attribute for the <form> element is empty.',
2024
});
2125
}
2226
}

0 commit comments

Comments
 (0)