UNPKG

eslint-plugin-comment-length

Version:

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

1 lines 3.68 kB
{"version":3,"file":"util.format-block.cjs","sources":["../../../../src/rules/limit-single-line-comments/util.format-block.ts"],"sourcesContent":["import type { TSESTree } from \"@typescript-eslint/utils\";\n\nimport type { Context } from \"../../typings.context.js\";\nimport { isAnotherWrapPointComing } from \"../../utils/is-another-wrap-point-coming.js\";\nimport { isPunctuation } from \"../../utils/is-punctuation.js\";\nimport { isURL } from \"../../utils/is-url.js\";\n\nimport { SINGLE_LINE_COMMENT_BOILERPLATE_SIZE } from \"./const.boilerplate-size.js\";\n\nexport function formatBlock(\n block: TSESTree.LineComment,\n context: Context,\n): string {\n const lineStartSize =\n context.whitespace.size + SINGLE_LINE_COMMENT_BOILERPLATE_SIZE;\n const words = block.value.trim().split(\" \");\n const newValue = words.reduce(\n (acc, curr, index) => {\n const currentWordIsURL = isURL(curr);\n const lengthIfAdded = acc.currentLineLength + curr.length + 1;\n\n // We can safely split to a new line in case we are reaching and\n // overflowing line AND if there is at least one word on the current line.\n const splitToNewline =\n lengthIfAdded >= context.maxLength &&\n acc.currentLineLength !== lineStartSize &&\n (!context.ignoreUrls || !currentWordIsURL);\n\n const previousWord = words[index - 1];\n\n const splitEarly =\n context.logicalWrap &&\n acc.currentLineLength >= context.maxLength / 2 &&\n previousWord &&\n isPunctuation(previousWord.at(-1)) &&\n previousWord.length > 1 &&\n !isAnotherWrapPointComing(\n acc.currentLineLength,\n context.maxLength,\n words.slice(index),\n );\n\n if (splitToNewline || splitEarly) {\n return {\n value: `${acc.value}\\n${context.whitespace.string}// ${curr}`,\n currentLineLength: lineStartSize + curr.length,\n };\n } else {\n return {\n value: `${acc.value} ${curr}`,\n currentLineLength: lengthIfAdded,\n };\n }\n },\n { value: \"//\", currentLineLength: lineStartSize },\n );\n return newValue.value;\n}\n"],"names":["SINGLE_LINE_COMMENT_BOILERPLATE_SIZE","isURL","isPunctuation","isAnotherWrapPointComing"],"mappings":";;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