123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- "use strict";
- const OPTIONS_SCHEMA = {
- type: "object",
- properties: {
- code: {
- type: "integer",
- minimum: 0
- },
- comments: {
- type: "integer",
- minimum: 0
- },
- tabWidth: {
- type: "integer",
- minimum: 0
- },
- ignorePattern: {
- type: "string"
- },
- ignoreComments: {
- type: "boolean"
- },
- ignoreStrings: {
- type: "boolean"
- },
- ignoreUrls: {
- type: "boolean"
- },
- ignoreTemplateLiterals: {
- type: "boolean"
- },
- ignoreRegExpLiterals: {
- type: "boolean"
- },
- ignoreTrailingComments: {
- type: "boolean"
- }
- },
- additionalProperties: false
- };
- const OPTIONS_OR_INTEGER_SCHEMA = {
- anyOf: [
- OPTIONS_SCHEMA,
- {
- type: "integer",
- minimum: 0
- }
- ]
- };
- module.exports = {
- meta: {
- deprecated: true,
- replacedBy: [],
- type: "layout",
- docs: {
- description: "Enforce a maximum line length",
- recommended: false,
- url: "https://eslint.org/docs/latest/rules/max-len"
- },
- schema: [
- OPTIONS_OR_INTEGER_SCHEMA,
- OPTIONS_OR_INTEGER_SCHEMA,
- OPTIONS_SCHEMA
- ],
- messages: {
- max: "This line has a length of {{lineLength}}. Maximum allowed is {{maxLength}}.",
- maxComment: "This line has a comment length of {{lineLength}}. Maximum allowed is {{maxCommentLength}}."
- }
- },
- create(context) {
-
- const URL_REGEXP = /[^:/?#]:\/\/[^?#]/u;
- const sourceCode = context.sourceCode;
-
- function computeLineLength(line, tabWidth) {
- let extraCharacterCount = 0;
- line.replace(/\t/gu, (match, offset) => {
- const totalOffset = offset + extraCharacterCount,
- previousTabStopOffset = tabWidth ? totalOffset % tabWidth : 0,
- spaceCount = tabWidth - previousTabStopOffset;
- extraCharacterCount += spaceCount - 1;
- });
- return Array.from(line).length + extraCharacterCount;
- }
-
- const options = Object.assign({}, context.options.at(-1));
-
- if (typeof context.options[0] === "number") {
- options.code = context.options[0];
- }
-
- if (typeof context.options[1] === "number") {
- options.tabWidth = context.options[1];
- }
- const maxLength = typeof options.code === "number" ? options.code : 80,
- tabWidth = typeof options.tabWidth === "number" ? options.tabWidth : 4,
- ignoreComments = !!options.ignoreComments,
- ignoreStrings = !!options.ignoreStrings,
- ignoreTemplateLiterals = !!options.ignoreTemplateLiterals,
- ignoreRegExpLiterals = !!options.ignoreRegExpLiterals,
- ignoreTrailingComments = !!options.ignoreTrailingComments || !!options.ignoreComments,
- ignoreUrls = !!options.ignoreUrls,
- maxCommentLength = options.comments;
- let ignorePattern = options.ignorePattern || null;
- if (ignorePattern) {
- ignorePattern = new RegExp(ignorePattern, "u");
- }
-
-
-
-
- function isTrailingComment(line, lineNumber, comment) {
- return comment &&
- (comment.loc.start.line === lineNumber && lineNumber <= comment.loc.end.line) &&
- (comment.loc.end.line > lineNumber || comment.loc.end.column === line.length);
- }
-
- function isFullLineComment(line, lineNumber, comment) {
- const start = comment.loc.start,
- end = comment.loc.end,
- isFirstTokenOnLine = !line.slice(0, comment.loc.start.column).trim();
- return comment &&
- (start.line < lineNumber || (start.line === lineNumber && isFirstTokenOnLine)) &&
- (end.line > lineNumber || (end.line === lineNumber && end.column === line.length));
- }
-
- function isJSXEmptyExpressionInSingleLineContainer(node) {
- if (!node || !node.parent || node.type !== "JSXEmptyExpression" || node.parent.type !== "JSXExpressionContainer") {
- return false;
- }
- const parent = node.parent;
- return parent.loc.start.line === parent.loc.end.line;
- }
-
- function stripTrailingComment(line, comment) {
-
- return line.slice(0, comment.loc.start.column).replace(/\s+$/u, "");
- }
-
- function ensureArrayAndPush(object, key, value) {
- if (!Array.isArray(object[key])) {
- object[key] = [];
- }
- object[key].push(value);
- }
-
- function getAllStrings() {
- return sourceCode.ast.tokens.filter(token => (token.type === "String" ||
- (token.type === "JSXText" && sourceCode.getNodeByRangeIndex(token.range[0] - 1).type === "JSXAttribute")));
- }
-
- function getAllTemplateLiterals() {
- return sourceCode.ast.tokens.filter(token => token.type === "Template");
- }
-
- function getAllRegExpLiterals() {
- return sourceCode.ast.tokens.filter(token => token.type === "RegularExpression");
- }
-
- function groupArrayByLineNumber(arr) {
- const obj = {};
- for (let i = 0; i < arr.length; i++) {
- const node = arr[i];
- for (let j = node.loc.start.line; j <= node.loc.end.line; ++j) {
- ensureArrayAndPush(obj, j, node);
- }
- }
- return obj;
- }
-
- function getAllComments() {
- const comments = [];
- sourceCode.getAllComments()
- .forEach(commentNode => {
- const containingNode = sourceCode.getNodeByRangeIndex(commentNode.range[0]);
- if (isJSXEmptyExpressionInSingleLineContainer(containingNode)) {
-
- if (comments.at(-1) !== containingNode.parent) {
- comments.push(containingNode.parent);
- }
- } else {
- comments.push(commentNode);
- }
- });
- return comments;
- }
-
- function checkProgramForMaxLength(node) {
-
- const lines = sourceCode.lines,
-
- comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? getAllComments() : [];
-
- let commentsIndex = 0;
- const strings = getAllStrings();
- const stringsByLine = groupArrayByLineNumber(strings);
- const templateLiterals = getAllTemplateLiterals();
- const templateLiteralsByLine = groupArrayByLineNumber(templateLiterals);
- const regExpLiterals = getAllRegExpLiterals();
- const regExpLiteralsByLine = groupArrayByLineNumber(regExpLiterals);
- lines.forEach((line, i) => {
-
- const lineNumber = i + 1;
-
- let lineIsComment = false;
- let textToMeasure;
-
- if (commentsIndex < comments.length) {
- let comment;
-
- do {
- comment = comments[++commentsIndex];
- } while (comment && comment.loc.start.line <= lineNumber);
-
- comment = comments[--commentsIndex];
- if (isFullLineComment(line, lineNumber, comment)) {
- lineIsComment = true;
- textToMeasure = line;
- } else if (ignoreTrailingComments && isTrailingComment(line, lineNumber, comment)) {
- textToMeasure = stripTrailingComment(line, comment);
-
- let lastIndex = commentsIndex;
- while (isTrailingComment(textToMeasure, lineNumber, comments[--lastIndex])) {
- textToMeasure = stripTrailingComment(textToMeasure, comments[lastIndex]);
- }
- } else {
- textToMeasure = line;
- }
- } else {
- textToMeasure = line;
- }
- if (ignorePattern && ignorePattern.test(textToMeasure) ||
- ignoreUrls && URL_REGEXP.test(textToMeasure) ||
- ignoreStrings && stringsByLine[lineNumber] ||
- ignoreTemplateLiterals && templateLiteralsByLine[lineNumber] ||
- ignoreRegExpLiterals && regExpLiteralsByLine[lineNumber]
- ) {
-
- return;
- }
- const lineLength = computeLineLength(textToMeasure, tabWidth);
- const commentLengthApplies = lineIsComment && maxCommentLength;
- if (lineIsComment && ignoreComments) {
- return;
- }
- const loc = {
- start: {
- line: lineNumber,
- column: 0
- },
- end: {
- line: lineNumber,
- column: textToMeasure.length
- }
- };
- if (commentLengthApplies) {
- if (lineLength > maxCommentLength) {
- context.report({
- node,
- loc,
- messageId: "maxComment",
- data: {
- lineLength,
- maxCommentLength
- }
- });
- }
- } else if (lineLength > maxLength) {
- context.report({
- node,
- loc,
- messageId: "max",
- data: {
- lineLength,
- maxLength
- }
- });
- }
- });
- }
-
-
-
- return {
- Program: checkProgramForMaxLength
- };
- }
- };
|