eslint-plugin-comment-length
Version:
An ESLint plugin that provides rules that limit the line length of your comments
40 lines (37 loc) • 1.63 kB
JavaScript
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';
function formatBlock(block, context) {
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;
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}
${context.whitespace.string}// ${curr}`,
currentLineLength: lineStartSize + curr.length
};
} else {
return {
value: `${acc.value} ${curr}`,
currentLineLength: lengthIfAdded
};
}
},
{ value: "//", currentLineLength: lineStartSize }
);
return newValue.value;
}
export { formatBlock };
//# sourceMappingURL=util.format-block.js.map