eslint-plugin-comment-length
Version:
An ESLint plugin that provides rules that limit the line length of your comments
1 lines • 3.44 kB
Source Map (JSON)
{"version":3,"file":"fix.overflow.cjs","sources":["../../../../src/rules/limit-multi-line-comments/fix.overflow.ts"],"sourcesContent":["import type { TSESLint } from \"@typescript-eslint/utils\";\n\nimport type { Context } from \"../../typings.context.js\";\n\nimport type { MultilineBlock } from \"./typings.block.js\";\nimport { FIRST_LINE_BOILERPLATE_SIZE } from \"./util.boilerplate-size.js\";\nimport { formatBlock } from \"./util.format-block.js\";\n\nexport function fixOverflowingBlock(\n fixer: TSESLint.RuleFixer,\n fixableBlock: MultilineBlock,\n context: Context,\n) {\n const newValue = formatBlock(fixableBlock, context);\n\n // Now, in case the entire block is only a single line\n // (e.g. /** text... */), then we should expand it into a multi-line\n // comment to preserve space.\n if (context.comment.lines.length === 1) {\n return fixer.replaceTextRange(\n context.comment.range,\n `/**\\n${newValue}\\n${context.whitespace.string} */`,\n );\n } else {\n // ... else we should simply replace the part of the comment which\n // overflows.\n const rawLines = context.comment.value.split(\"\\n\");\n const rangeStart =\n context.comment.range[0] +\n FIRST_LINE_BOILERPLATE_SIZE +\n rawLines.slice(0, fixableBlock.startIndex).join(\"\\n\").length;\n const rangeEnd =\n context.comment.range[0] +\n FIRST_LINE_BOILERPLATE_SIZE -\n 1 +\n rawLines.slice(0, fixableBlock.endIndex + 1).join(\"\\n\").length;\n\n let paddedValue = newValue;\n\n // and, in the rare case where the violating block starts on\n // the same line as the start of the multi-comment\n // (i.e. /** my-comment...), then move it down to the next line,\n // to maximize the available space.\n if (fixableBlock.startIndex === 0) {\n paddedValue = `\\n${paddedValue}`;\n }\n\n // ... and ensure that the end of the comment is actually pushed to the\n // final line if it isn't already.\n if (fixableBlock.endIndex === rawLines.length - 1) {\n paddedValue = `${paddedValue}\\n${context.whitespace.string} `;\n }\n\n return fixer.replaceTextRange([rangeStart, rangeEnd], paddedValue);\n }\n}\n"],"names":["formatBlock","FIRST_LINE_BOILERPLATE_S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