UNPKG

eslint-plugin-comment-length

Version:

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

70 lines (66 loc) 2.92 kB
'use strict'; var isCodeInComment = require('../../utils/is-code-in-comment.cjs'); var isCommentInComment = require('../../utils/is-comment-in-comment.cjs'); var isJsdocLike = require('../../utils/is-jsdoc-like.cjs'); var isOnOwnLine = require('../../utils/is-on-own-line.cjs'); var isSemanticComment = require('../../utils/is-semantic-comment.cjs'); var detect_overflow = require('./detect.overflow.cjs'); var report_compact = require('./report.compact.cjs'); var report_overflow = require('./report.overflow.cjs'); var util_boilerplateSize = require('./util.boilerplate-size.cjs'); var util_extractBlocks = require('./util.extract-blocks.cjs'); function limitMultiLineComments(ruleContext, options, comments) { const sourceCode = ruleContext.sourceCode; const lines = sourceCode.getLines(); for (const comment of comments) { const commentRange = comment.range; if (!commentRange || !comment.loc || comment.type !== "Block" || !isOnOwnLine.isCommentOnOwnLine(sourceCode, comment) || isSemanticComment.isSemanticComment(comment, options.semanticComments)) { continue; } const whitespaceString = (() => { const firstLine = lines[comment.loc.start.line - 1]; const lastLine = lines[comment.loc.end.line - 1]; if (comment.loc.start.line === comment.loc.end.line || lastLine && !/^( |\t)*\*\//.test(lastLine)) { return firstLine?.split("/*")[0] ?? ""; } return lastLine?.split(" */")[0] ?? firstLine?.split("/*")[0] ?? ""; })(); const commentLines = getCommentLines(comment); const context = { ...options, whitespace: { string: whitespaceString, size: whitespaceString.split("").reduce( (acc, curr) => acc + (curr === " " ? options.tabSize : 1), 0 ) }, boilerplateSize: util_boilerplateSize.getBoilerPlateSize(commentLines), comment: { range: commentRange, lines: commentLines, value: comment.value } }; const blocks = util_extractBlocks.extractBlocksFromMultilineComment(context).filter( (block) => !block.lines.some( (line) => isCommentInComment.isCommentInComment(line) || isJsdocLike.isJSDocLikeComment(line) ) && !isCodeInComment.isCodeInComment(block.value, context) ); const overflowingBlocks = detect_overflow.detectOverflowInMultilineBlocks( ruleContext, context, blocks ); report_overflow.reportOverflowingBlocks(ruleContext, comment, context, overflowingBlocks); const remainingBlocks = blocks.filter( (it) => !overflowingBlocks.includes(it) ); report_compact.reportCompactableBlocks(ruleContext, comment, context, remainingBlocks); } } function getCommentLines(comment) { return comment.value.split("\n").map((it) => it.replace(/^( |\t)*?\*/, "")); } exports.limitMultiLineComments = limitMultiLineComments; //# sourceMappingURL=root.cjs.map