@isentinel/eslint-plugin-comment-length
Version:
64 lines • 3.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.limitMultiLineComments = void 0;
const is_code_in_comment_1 = require("../../utils/is-code-in-comment");
const is_comment_in_comment_1 = require("../../utils/is-comment-in-comment");
const is_jsdoc_like_1 = require("../../utils/is-jsdoc-like");
const is_on_own_line_1 = require("../../utils/is-on-own-line");
const is_semantic_comment_1 = require("../../utils/is-semantic-comment");
const detect_overflow_1 = require("./detect.overflow");
const report_compact_1 = require("./report.compact");
const report_overflow_1 = require("./report.overflow");
const util_boilerplate_size_1 = require("./util.boilerplate-size");
const util_extract_blocks_1 = require("./util.extract-blocks");
function limitMultiLineComments(ruleContext, options, comments) {
const sourceCode = ruleContext.getSourceCode();
const lines = sourceCode.getLines();
for (const comment of comments) {
const commentRange = comment.range;
if (!commentRange ||
!comment.loc ||
comment.type !== "Block" ||
!(0, is_on_own_line_1.isCommentOnOwnLine)(sourceCode, comment) ||
(0, is_semantic_comment_1.isSemanticComment)(comment)) {
continue;
}
const whitespaceString = (() => {
const firstLine = lines[comment.loc.start.line - 1];
const lastLine = lines[comment.loc.end.line - 1];
if (comment.loc.start.line === comment.loc.end.line ||
(lastLine && !/^( |\t)*\*\//.test(lastLine))) {
return firstLine?.split("/*")[0] ?? "";
}
return lastLine?.split(" */")[0] ?? firstLine?.split("/*")[0] ?? "";
})();
const commentLines = getCommentLines(comment);
const context = {
...options,
whitespace: {
string: whitespaceString,
size: whitespaceString
.split("")
.reduce((acc, curr) => acc + (curr === "\t" ? options.tabSize : 1), 0),
},
boilerplateSize: (0, util_boilerplate_size_1.getBoilerPlateSize)(commentLines),
comment: {
range: commentRange,
lines: commentLines,
value: comment.value,
},
};
// Extract all valid blocks, but immediately remove those that should be
// ignored no matter what.
const blocks = (0, util_extract_blocks_1.extractBlocksFromMultilineComment)(context).filter((block) => !block.lines.some((line) => (0, is_comment_in_comment_1.isCommentInComment)(line) || (0, is_jsdoc_like_1.isJSDocLikeComment)(line)) && !(0, is_code_in_comment_1.isCodeInComment)(block.value, ruleContext.parserPath, context));
const overflowingBlocks = (0, detect_overflow_1.detectOverflowInMultilineBlocks)(ruleContext, context, blocks);
(0, report_overflow_1.reportOverflowingBlocks)(ruleContext, comment, context, overflowingBlocks);
const remainingBlocks = blocks.filter((it) => !overflowingBlocks.includes(it));
(0, report_compact_1.reportCompactableBlocks)(ruleContext, comment, context, remainingBlocks);
}
}
exports.limitMultiLineComments = limitMultiLineComments;
function getCommentLines(comment) {
return comment.value.split("\n").map((it) => it.replace(/^( |\t)*?\*/, ""));
}
//# sourceMappingURL=root.js.map