UNPKG

eslint-plugin-comment-length

Version:

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

42 lines (38 loc) 1.71 kB
'use strict'; var isAnotherWrapPointComing = require('../../utils/is-another-wrap-point-coming.cjs'); var isPunctuation = require('../../utils/is-punctuation.cjs'); var isUrl = require('../../utils/is-url.cjs'); var const_boilerplateSize = require('./const.boilerplate-size.cjs'); function formatBlock(block, context) { const lineStartSize = context.whitespace.size + const_boilerplateSize.SINGLE_LINE_COMMENT_BOILERPLATE_SIZE; const words = block.value.trim().split(" "); const newValue = words.reduce( (acc, curr, index) => { const currentWordIsURL = isUrl.isURL(curr); const lengthIfAdded = acc.currentLineLength + curr.length + 1; 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.isPunctuation(previousWord.at(-1)) && previousWord.length > 1 && !isAnotherWrapPointComing.isAnotherWrapPointComing( acc.currentLineLength, context.maxLength, words.slice(index) ); if (splitToNewline || splitEarly) { return { value: `${acc.value} ${context.whitespace.string}// ${curr}`, currentLineLength: lineStartSize + curr.length }; } else { return { value: `${acc.value} ${curr}`, currentLineLength: lengthIfAdded }; } }, { value: "//", currentLineLength: lineStartSize } ); return newValue.value; } exports.formatBlock = formatBlock; //# sourceMappingURL=util.format-block.cjs.map