|
1 | 1 | 'use strict';
|
2 | 2 | const { is_comment_node } = require('@linthtml/dom-utils');
|
3 | 3 |
|
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.' |
11 | 12 | }
|
12 | 13 | };
|
13 | 14 |
|
14 | 15 | module.exports = {
|
15 | 16 | name: 'htmlacademy/space-between-comments',
|
16 | 17 | // eslint-disable-next-line camelcase
|
17 |
| - lint(node, rule_config, { report }) { |
| 18 | + lint(node, rule_config = 'space', { report }) { |
18 | 19 | if (is_comment_node(node)) {
|
19 | 20 | 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]; |
27 | 23 |
|
| 24 | + if (!check(comment)) { |
28 | 25 | report({
|
29 | 26 | position: node.loc,
|
30 |
| - message |
| 27 | + message: errorMessage |
31 | 28 | });
|
32 | 29 | }
|
33 | 30 | }
|
|
0 commit comments