UNPKG

eslint-plugin-comment-length

Version:

An ESLint plugin that provides rules that limit the line length of your comments

84 lines (81 loc) 3.46 kB
import { MessageIds } from '../../const.message-ids.js'; import { isCodeInComment } from '../../utils/is-code-in-comment.js'; import { isCommentInComment } from '../../utils/is-comment-in-comment.js'; import { isLineOverflowing } from '../../utils/is-line-overflowing.js'; import { isCommentOnOwnLine } from '../../utils/is-on-own-line.js'; import { isSemanticComment } from '../../utils/is-semantic-comment.js'; import { SINGLE_LINE_COMMENT_BOILERPLATE_SIZE } from './const.boilerplate-size.js'; import { fixOverflow } from './fix.overflow.js'; import { canBlockBeCompated } from './util.can-block-be-compacted.js'; import { captureNearbyComments } from './util.capture-nearby-comments.js'; import { captureRelevantCommentsIntoBlock } from './util.capture-relevant-comments.js'; function limitSingleLineComments(ruleContext, options, comments) { const sourceCode = ruleContext.sourceCode; const lines = sourceCode.getLines(); for (let i = 0; i < comments.length; i++) { const currentCommentLine = comments[i]; if (!currentCommentLine?.range || !currentCommentLine.value || !isCommentOnOwnLine(sourceCode, currentCommentLine) || isSemanticComment(currentCommentLine, options.semanticComments)) { continue; } const line = lines[currentCommentLine.loc.start.line - 1]; const whitespaceString = line?.split("//")[0] ?? ""; let context = { ...options, whitespace: { string: whitespaceString, size: whitespaceString.split("").reduce( (acc, curr) => acc + (curr === " " ? options.tabSize : 1), 0 ) }, boilerplateSize: SINGLE_LINE_COMMENT_BOILERPLATE_SIZE, comment: { range: currentCommentLine.range, lines: [currentCommentLine.value], value: currentCommentLine.value } }; const currentBlock = captureRelevantCommentsIntoBlock( sourceCode, comments, i, context ); const fixableComment = currentBlock.mergedComment; i += currentBlock.endIndex - currentBlock.startIndex; const nearbyComments = captureNearbyComments(comments, i); const wrappedByBackticks = (nearbyComments?.value.trimStart().startsWith("` ") || nearbyComments?.value.trimStart().startsWith("``")) && nearbyComments?.value.trimEnd().endsWith("`"); if (!fixableComment || wrappedByBackticks || isCommentInComment(fixableComment.value) || isCodeInComment(nearbyComments?.value, context)) { continue; } context = { ...context, comment: { range: fixableComment.range, lines: [fixableComment.value], value: fixableComment.value } }; if (comments.slice(currentBlock.startIndex, currentBlock.endIndex + 1).some((line2) => isLineOverflowing(line2.value, context))) { ruleContext.report({ loc: fixableComment.loc, messageId: MessageIds.EXCEEDS_MAX_LENGTH, data: { maxLength: context.maxLength }, fix: (fixer) => fixOverflow(fixer, fixableComment, context) }); } else if (context.mode === "compact" && canBlockBeCompated(comments, currentBlock, context)) { ruleContext.report({ loc: fixableComment.loc, messageId: MessageIds.CAN_COMPACT, data: { maxLength: context.maxLength }, fix: (fixer) => fixOverflow(fixer, fixableComment, context) }); } } } export { limitSingleLineComments }; //# sourceMappingURL=root.js.map