Back to Biomejs

noExcessiveLinesPerFunction

src/content/docs/linter/rules/no-excessive-lines-per-function.mdx

latest6.3 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/complexity/noExcessiveLinesPerFunction`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Inspired from [`max-lines-per-function`](https://eslint.org/docs/latest/rules/max-lines-per-function)

How to configure

json
{
	"linter": {
		"rules": {
			"complexity": {
				"noExcessiveLinesPerFunction": "error"
			}
		}
	}
}

Description

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.

Examples

Invalid

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

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

Valid

js
 function foo () {
    const x = 0;
    const y = 1;
};

Options

The rule supports the following options:

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

maxLines

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:

json
{
	"linter": {
		"rules": {
			"complexity": {
				"noExcessiveLinesPerFunction": {
					"options": {
						"maxLines": 2
					}
				}
			}
		}
	}
}

js
function example() {
 const a = 1; // 1
 const b = 2; // 2
 const c = 3; // 3
};
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-excessive-lines-per-function">lint/complexity/noExcessiveLinesPerFunction</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This function has too many lines (3). Maximum allowed is 2.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>function example() &#123; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> const a = 1; // 1 <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> const b = 2; // 2 <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> const c = 3; // 3 <strong><span style="color: Tomato;">&gt;</span></strong> <strong>5 │ </strong>&#125;; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong> <strong>6 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider refactoring this function to split it into smaller functions.</span> </code></pre>

skipBlankLines

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:

json
{
	"linter": {
		"rules": {
			"complexity": {
				"noExcessiveLinesPerFunction": {
					"options": {
						"maxLines": 2,
						"skipBlankLines": true
					}
				}
			}
		}
	}
}

js
function example() {
 const a = 1; // 1
 // not counted
 const b = 2; // 2
 // not counted
};

skipIifes

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:

json
{
	"linter": {
		"rules": {
			"complexity": {
				"noExcessiveLinesPerFunction": {
					"options": {
						"maxLines": 2,
						"skipIifes": true
					}
				}
			}
		}
	}
}

js
(() => {
 const a = 1; // 1
 const b = 2; // 2
 const c = 3; // 3
})();
</TabItem> </Tabs>