UNPKG

arui-presets-lint

Version:
44 lines (43 loc) 1.78 kB
import { AST_TOKEN_TYPES } from '@typescript-eslint/utils'; import { DESCRIPTION_REPLACEMENT } from '../constants/index.js'; function formatCommentWithDescription(comment, description) { const commentValue = comment.value.trim(); const baseText = commentValue.replace(DESCRIPTION_REPLACEMENT, '').trim(); return `${baseText} -- ${description}`; } export function fixSingleLineComment(fixer, comment, description) { const newCommentText = formatCommentWithDescription(comment, description); return fixer.replaceText(comment, `// ${newCommentText}`); } export function fixBlockComment(fixer, comment, description) { const commentText = comment.value; const lines = commentText?.split('\n'); if (lines.length === 1) { const newCommentText = formatCommentWithDescription(comment, description); return fixer.replaceText(comment, `/* ${newCommentText} */`); } return null; } export function createSuggestionFix(comment, description) { return (fixer) => { if (comment.type === AST_TOKEN_TYPES.Line) { return fixSingleLineComment(fixer, comment, description); } if (comment.type === AST_TOKEN_TYPES.Block) { return fixBlockComment(fixer, comment, description); } return null; }; } export function createAboveCommentFix(comment, description) { return (fixer) => { const indent = ' '.repeat(comment.loc.start.column); if (comment.type === AST_TOKEN_TYPES.Line) { return fixer.insertTextBefore(comment, `// ${description}\n${indent}`); } if (comment.type === AST_TOKEN_TYPES.Block) { return fixer.insertTextBefore(comment, `/* ${description} */\n${indent}`); } return null; }; }