eslint/max-lines-per-function Pedantic β
What it does β
Enforce a maximum number of lines of code in a function. This rule ensures that functions do not exceed a specified line count, promoting smaller, more focused functions that are easier to maintain and understand.
Why is this bad? β
Some people consider large functions a code smell. Large functions tend to do a lot of things and can make it hard to follow whatβs going on. Many coding style guides dictate a limit to the number of lines that a function can comprise of. This rule can help enforce that style.
Examples β
Examples of incorrect code for this rule with a particular max value:
/* { "eslint/max-lines-per-function": ["error", 2] } */
function foo() {
const x = 0;
}
/* { "eslint/max-lines-per-function": ["error", 4] } */
function foo() {
// a comment followed by a blank line
const x = 0;
}
Examples of correct code for this rule with a particular max value:
/* { "eslint/max-lines-per-function": ["error", 3] } */
function foo() {
const x = 0;
}
/* { "eslint/max-lines-per-function": ["error", 5] } */
function foo() {
// a comment followed by a blank line
const x = 0;
}
Options β
max β
The max
enforces a maximum number of lines in a function.
skipBlankLines β
The skipBlankLines
ignore lines made up purely of whitespace.
skipComments β
The skipComments
ignore lines containing just comments.
IIFEs β
The IIFEs
option controls whether IIFEs are included in the line count. By default, IIFEs are not considered, but when set to true
, they will be included in the line count for the function.
Example:
"eslint/max-lines-per-function": [
"error",
{
"max": 50,
"skipBlankLines": false,
"skipComments": false,
"IIFEs": false
}
]
How to use β
To enable this rule in the CLI or using the config file, you can use:
oxlint --deny max-lines-per-function
{
"rules": {
"max-lines-per-function": "error"
}
}