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.
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 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;
}
{ type: number, default: 50 }
The max
enforces a maximum number of lines in a function.
{ type: boolean, default: false }
The skipBlankLines
ignore lines made up purely of whitespace.
{ type: boolean, default: false }
The skipComments
ignore lines containing just comments.
{ type: boolean, default: false }
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
}
]
To enable this rule in the CLI or using the config file, you can use:
::: code-group
oxlint --deny max-lines-per-function
{
"rules": {
"max-lines-per-function": "error"
}
}
:::