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");
12 const { upperCaseFirst } = require("../shared/string-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const OPTIONS_SCHEMA = {
35 additionalProperties: false
38 const OPTIONS_OR_INTEGER_SCHEMA = {
49 * Given a list of comment nodes, return a map with numeric keys (source code line numbers) and comment token values.
50 * @param {Array} comments An array of comment nodes.
51 * @returns {Map.<string,Node>} A map with numeric keys (source code line numbers) and comment token values.
53 function getCommentLineNumbers(comments) {
54 const map = new Map();
56 comments.forEach(comment => {
57 for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) {
64 //------------------------------------------------------------------------------
66 //------------------------------------------------------------------------------
73 description: "enforce a maximum number of lines of code in a function",
74 category: "Stylistic Issues",
76 url: "https://eslint.org/docs/rules/max-lines-per-function"
80 OPTIONS_OR_INTEGER_SCHEMA
83 exceed: "{{name}} has too many lines ({{lineCount}}). Maximum allowed is {{maxLines}}."
88 const sourceCode = context.getSourceCode();
89 const lines = sourceCode.lines;
91 const option = context.options[0];
93 let skipComments = false;
94 let skipBlankLines = false;
97 if (typeof option === "object") {
98 maxLines = typeof option.max === "number" ? option.max : 50;
99 skipComments = !!option.skipComments;
100 skipBlankLines = !!option.skipBlankLines;
101 IIFEs = !!option.IIFEs;
102 } else if (typeof option === "number") {
106 const commentLineNumbers = getCommentLineNumbers(sourceCode.getAllComments());
108 //--------------------------------------------------------------------------
110 //--------------------------------------------------------------------------
113 * Tells if a comment encompasses the entire line.
114 * @param {string} line The source line with a trailing comment
115 * @param {number} lineNumber The one-indexed line number this is on
116 * @param {ASTNode} comment The comment to remove
117 * @returns {boolean} If the comment covers the entire line
119 function isFullLineComment(line, lineNumber, comment) {
120 const start = comment.loc.start,
121 end = comment.loc.end,
122 isFirstTokenOnLine = start.line === lineNumber && !line.slice(0, start.column).trim(),
123 isLastTokenOnLine = end.line === lineNumber && !line.slice(end.column).trim();
126 (start.line < lineNumber || isFirstTokenOnLine) &&
127 (end.line > lineNumber || isLastTokenOnLine);
131 * Identifies is a node is a FunctionExpression which is part of an IIFE
132 * @param {ASTNode} node Node to test
133 * @returns {boolean} True if it's an IIFE
135 function isIIFE(node) {
136 return (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node;
140 * Identifies is a node is a FunctionExpression which is embedded within a MethodDefinition or Property
141 * @param {ASTNode} node Node to test
142 * @returns {boolean} True if it's a FunctionExpression embedded within a MethodDefinition or Property
144 function isEmbedded(node) {
148 if (node !== node.parent.value) {
151 if (node.parent.type === "MethodDefinition") {
154 if (node.parent.type === "Property") {
155 return node.parent.method === true || node.parent.kind === "get" || node.parent.kind === "set";
161 * Count the lines in the function
162 * @param {ASTNode} funcNode Function AST node
166 function processFunction(funcNode) {
167 const node = isEmbedded(funcNode) ? funcNode.parent : funcNode;
169 if (!IIFEs && isIIFE(node)) {
174 for (let i = node.loc.start.line - 1; i < node.loc.end.line; ++i) {
175 const line = lines[i];
178 if (commentLineNumbers.has(i + 1) && isFullLineComment(line, i + 1, commentLineNumbers.get(i + 1))) {
183 if (skipBlankLines) {
184 if (line.match(/^\s*$/u)) {
192 if (lineCount > maxLines) {
193 const name = upperCaseFirst(astUtils.getFunctionNameWithKind(funcNode));
198 data: { name, lineCount, maxLines }
203 //--------------------------------------------------------------------------
205 //--------------------------------------------------------------------------
208 FunctionDeclaration: processFunction,
209 FunctionExpression: processFunction,
210 ArrowFunctionExpression: processFunction