noExcessiveStatementsPerFunction (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/noExcessiveStatementsPerFunction - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
max-statements
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noExcessiveStatementsPerFunction": "error" } } }}Description
Section titled “Description”Enforce a maximum number of statements allowed in a function.
Functions with many statements tend to do too many things at once, which makes them harder to read, test, and maintain.
The rule checks every kind of function: function declarations and expressions, arrow functions, methods, getters, setters, and constructors.
The rule counts every statement in the function body, including the statements
inside braces ({ }), such as the body of an if statement, a loop, or a try/catch.
A statement without braces, such as b in if (a) b;, or a statement in a switch case,
doesn’t add to the count.
The statements of a nested function count towards the nested function only.
Class static blocks (static { } inside a class) aren’t checked,
and their statements don’t count towards the function that contains the class.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function foo() { const foo1 = 1; const foo2 = 2; const foo3 = 3; const foo4 = 4; const foo5 = 5; const foo6 = 6; const foo7 = 7; const foo8 = 8; const foo9 = 9; const foo10 = 10; const foo11 = 11;}code-block.js:1:1 lint/nursery/noExcessiveStatementsPerFunction ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function has too many statements (11). Maximum allowed is 10.
> 1 │ function foo() {
│ ^^^^^^^^^^^^^^
2 │ const foo1 = 1;
3 │ const foo2 = 2;
ℹ Functions with many statements are harder to read, test, and maintain.
ℹ Consider splitting this function into smaller functions.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function foo(items) { const result = []; for (const item of items) { if (item.enabled) { const value = item.value; const label = item.label; const description = item.description; result.push({ value, label, description }); } else { const fallback = item.fallback; const reason = item.reason; result.push({ fallback, reason }); } } return result;}code-block.js:1:1 lint/nursery/noExcessiveStatementsPerFunction ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function has too many statements (11). Maximum allowed is 10.
> 1 │ function foo(items) {
│ ^^^^^^^^^^^^^^^^^^^
2 │ const result = [];
3 │ for (const item of items) {
ℹ Functions with many statements are harder to read, test, and maintain.
ℹ Consider splitting this function into smaller functions.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function foo() { const foo1 = 1; const foo2 = 2; const foo3 = 3; const foo4 = 4; const foo5 = 5; const foo6 = 6; const foo7 = 7; const foo8 = 8; const foo9 = 9; return function () { // The statements of this function don't count towards `foo`. let bar; let baz; return 42; };}Options
Section titled “Options”The maximum number of statements allowed in a function.
Default: 10
{ "linter": { "rules": { "nursery": { "noExcessiveStatementsPerFunction": { "level": "on", "options": { "max": 2 } } } } }}Invalid
Section titled “Invalid”function foo() { const a = 1; const b = 2; return a + b;}code-block.js:1:1 lint/nursery/noExcessiveStatementsPerFunction ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function has too many statements (3). Maximum allowed is 2.
> 1 │ function foo() {
│ ^^^^^^^^^^^^^^
2 │ const a = 1;
3 │ const b = 2;
ℹ Functions with many statements are harder to read, test, and maintain.
ℹ Consider splitting this function into smaller functions.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function foo() { const a = 1; return a + 2;}ignoreTopLevelFunctions
Section titled “ignoreTopLevelFunctions”When set to true, a top-level function (a function that isn’t nested in another function)
isn’t checked, as long as it’s the only top-level function in the file.
This is useful for files whose code is entirely wrapped in a single function,
such as (function () { ... })() or define(function () { ... }).
If the file contains several top-level functions, all of them are checked.
Default: false
{ "linter": { "rules": { "nursery": { "noExcessiveStatementsPerFunction": { "level": "on", "options": { "max": 2, "ignoreTopLevelFunctions": true } } } } }}Invalid
Section titled “Invalid”(function () { const a = 1; const b = 2; const c = 3;})();
(function () { const a = 1;})();code-block.js:1:2 lint/nursery/noExcessiveStatementsPerFunction ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This function has too many statements (3). Maximum allowed is 2.
> 1 │ (function () {
│ ^^^^^^^^^^^
2 │ const a = 1;
3 │ const b = 2;
ℹ Functions with many statements are harder to read, test, and maintain.
ℹ Consider splitting this function into smaller functions.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
(function () { const a = 1; const b = 2; const c = 3;})();Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.