From b42ab4d42737324cea8a7297f7c7056b0ff344cd Mon Sep 17 00:00:00 2001 From: Nikolai Shabalin Date: Fri, 12 Jan 2024 09:35:56 +0300 Subject: [PATCH] Updates the code structure --- rules/space-between-comments/index.js | 29 ++++++++++++--------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/rules/space-between-comments/index.js b/rules/space-between-comments/index.js index b5949d3..c6f9b4c 100644 --- a/rules/space-between-comments/index.js +++ b/rules/space-between-comments/index.js @@ -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 }); } }