2 * @fileoverview A rule to set the maximum number of line of code in a function.
3 * @author Pete Ward <peteward44@gmail.com>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const astUtils = require("./utils/ast-utils");
13 const lodash = require("lodash");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const OPTIONS_SCHEMA = {
36 additionalProperties: false
39 const OPTIONS_OR_INTEGER_SCHEMA = {
50 * Given a list of comment nodes, return a map with numeric keys (source code line numbers) and comment token values.
51 * @param {Array} comments An array of comment nodes.
52 * @returns {Map.<string,Node>} A map with numeric keys (source code line numbers) and comment token values.
54 function getCommentLineNumbers(comments) {
55 const map = new Map();
60 comments.forEach(comment => {
61 for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) {
68 //------------------------------------------------------------------------------
70 //------------------------------------------------------------------------------
77 description: "enforce a maximum number of line of code in a function",
78 category: "Stylistic Issues",
80 url: "https://eslint.org/docs/rules/max-lines-per-function"
84 OPTIONS_OR_INTEGER_SCHEMA
87 exceed: "{{name}} has too many lines ({{lineCount}}). Maximum allowed is {{maxLines}}."
92 const sourceCode = context.getSourceCode();
93 const lines = sourceCode.lines;
95 const option = context.options[0];
97 let skipComments = false;
98 let skipBlankLines = false;
101 if (typeof option === "object") {
102 maxLines = typeof option.max === "number" ? option.max : 50;
103 skipComments = !!option.skipComments;
104 skipBlankLines = !!option.skipBlankLines;
105 IIFEs = !!option.IIFEs;
106 } else if (typeof option === "number") {
110 const commentLineNumbers = getCommentLineNumbers(sourceCode.getAllComments());
112 //--------------------------------------------------------------------------
114 //--------------------------------------------------------------------------
117 * Tells if a comment encompasses the entire line.
118 * @param {string} line The source line with a trailing comment
119 * @param {number} lineNumber The one-indexed line number this is on
120 * @param {ASTNode} comment The comment to remove
121 * @returns {boolean} If the comment covers the entire line
123 function isFullLineComment(line, lineNumber, comment) {
124 const start = comment.loc.start,
125 end = comment.loc.end,
126 isFirstTokenOnLine = start.line === lineNumber && !line.slice(0, start.column).trim(),
127 isLastTokenOnLine = end.line === lineNumber && !line.slice(end.column).trim();
130 (start.line < lineNumber || isFirstTokenOnLine) &&
131 (end.line > lineNumber || isLastTokenOnLine);
135 * Identifies is a node is a FunctionExpression which is part of an IIFE
136 * @param {ASTNode} node Node to test
137 * @returns {boolean} True if it's an IIFE
139 function isIIFE(node) {
140 return node.type === "FunctionExpression" && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node;
144 * Identifies is a node is a FunctionExpression which is embedded within a MethodDefinition or Property
145 * @param {ASTNode} node Node to test
146 * @returns {boolean} True if it's a FunctionExpression embedded within a MethodDefinition or Property
148 function isEmbedded(node) {
152 if (node !== node.parent.value) {
155 if (node.parent.type === "MethodDefinition") {
158 if (node.parent.type === "Property") {
159 return node.parent.method === true || node.parent.kind === "get" || node.parent.kind === "set";
165 * Count the lines in the function
166 * @param {ASTNode} funcNode Function AST node
170 function processFunction(funcNode) {
171 const node = isEmbedded(funcNode) ? funcNode.parent : funcNode;
173 if (!IIFEs && isIIFE(node)) {
178 for (let i = node.loc.start.line - 1; i < node.loc.end.line; ++i) {
179 const line = lines[i];
182 if (commentLineNumbers.has(i + 1) && isFullLineComment(line, i + 1, commentLineNumbers.get(i + 1))) {
187 if (skipBlankLines) {
188 if (line.match(/^\s*$/u)) {
196 if (lineCount > maxLines) {
197 const name = lodash.upperFirst(astUtils.getFunctionNameWithKind(funcNode));
202 data: { name, lineCount, maxLines }
207 //--------------------------------------------------------------------------
209 //--------------------------------------------------------------------------
212 FunctionDeclaration: processFunction,
213 FunctionExpression: processFunction,
214 ArrowFunctionExpression: processFunction