UNPKG

arui-presets-lint

Version:
48 lines (47 loc) 2.2 kB
import {} from '@typescript-eslint/utils'; import { MESSAGE_IDS } from '../constants/index.js'; import {} from '../types/index.js'; import { getPreviousComment, isAdjacentComment, isValidDescription, parseDirectiveComment, validateAboveComment, } from './comment-parser.js'; export class CommentValidator { ignoredDirectives; allComments; constructor(ignoredDirectives, allComments) { this.ignoredDirectives = new Set(ignoredDirectives); this.allComments = allComments; } getMeaningfulAboveComment(comment) { const previousComment = getPreviousComment(this.allComments, comment); if (previousComment && isAdjacentComment(previousComment, comment) && !parseDirectiveComment(previousComment) && validateAboveComment(previousComment)) { return previousComment; } return undefined; } validate(comment) { const directive = parseDirectiveComment(comment); if (!directive) { return { needsReport: false, messageId: '' }; } const shouldIgnore = this.ignoredDirectives.has(directive.kind); const hasValidInlineDescription = directive.description ? isValidDescription(directive.description) : false; const hasValidAboveComment = Boolean(this.getMeaningfulAboveComment(comment)); if (shouldIgnore || hasValidInlineDescription || hasValidAboveComment) { return { needsReport: false, messageId: '', directive }; } const aboveComment = getPreviousComment(this.allComments, comment); let messageId = MESSAGE_IDS.missingDescription; const hasInvalidInlineDescription = directive.description && !isValidDescription(directive.description); const hasInvalidAboveComment = aboveComment && isAdjacentComment(aboveComment, comment) && !parseDirectiveComment(aboveComment) && !validateAboveComment(aboveComment); if (hasInvalidInlineDescription || hasInvalidAboveComment) { messageId = MESSAGE_IDS.invalidDescription; } return { needsReport: true, messageId, directive }; } }