eslint-plugin-comment-length
Version:
An ESLint plugin that provides rules that limit the line length of your comments
134 lines (130 loc) • 4.71 kB
JavaScript
;
var utils = require('@typescript-eslint/utils');
var astUtils = require('@typescript-eslint/utils/ast-utils');
var const_messageIds = require('../../const.message-ids.cjs');
var typings_options = require('../../typings.options.cjs');
var resolveDocsRoute = require('../../utils/resolve-docs-route.cjs');
var root$1 = require('../limit-multi-line-comments/root.cjs');
var root = require('../limit-single-line-comments/root.cjs');
const createRule = utils.ESLintUtils.RuleCreator(resolveDocsRoute.resolveDocsRoute);
const limitTaggedTemplateLiteralCommentsRule = createRule({
name: "limit-tagged-template-literal-comments",
defaultOptions: [{ ...typings_options.defaultOptions[0], tags: ["css"] }],
meta: {
type: "layout",
fixable: "whitespace",
messages: const_messageIds.reportMessages,
docs: {
description: "Reflows javascript comments within tagged template literals to ensure that blocks never exceed the configured length"
},
schema: [
{
...typings_options.optionsSchema,
type: "object",
properties: {
...typings_options.optionsSchema[0].properties,
tags: { type: "array", items: { type: "string" } }
}
}
]
},
create: (ruleContext, [options]) => {
return {
TaggedTemplateExpression: (node) => {
if (!astUtils.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.AST_TOKEN_TYPES.Block,
value: "",
loc: {
start: {
column,
line
},
end: {
column: 0,
line: 0
}
},
range: [rangeIndex, 0]
};
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 = void 0;
continue;
}
if (currentChar === "/" && nextChar === "/" && quasi.value.cooked[cursor + 2] === " ") {
currentLineComment = {
type: utils.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 = void 0;
}
line++;
column = 0;
} else {
column++;
}
}
}
root.limitSingleLineComments(ruleContext, options, lineComments);
root$1.limitMultiLineComments(ruleContext, options, blockComments);
}
};
}
});
exports.limitTaggedTemplateLiteralCommentsRule = limitTaggedTemplateLiteralCommentsRule;
//# sourceMappingURL=rule.cjs.map