UNPKG

@isentinel/eslint-plugin-comment-length

Version:
95 lines 4.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.limitSingleLineComments = void 0; const const_message_ids_1 = require("../../const.message-ids"); const is_code_in_comment_1 = require("../../utils/is-code-in-comment"); const is_comment_in_comment_1 = require("../../utils/is-comment-in-comment"); const is_line_overflowing_1 = require("../../utils/is-line-overflowing"); const is_on_own_line_1 = require("../../utils/is-on-own-line"); const is_semantic_comment_1 = require("../../utils/is-semantic-comment"); const const_boilerplate_size_1 = require("./const.boilerplate-size"); const fix_overflow_1 = require("./fix.overflow"); const util_can_block_be_compacted_1 = require("./util.can-block-be-compacted"); const util_capture_nearby_comments_1 = require("./util.capture-nearby-comments"); const util_capture_relevant_comments_1 = require("./util.capture-relevant-comments"); function limitSingleLineComments(ruleContext, options, comments) { const sourceCode = ruleContext.getSourceCode(); const lines = sourceCode.getLines(); for (let i = 0; i < comments.length; i++) { const currentCommentLine = comments[i]; if (!currentCommentLine?.range || !currentCommentLine.value || !(0, is_on_own_line_1.isCommentOnOwnLine)(sourceCode, currentCommentLine) || (0, is_semantic_comment_1.isSemanticComment)(currentCommentLine)) { 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 === "\t" ? options.tabSize : 1), 0), }, boilerplateSize: const_boilerplate_size_1.SINGLE_LINE_COMMENT_BOILERPLATE_SIZE, comment: { range: currentCommentLine.range, lines: [currentCommentLine.value], value: currentCommentLine.value, }, }; const currentBlock = (0, util_capture_relevant_comments_1.captureRelevantCommentsIntoBlock)(sourceCode, comments, i, context); const fixableComment = currentBlock.mergedComment; // ensure that we only visit a captured block once i += currentBlock.endIndex - currentBlock.startIndex; const nearbyComments = (0, util_capture_nearby_comments_1.captureNearbyComments)(comments, i); const wrappedByBackticks = (nearbyComments?.value.trimStart().startsWith("` ") || nearbyComments?.value.trimStart().startsWith("``")) && nearbyComments?.value.trimEnd().endsWith("`"); if (!fixableComment || wrappedByBackticks || (0, is_comment_in_comment_1.isCommentInComment)(fixableComment.value) || (0, is_code_in_comment_1.isCodeInComment)(nearbyComments?.value, ruleContext.parserPath, context)) { continue; } // Update our context to reflect that we may have merged multiple comment // lines into a singular block. context = { ...context, comment: { range: fixableComment.range, lines: [fixableComment.value], value: fixableComment.value, }, }; // In case any lines in our current block overflows, then we need to warn // that overflow has been detected if (comments .slice(currentBlock.startIndex, currentBlock.endIndex + 1) .some((line) => (0, is_line_overflowing_1.isLineOverflowing)(line.value, context))) { ruleContext.report({ loc: fixableComment.loc, messageId: const_message_ids_1.MessageIds.EXCEEDS_MAX_LENGTH, data: { maxLength: context.maxLength, }, fix: (fixer) => (0, fix_overflow_1.fixOverflow)(fixer, fixableComment, context), }); } else if (context.mode === "compact" && (0, util_can_block_be_compacted_1.canBlockBeCompated)(comments, currentBlock, context)) { ruleContext.report({ loc: fixableComment.loc, messageId: const_message_ids_1.MessageIds.CAN_COMPACT, data: { maxLength: context.maxLength, }, fix: (fixer) => (0, fix_overflow_1.fixOverflow)(fixer, fixableComment, context), }); } } } exports.limitSingleLineComments = limitSingleLineComments; //# sourceMappingURL=root.js.map