UNPKG

eslint-plugin-comment-length

Version:

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

86 lines (82 loc) 3.74 kB
'use strict'; var const_messageIds = require('../../const.message-ids.cjs'); var isCodeInComment = require('../../utils/is-code-in-comment.cjs'); var isCommentInComment = require('../../utils/is-comment-in-comment.cjs'); var isLineOverflowing = require('../../utils/is-line-overflowing.cjs'); var isOnOwnLine = require('../../utils/is-on-own-line.cjs'); var isSemanticComment = require('../../utils/is-semantic-comment.cjs'); var const_boilerplateSize = require('./const.boilerplate-size.cjs'); var fix_overflow = require('./fix.overflow.cjs'); var util_canBlockBeCompacted = require('./util.can-block-be-compacted.cjs'); var util_captureNearbyComments = require('./util.capture-nearby-comments.cjs'); var util_captureRelevantComments = require('./util.capture-relevant-comments.cjs'); 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 || !isOnOwnLine.isCommentOnOwnLine(sourceCode, currentCommentLine) || isSemanticComment.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: const_boilerplateSize.SINGLE_LINE_COMMENT_BOILERPLATE_SIZE, comment: { range: currentCommentLine.range, lines: [currentCommentLine.value], value: currentCommentLine.value } }; const currentBlock = util_captureRelevantComments.captureRelevantCommentsIntoBlock( sourceCode, comments, i, context ); const fixableComment = currentBlock.mergedComment; i += currentBlock.endIndex - currentBlock.startIndex; const nearbyComments = util_captureNearbyComments.captureNearbyComments(comments, i); const wrappedByBackticks = (nearbyComments?.value.trimStart().startsWith("` ") || nearbyComments?.value.trimStart().startsWith("``")) && nearbyComments?.value.trimEnd().endsWith("`"); if (!fixableComment || wrappedByBackticks || isCommentInComment.isCommentInComment(fixableComment.value) || isCodeInComment.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.isLineOverflowing(line2.value, context))) { ruleContext.report({ loc: fixableComment.loc, messageId: const_messageIds.MessageIds.EXCEEDS_MAX_LENGTH, data: { maxLength: context.maxLength }, fix: (fixer) => fix_overflow.fixOverflow(fixer, fixableComment, context) }); } else if (context.mode === "compact" && util_canBlockBeCompacted.canBlockBeCompated(comments, currentBlock, context)) { ruleContext.report({ loc: fixableComment.loc, messageId: const_messageIds.MessageIds.CAN_COMPACT, data: { maxLength: context.maxLength }, fix: (fixer) => fix_overflow.fixOverflow(fixer, fixableComment, context) }); } } } exports.limitSingleLineComments = limitSingleLineComments; //# sourceMappingURL=root.cjs.map