Skip to content

Commit

Permalink
Updates the code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolai-shabalin committed Jan 12, 2024
1 parent 500fd83 commit b42ab4d
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions rules/space-between-comments/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
'use strict';
const { is_comment_node } = require('@linthtml/dom-utils');

const hasSpacesAtStartAndEnd = (rule, string) => {
rule = rule === undefined ? 'space' : rule;

if (rule === 'space') {
return string.startsWith(' ') && string.endsWith(' ');
} else if (rule === 'no-space') {
return !string.startsWith(' ') && !string.endsWith(' ');
const rules = {
'space': {
check: (string) => string.startsWith(' ') && string.endsWith(' '),
errorMessage: 'The comment should contain spaces at the beginning and end of the message.'
},
'no-space': {
check: (string) => !string.startsWith(' ') && !string.endsWith(' '),
errorMessage: 'The comment should not contain spaces at the beginning and end of the message.'
}
};

module.exports = {
name: 'htmlacademy/space-between-comments',
// eslint-disable-next-line camelcase
lint(node, rule_config, { report }) {
lint(node, rule_config = 'space', { report }) {
if (is_comment_node(node)) {
const comment = node.data;
const isEdges = hasSpacesAtStartAndEnd(rule_config, comment);

if (!isEdges) {
// eslint-disable-next-line camelcase
const message = rule_config === 'space'
? 'The comment should contain spaces at the beginning and end of the message.'
: 'The comment should not contain spaces at the beginning and end of the message.';
// eslint-disable-next-line camelcase
const { check, errorMessage } = rules[rule_config];

if (!check(comment)) {
report({
position: node.loc,
message
message: errorMessage
});
}
}
Expand Down

0 comments on commit b42ab4d

Please sign in to comment.