|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
-
-
- "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: {
- type: "layout",
-
- docs: {
- description: "enforce a maximum line length",
- category: "Stylistic Issues",
- recommended: false,
- url: "https://eslint.org/docs/rules/max-len"
- },
-
- schema: [
- OPTIONS_OR_INTEGER_SCHEMA,
- OPTIONS_OR_INTEGER_SCHEMA,
- OPTIONS_SCHEMA
- ]
- },
-
- create(context) {
-
-
-
- const URL_REGEXP = /[^:/?#]:\/\/[^?#]/;
-
- const sourceCode = context.getSourceCode();
-
-
-
- function computeLineLength(line, tabWidth) {
- let extraCharacterCount = 0;
-
- line.replace(/\t/g, (match, offset) => {
- const totalOffset = offset + extraCharacterCount,
- previousTabStopOffset = tabWidth ? totalOffset % tabWidth : 0,
- spaceCount = tabWidth - previousTabStopOffset;
-
- extraCharacterCount += spaceCount - 1;
- });
- return Array.from(line).length + extraCharacterCount;
- }
-
-
- const lastOption = context.options[context.options.length - 1];
- const options = typeof lastOption === "object" ? Object.create(lastOption) : {};
-
-
- if (typeof context.options[0] === "number") {
- options.code = context.options[0];
- }
-
-
- if (typeof context.options[1] === "number") {
- options.tabWidth = context.options[1];
- }
-
- const maxLength = options.code || 80,
- tabWidth = options.tabWidth || 4,
- ignoreComments = options.ignoreComments || false,
- ignoreStrings = options.ignoreStrings || false,
- ignoreTemplateLiterals = options.ignoreTemplateLiterals || false,
- ignoreRegExpLiterals = options.ignoreRegExpLiterals || false,
- ignoreTrailingComments = options.ignoreTrailingComments || options.ignoreComments || false,
- ignoreUrls = options.ignoreUrls || false,
- maxCommentLength = options.comments;
- let ignorePattern = options.ignorePattern || null;
-
- if (ignorePattern) {
- ignorePattern = new RegExp(ignorePattern);
- }
-
-
-
-
-
-
-
- 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 stripTrailingComment(line, comment) {
-
-
- return line.slice(0, comment.loc.start.column).replace(/\s+$/, "");
- }
-
-
-
- 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 groupByLineNumber(acc, node) {
- for (let i = node.loc.start.line; i <= node.loc.end.line; ++i) {
- ensureArrayAndPush(acc, i, node);
- }
- return acc;
- }
-
-
-
- function checkProgramForMaxLength(node) {
-
-
- const lines = sourceCode.lines,
-
-
- comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? sourceCode.getAllComments() : [];
-
-
- let commentsIndex = 0;
-
- const strings = getAllStrings();
- const stringsByLine = strings.reduce(groupByLineNumber, {});
-
- const templateLiterals = getAllTemplateLiterals();
- const templateLiteralsByLine = templateLiterals.reduce(groupByLineNumber, {});
-
- const regExpLiterals = getAllRegExpLiterals();
- const regExpLiteralsByLine = regExpLiterals.reduce(groupByLineNumber, {});
-
- lines.forEach((line, i) => {
-
-
- const lineNumber = i + 1;
-
-
-
- let lineIsComment = false;
- let textToMeasure;
-
-
-
- if (commentsIndex < comments.length) {
- let comment = null;
-
-
- 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);
- } 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;
- }
-
- if (commentLengthApplies) {
- if (lineLength > maxCommentLength) {
- context.report({
- node,
- loc: { line: lineNumber, column: 0 },
- message: "Line {{lineNumber}} exceeds the maximum comment line length of {{maxCommentLength}}.",
- data: {
- lineNumber: i + 1,
- maxCommentLength
- }
- });
- }
- } else if (lineLength > maxLength) {
- context.report({
- node,
- loc: { line: lineNumber, column: 0 },
- message: "Line {{lineNumber}} exceeds the maximum line length of {{maxLength}}.",
- data: {
- lineNumber: i + 1,
- maxLength
- }
- });
- }
- });
- }
-
-
-
-
-
-
- return {
- Program: checkProgramForMaxLength
- };
-
- }
- };
|