eslint-plugin-comment-length
Version:
An ESLint plugin that provides rules that limit the line length of your comments
47 lines (43 loc) • 1.98 kB
JavaScript
;
var isAnotherWrapPointComing = require('../../utils/is-another-wrap-point-coming.cjs');
var isPunctuation = require('../../utils/is-punctuation.cjs');
var util_boilerplateSize = require('./util.boilerplate-size.cjs');
function formatBlock(fixable, context) {
const lineStartSize = context.whitespace.size + util_boilerplateSize.MULTILINE_BOILERPLATE_SIZE + (fixable.lineOffsets[0]?.size ?? 0);
const words = fixable.value.trim().split(" ");
const newValue = words.reduce(
(acc, curr, index) => {
const lengthIfAdded = acc.currentLineLength + curr.length + 1;
const splitToNewline = lengthIfAdded > context.maxLength && acc.currentLineLength !== lineStartSize;
const previousWord = words[index - 1];
const splitEarly = context.logicalWrap && acc.currentLineLength >= context.maxLength / 2 && previousWord && previousWord.at(-1) && isPunctuation.isPunctuation(previousWord.at(-1)) && previousWord.length > 1 && !isAnotherWrapPointComing.isAnotherWrapPointComing(
acc.currentLineLength,
context.maxLength,
words.slice(index)
);
if (splitToNewline || splitEarly) {
const nextLine = `${context.whitespace.string} *${fixable.lineOffsets[Math.min(acc.currentLineIndex + 1, fixable.lineOffsets.length - 1)]?.string ?? ""}${curr} `;
return {
value: `${acc.value.trimEnd()}
${nextLine}`,
currentLineLength: nextLine.length,
currentLineIndex: acc.currentLineIndex + 1
};
} else {
return {
value: `${acc.value}${curr} `,
currentLineLength: lengthIfAdded,
currentLineIndex: acc.currentLineIndex
};
}
},
{
value: `${context.whitespace.string} *${fixable.lineOffsets[0]?.string ?? ""}`,
currentLineLength: lineStartSize,
currentLineIndex: 0
}
);
return newValue.value.trimEnd();
}
exports.formatBlock = formatBlock;
//# sourceMappingURL=util.format-block.cjs.map