UNPKG

eslint-plugin-comment-length

Version:

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

59 lines (51 loc) 1.97 kB
import type { TSESTree } from "@typescript-eslint/utils"; import type { Context } from "../../typings.context.js"; import { isAnotherWrapPointComing } from "../../utils/is-another-wrap-point-coming.js"; import { isPunctuation } from "../../utils/is-punctuation.js"; import { isURL } from "../../utils/is-url.js"; import { SINGLE_LINE_COMMENT_BOILERPLATE_SIZE } from "./const.boilerplate-size.js"; export function formatBlock( block: TSESTree.LineComment, context: Context, ): string { const lineStartSize = context.whitespace.size + SINGLE_LINE_COMMENT_BOILERPLATE_SIZE; const words = block.value.trim().split(" "); const newValue = words.reduce( (acc, curr, index) => { const currentWordIsURL = isURL(curr); const lengthIfAdded = acc.currentLineLength + curr.length + 1; // We can safely split to a new line in case we are reaching and // overflowing line AND if there is at least one word on the current line. const splitToNewline = lengthIfAdded >= context.maxLength && acc.currentLineLength !== lineStartSize && (!context.ignoreUrls || !currentWordIsURL); const previousWord = words[index - 1]; const splitEarly = context.logicalWrap && acc.currentLineLength >= context.maxLength / 2 && previousWord && isPunctuation(previousWord.at(-1)) && previousWord.length > 1 && !isAnotherWrapPointComing( acc.currentLineLength, context.maxLength, words.slice(index), ); if (splitToNewline || splitEarly) { return { value: `${acc.value}\n${context.whitespace.string}// ${curr}`, currentLineLength: lineStartSize + curr.length, }; } else { return { value: `${acc.value} ${curr}`, currentLineLength: lengthIfAdded, }; } }, { value: "//", currentLineLength: lineStartSize }, ); return newValue.value; }