Pular para o conteúdo

noExcessiveLinesPerFunction

Este conteúdo não está disponível em sua língua ainda.

Restrict the number of lines of code in a function.

This rule checks the number of lines in a function body and reports a diagnostic if it exceeds a specified limit. Remember that this rule only counts the lines of code in the function body, not the entire function declaration. Some people consider large functions a code smell. Large functions tend to do a lot of things and can make it hard following what’s going on. Many coding style guides dictate a limit of the number of lines that a function can comprise of. This rule can help enforce that style.

The following example will show diagnostic when you set the maxLines limit to 3, however the default value is 50.

function foo () {
const x = 0;
const y = 1;
const z = 2;
return x + y + z;
};
function foo () {
const x = 0;
const y = 1;
};

The rule supports the following options:

{
"options": {
"maxLines": 50,
"skipBlankLines": false,
"skipIifes": false
}
}

This option sets the maximum number of lines allowed in a function body. If the function body exceeds this limit, a diagnostic will be reported.

Default: 50

When maxLines: 2, the following function will be considered invalid:

{
"options": {
"maxLines": 2
}
}
function example() {
const a = 1; // 1
const b = 2; // 2
const c = 3; // 3
};

When this options is set to true, blank lines in the function body are not counted towards the maximum line limit. This means that only lines with actual code or comments will be counted.

Default: false

When maxLines: 2 and skipBlankLines: true, the following function will be considered valid:

{
"options": {
"maxLines": 2,
"skipBlankLines": true
}
}
function example() {
const a = 1; // 1
// not counted
const b = 2; // 2
// not counted
};

When this option is set to true, Immediately Invoked Function Expressions (IIFEs) are not checked for the maximum line limit.

Default: false

When maxLines: 2 and skipIifes: true, the following IIFE will be considered valid even though its body has 3 lines:

{
"options": {
"maxLines": 2,
"skipIifes": true
}
}
(() => {
const a = 1; // 1
const b = 2; // 2
const c = 3; // 3
})();
biome.json
{
"linter": {
"rules": {
"nursery": {
"noExcessiveLinesPerFunction": "error"
}
}
}
}