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();
57 comments
.forEach(comment
=> {
58 for (let i
= comment
.loc
.start
.line
; i
<= comment
.loc
.end
.line
; i
++) {
65 //------------------------------------------------------------------------------
67 //------------------------------------------------------------------------------
74 description
: "enforce a maximum number of line of code in a function",
75 category
: "Stylistic Issues",
77 url
: "https://eslint.org/docs/rules/max-lines-per-function"
81 OPTIONS_OR_INTEGER_SCHEMA
84 exceed
: "{{name}} has too many lines ({{lineCount}}). Maximum allowed is {{maxLines}}."
89 const sourceCode
= context
.getSourceCode();
90 const lines
= sourceCode
.lines
;
92 const option
= context
.options
[0];
94 let skipComments
= false;
95 let skipBlankLines
= false;
98 if (typeof option
=== "object") {
99 maxLines
= typeof option
.max
=== "number" ? option
.max
: 50;
100 skipComments
= !!option
.skipComments
;
101 skipBlankLines
= !!option
.skipBlankLines
;
102 IIFEs
= !!option
.IIFEs
;
103 } else if (typeof option
=== "number") {
107 const commentLineNumbers
= getCommentLineNumbers(sourceCode
.getAllComments());
109 //--------------------------------------------------------------------------
111 //--------------------------------------------------------------------------
114 * Tells if a comment encompasses the entire line.
115 * @param {string} line The source line with a trailing comment
116 * @param {number} lineNumber The one-indexed line number this is on
117 * @param {ASTNode} comment The comment to remove
118 * @returns {boolean} If the comment covers the entire line
120 function isFullLineComment(line
, lineNumber
, comment
) {
121 const start
= comment
.loc
.start
,
122 end
= comment
.loc
.end
,
123 isFirstTokenOnLine
= start
.line
=== lineNumber
&& !line
.slice(0, start
.column
).trim(),
124 isLastTokenOnLine
= end
.line
=== lineNumber
&& !line
.slice(end
.column
).trim();
127 (start
.line
< lineNumber
|| isFirstTokenOnLine
) &&
128 (end
.line
> lineNumber
|| isLastTokenOnLine
);
132 * Identifies is a node is a FunctionExpression which is part of an IIFE
133 * @param {ASTNode} node Node to test
134 * @returns {boolean} True if it's an IIFE
136 function isIIFE(node
) {
137 return (node
.type
=== "FunctionExpression" || node
.type
=== "ArrowFunctionExpression") && node
.parent
&& node
.parent
.type
=== "CallExpression" && node
.parent
.callee
=== node
;
141 * Identifies is a node is a FunctionExpression which is embedded within a MethodDefinition or Property
142 * @param {ASTNode} node Node to test
143 * @returns {boolean} True if it's a FunctionExpression embedded within a MethodDefinition or Property
145 function isEmbedded(node
) {
149 if (node
!== node
.parent
.value
) {
152 if (node
.parent
.type
=== "MethodDefinition") {
155 if (node
.parent
.type
=== "Property") {
156 return node
.parent
.method
=== true || node
.parent
.kind
=== "get" || node
.parent
.kind
=== "set";
162 * Count the lines in the function
163 * @param {ASTNode} funcNode Function AST node
167 function processFunction(funcNode
) {
168 const node
= isEmbedded(funcNode
) ? funcNode
.parent
: funcNode
;
170 if (!IIFEs
&& isIIFE(node
)) {
175 for (let i
= node
.loc
.start
.line
- 1; i
< node
.loc
.end
.line
; ++i
) {
176 const line
= lines
[i
];
179 if (commentLineNumbers
.has(i
+ 1) && isFullLineComment(line
, i
+ 1, commentLineNumbers
.get(i
+ 1))) {
184 if (skipBlankLines
) {
185 if (line
.match(/^\s*$/u)) {
193 if (lineCount
> maxLines
) {
194 const name
= lodash
.upperFirst(astUtils
.getFunctionNameWithKind(funcNode
));
199 data
: { name
, lineCount
, maxLines
}
204 //--------------------------------------------------------------------------
206 //--------------------------------------------------------------------------
209 FunctionDeclaration
: processFunction
,
210 FunctionExpression
: processFunction
,
211 ArrowFunctionExpression
: processFunction