@isentinel/eslint-plugin-comment-length
Version:
42 lines • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatBlock = void 0;
const is_another_wrap_point_coming_1 = require("../../utils/is-another-wrap-point-coming");
const is_punctuation_1 = require("../../utils/is-punctuation");
const is_url_1 = require("../../utils/is-url");
const const_boilerplate_size_1 = require("./const.boilerplate-size");
function formatBlock(block, context) {
const lineStartSize = context.whitespace.size + const_boilerplate_size_1.SINGLE_LINE_COMMENT_BOILERPLATE_SIZE;
const words = block.value.trim().split(" ");
const newValue = words.reduce((acc, curr, index) => {
const currentWordIsURL = (0, is_url_1.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 &&
(0, is_punctuation_1.isPunctuation)(previousWord.at(-1)) &&
previousWord.length > 1 &&
!(0, is_another_wrap_point_coming_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;
}
exports.formatBlock = formatBlock;
//# sourceMappingURL=util.format-block.js.map