2 * @fileoverview enforce a maximum file length
3 * @author Alberto RodrÃguez
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const lodash = require("lodash");
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
23 description: "enforce a maximum number of lines per file",
24 category: "Stylistic Issues",
26 url: "https://eslint.org/docs/rules/max-lines"
50 additionalProperties: false
56 exceed: "File has too many lines ({{actual}}). Maximum allowed is {{max}}."
61 const option = context.options[0];
64 if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max")) {
66 } else if (typeof option === "number") {
70 const skipComments = option && option.skipComments;
71 const skipBlankLines = option && option.skipBlankLines;
73 const sourceCode = context.getSourceCode();
76 * Returns whether or not a token is a comment node type
77 * @param {Token} token The token to check
78 * @returns {boolean} True if the token is a comment node
80 function isCommentNodeType(token) {
81 return token && (token.type === "Block" || token.type === "Line");
85 * Returns the line numbers of a comment that don't have any code on the same line
86 * @param {Node} comment The comment node to check
87 * @returns {number[]} The line numbers
89 function getLinesWithoutCode(comment) {
90 let start = comment.loc.start.line;
91 let end = comment.loc.end.line;
97 token = sourceCode.getTokenBefore(token, { includeComments: true });
98 } while (isCommentNodeType(token));
100 if (token && astUtils.isTokenOnSameLine(token, comment)) {
106 token = sourceCode.getTokenAfter(token, { includeComments: true });
107 } while (isCommentNodeType(token));
109 if (token && astUtils.isTokenOnSameLine(comment, token)) {
114 return lodash.range(start, end + 1);
121 let lines = sourceCode.lines.map((text, i) => ({ lineNumber: i + 1, text }));
123 if (skipBlankLines) {
124 lines = lines.filter(l => l.text.trim() !== "");
128 const comments = sourceCode.getAllComments();
130 const commentLines = lodash.flatten(comments.map(comment => getLinesWithoutCode(comment)));
132 lines = lines.filter(l => !lodash.includes(commentLines, l.lineNumber));
135 if (lines.length > max) {
137 loc: { line: 1, column: 0 },