@isentinel/eslint-plugin-comment-length
Version:
139 lines • 6.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.limitTaggedTemplateLiteralCommentsRule = void 0;
const utils_1 = require("@typescript-eslint/utils");
const ast_utils_1 = require("@typescript-eslint/utils/ast-utils");
const const_message_ids_1 = require("../../const.message-ids");
const typings_options_1 = require("../../typings.options");
const resolve_docs_route_1 = require("../../utils/resolve-docs-route");
const root_1 = require("../limit-multi-line-comments/root");
const root_2 = require("../limit-single-line-comments/root");
const createRule = utils_1.ESLintUtils.RuleCreator(resolve_docs_route_1.resolveDocsRoute);
exports.limitTaggedTemplateLiteralCommentsRule = createRule({
name: "limit-tagged-template-literal-comments",
defaultOptions: [{ ...typings_options_1.defaultOptions[0], tags: ["css"] }],
meta: {
type: "layout",
fixable: "whitespace",
messages: const_message_ids_1.reportMessages,
docs: {
description: "Reflows javascript comments within tagged template literals to ensure that blocks never exceed the configured length",
recommended: "stylistic",
},
schema: [
{
...typings_options_1.optionsSchema,
type: "object",
properties: {
...typings_options_1.optionsSchema[0].properties,
tags: { type: "array", items: { type: "string" } },
},
},
],
},
create: (ruleContext, [options]) => {
return {
TaggedTemplateExpression: (node) => {
if (!(0, ast_utils_1.isIdentifier)(node.tag) || !options.tags.includes(node.tag.name)) {
return;
}
const blockComments = [];
const lineComments = [];
for (const quasi of node.quasi.quasis) {
let column = quasi.loc.start.column;
let line = quasi.loc.start.line;
let rangeIndex = quasi.range[0];
let currentBlockComment;
let currentLineComment;
for (let cursor = 0; cursor < quasi.value.cooked.length; cursor++) {
const currentChar = quasi.value.cooked[cursor];
const nextChar = quasi.value.cooked[cursor + 1];
rangeIndex++;
if (currentChar === "/" && nextChar === "*") {
currentBlockComment = {
type: utils_1.AST_TOKEN_TYPES.Block,
value: "",
loc: {
start: {
column,
line,
},
end: {
column: 0,
line: 0,
},
},
range: [rangeIndex, 0],
};
// Skip the next char which is also part of the start of our
// comment.
cursor++;
column += 2;
rangeIndex++;
continue;
}
if (currentChar === "*" &&
nextChar === "/" &&
currentBlockComment) {
cursor++;
column += 2;
rangeIndex++;
currentBlockComment.loc.end.line = line;
currentBlockComment.loc.end.column = column;
currentBlockComment.range[1] = rangeIndex + 1;
blockComments.push(currentBlockComment);
currentBlockComment = undefined;
continue;
}
if (currentChar === "/" &&
nextChar === "/" &&
quasi.value.cooked[cursor + 2] === " ") {
currentLineComment = {
type: utils_1.AST_TOKEN_TYPES.Line,
value: "",
loc: {
start: {
column,
line,
},
end: {
column: 0,
line: 0,
},
},
range: [rangeIndex, 0],
};
cursor++;
column += 2;
rangeIndex++;
continue;
}
if (currentBlockComment) {
currentBlockComment.value += currentChar;
}
if (currentLineComment) {
currentLineComment.value += currentChar;
}
if (currentChar === "\n") {
if (currentLineComment) {
currentLineComment.loc.end.line = line;
currentLineComment.loc.end.column = column;
currentLineComment.range[1] = rangeIndex;
lineComments.push(currentLineComment);
currentLineComment = undefined;
}
line++;
column = 0;
}
else {
column++;
}
}
}
(0, root_2.limitSingleLineComments)(ruleContext, options, lineComments);
(0, root_1.limitMultiLineComments)(ruleContext, options, blockComments);
},
};
},
});
//# sourceMappingURL=rule.js.map