Skip to content

Commit b42ab4d

Browse files
Updates the code structure
1 parent 500fd83 commit b42ab4d

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

rules/space-between-comments/index.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
'use strict';
22
const { is_comment_node } = require('@linthtml/dom-utils');
33

4-
const hasSpacesAtStartAndEnd = (rule, string) => {
5-
rule = rule === undefined ? 'space' : rule;
6-
7-
if (rule === 'space') {
8-
return string.startsWith(' ') && string.endsWith(' ');
9-
} else if (rule === 'no-space') {
10-
return !string.startsWith(' ') && !string.endsWith(' ');
4+
const rules = {
5+
'space': {
6+
check: (string) => string.startsWith(' ') && string.endsWith(' '),
7+
errorMessage: 'The comment should contain spaces at the beginning and end of the message.'
8+
},
9+
'no-space': {
10+
check: (string) => !string.startsWith(' ') && !string.endsWith(' '),
11+
errorMessage: 'The comment should not contain spaces at the beginning and end of the message.'
1112
}
1213
};
1314

1415
module.exports = {
1516
name: 'htmlacademy/space-between-comments',
1617
// eslint-disable-next-line camelcase
17-
lint(node, rule_config, { report }) {
18+
lint(node, rule_config = 'space', { report }) {
1819
if (is_comment_node(node)) {
1920
const comment = node.data;
20-
const isEdges = hasSpacesAtStartAndEnd(rule_config, comment);
21-
22-
if (!isEdges) {
23-
// eslint-disable-next-line camelcase
24-
const message = rule_config === 'space'
25-
? 'The comment should contain spaces at the beginning and end of the message.'
26-
: 'The comment should not contain spaces at the beginning and end of the message.';
21+
// eslint-disable-next-line camelcase
22+
const { check, errorMessage } = rules[rule_config];
2723

24+
if (!check(comment)) {
2825
report({
2926
position: node.loc,
30-
message
27+
message: errorMessage
3128
});
3229
}
3330
}

0 commit comments

Comments
 (0)