eslint-plugin-comment-length
Version:
An ESLint plugin that provides rules that limit the line length of your comments
1 lines • 7.45 kB
Source Map (JSON)
{"version":3,"file":"root.cjs","sources":["../../../../src/rules/limit-single-line-comments/root.ts"],"sourcesContent":["import type { TSESTree } from \"@typescript-eslint/utils\";\nimport type { RuleContext } from \"@typescript-eslint/utils/ts-eslint\";\n\nimport { MessageIds } from \"../../const.message-ids.js\";\nimport type { Context } from \"../../typings.context.js\";\nimport type { Options } from \"../../typings.options.js\";\nimport { isCodeInComment } from \"../../utils/is-code-in-comment.js\";\nimport { isCommentInComment } from \"../../utils/is-comment-in-comment.js\";\nimport { isLineOverflowing } from \"../../utils/is-line-overflowing.js\";\nimport { isCommentOnOwnLine } from \"../../utils/is-on-own-line.js\";\nimport { isSemanticComment } from \"../../utils/is-semantic-comment.js\";\n\nimport { SINGLE_LINE_COMMENT_BOILERPLATE_SIZE } from \"./const.boilerplate-size.js\";\nimport { fixOverflow } from \"./fix.overflow.js\";\nimport { canBlockBeCompated } from \"./util.can-block-be-compacted.js\";\nimport { captureNearbyComments } from \"./util.capture-nearby-comments.js\";\nimport { captureRelevantCommentsIntoBlock } from \"./util.capture-relevant-comments.js\";\n\nexport function limitSingleLineComments(\n ruleContext: RuleContext<string, unknown[]>,\n options: Options,\n comments: TSESTree.LineComment[],\n) {\n const sourceCode = ruleContext.sourceCode;\n const lines = sourceCode.getLines();\n\n for (let i = 0; i < comments.length; i++) {\n const currentCommentLine = comments[i];\n\n if (\n !currentCommentLine?.range ||\n !currentCommentLine.value ||\n !isCommentOnOwnLine(sourceCode, currentCommentLine) ||\n isSemanticComment(currentCommentLine, options.semanticComments)\n ) {\n continue;\n }\n\n const line = lines[currentCommentLine.loc.start.line - 1];\n const whitespaceString = line?.split(\"//\")[0] ?? \"\";\n\n let context = {\n ...options,\n whitespace: {\n string: whitespaceString,\n size: whitespaceString\n .split(\"\")\n .reduce(\n (acc, curr) => acc + (curr === \"\\t\" ? options.tabSize : 1),\n 0,\n ),\n },\n boilerplateSize: SINGLE_LINE_COMMENT_BOILERPLATE_SIZE,\n comment: {\n range: currentCommentLine.range,\n lines: [currentCommentLine.value],\n value: currentCommentLine.value,\n },\n } satisfies Context;\n\n const currentBlock = captureRelevantCommentsIntoBlock(\n sourceCode,\n comments,\n i,\n context,\n );\n const fixableComment = currentBlock.mergedComment;\n\n // ensure that we only visit a captured block once\n i += currentBlock.endIndex - currentBlock.startIndex;\n\n const nearbyComments = captureNearbyComments(comments, i);\n const wrappedByBackticks =\n (nearbyComments?.value.trimStart().startsWith(\"` \") ||\n nearbyComments?.value.trimStart().startsWith(\"``\")) &&\n nearbyComments?.value.trimEnd().endsWith(\"`\");\n\n if (\n !fixableComment ||\n wrappedByBackticks ||\n isCommentInComment(fixableComment.value) ||\n isCodeInComment(nearbyComments?.value, context)\n ) {\n continue;\n }\n\n // Update our context to reflect that we may have merged multiple comment\n // lines into a singular block.\n context = {\n ...context,\n comment: {\n range: fixableComment.range,\n lines: [fixableComment.value],\n value: fixableComment.value,\n },\n };\n\n // In case any lines in our current block overflows, then we need to warn\n // that overflow has been detected\n if (\n comments\n .slice(currentBlock.startIndex, currentBlock.endIndex + 1)\n .some((line) => isLineOverflowing(line.value, context))\n ) {\n ruleContext.report({\n loc: fixableComment.loc,\n messageId: MessageIds.EXCEEDS_MAX_LENGTH,\n data: {\n maxLength: context.maxLength,\n },\n fix: (fixer) => fixOverflow(fixer, fixableComment, context),\n });\n } else if (\n context.mode === \"compact\" &&\n canBlockBeCompated(comments, currentBlock, context)\n ) {\n ruleContext.report({\n loc: fixableComment.loc,\n messageId: MessageIds.CAN_COMPACT,\n data: {\n maxLength: context.maxLength,\n },\n fix: (fixer) => fixOverflow(fixer, fixableComment, context),\n });\n }\n }\n}\n"],"names":["isCommentOnOwnLine","isSemanticComment","SINGLE_LINE_COMMENT_BOILERPLATE_SIZE","captureRelevantCommentsIntoBlock","captureNearbyComments","isCommentInComment","isCodeInComment","line","isLineOverflowing","MessageIds","fixOverflow","canBlockB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