Back to Biomejs

noExcessiveLinesPerFile

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

latest16.5 KB
Original Source

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

<Tabs> <TabItem label="CSS" icon="seti:css"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.12` - Diagnostic Category: [`lint/nursery/noExcessiveLinesPerFile`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). ## How to configure ```json title="biome.json" { "linter": { "rules": { "nursery": { "noExcessiveLinesPerFile": "error" } } } }
## Description
Restrict the number of lines in a file.

Large files tend to do many things and can make it hard to follow what's going on.
This rule can help enforce a limit on the number of lines in a file.

## Examples

### Invalid

The following example will show a diagnostic when `maxLines` is set to 2:

```json title='biome.json'
{
	"linter": {
		"rules": {
			"nursery": {
				"noExcessiveLinesPerFile": {
					"options": {
						"maxLines": 2
					}
				}
			}
		}
	}
}

css
.a { color: red; }
.b { color: blue; }
.c { color: green; }
<pre class="language-text"><code class="language-text">code-block.css:1:1 <a href="https://biomejs.dev/linter/rules/no-excessive-lines-per-file">lint/nursery/noExcessiveLinesPerFile</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This file has too many lines (3). Maximum allowed is 2.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>.a &#123; color: red; &#125; <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;">&gt;</span></strong> <strong>2 │ </strong>.b &#123; color: blue; &#125; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong>.c &#123; color: green; &#125; <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>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider splitting this file into smaller files.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>

Valid

css
.a { color: red; }
.b { color: blue; }

Options

maxLines

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

Default: 300

skipBlankLines

When this option is set to true, blank lines are not counted towards the maximum line limit.

Default: false

</TabItem> <TabItem label="GraphQL" icon="seti:graphql"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.12` - Diagnostic Category: [`lint/nursery/noExcessiveLinesPerFile`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). ## How to configure ```json title="biome.json" { "linter": { "rules": { "nursery": { "noExcessiveLinesPerFile": "error" } } } }
## Description
Restrict the number of lines in a file.

Large files tend to do many things and can make it hard to follow what's going on.
This rule can help enforce a limit on the number of lines in a file.

## Examples

### Invalid

The following example will show a diagnostic when `maxLines` is set to 2:

```json title='biome.json'
{
	"linter": {
		"rules": {
			"nursery": {
				"noExcessiveLinesPerFile": {
					"options": {
						"maxLines": 2
					}
				}
			}
		}
	}
}

graphql
query Foo { id }
query Bar { id }
query Baz { id }
<pre class="language-text"><code class="language-text"></code></pre>

Valid

graphql
query Foo { id }
query Bar { id }

Options

maxLines

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

Default: 300

skipBlankLines

When this option is set to true, blank lines are not counted towards the maximum line limit.

Default: false

</TabItem> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.12` - Diagnostic Category: [`lint/nursery/noExcessiveLinesPerFile`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Inspired from [`max-lines`](https://eslint.org/docs/latest/rules/max-lines)

How to configure

json
{
	"linter": {
		"rules": {
			"nursery": {
				"noExcessiveLinesPerFile": "error"
			}
		}
	}
}

Description

Restrict the number of lines in a file.

This rule checks the number of lines in a file and reports a diagnostic if it exceeds a specified limit. Some people consider large files a code smell. Large files tend to do many things and can make it hard to follow what's going on. Many coding style guides dictate a limit of the number of lines that a file can comprise of. This rule can help enforce that style.

Examples

Invalid

The following example will show a diagnostic when maxLines is set to 2:

json
{
	"linter": {
		"rules": {
			"nursery": {
				"noExcessiveLinesPerFile": {
					"options": {
						"maxLines": 2
					}
				}
			}
		}
	}
}

js
const a = 1;
const b = 2;
const c = 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-file">lint/nursery/noExcessiveLinesPerFile</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This file has too many lines (3). Maximum allowed is 2.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const a = 1; <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;">&gt;</span></strong> <strong>2 │ </strong>const b = 2; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong>const c = 3; <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>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider splitting this file into smaller files.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>

Valid

js
const a = 1;
const b = 2;

Options

The following options are available:

maxLines

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

Default: 300

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

json
{
	"linter": {
		"rules": {
			"nursery": {
				"noExcessiveLinesPerFile": {
					"options": {
						"maxLines": 2
					}
				}
			}
		}
	}
}

js
const a = 1;
const b = 2;
const c = 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-file">lint/nursery/noExcessiveLinesPerFile</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This file has too many lines (3). Maximum allowed is 2.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const a = 1; <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;">&gt;</span></strong> <strong>2 │ </strong>const b = 2; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong>const c = 3; <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>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider splitting this file into smaller files.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>

skipBlankLines

When this option is set to true, blank lines 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: 3 and skipBlankLines: true, the following file will be considered valid even though it has 5 total lines, because only 3 lines contain code:

json
{
	"linter": {
		"rules": {
			"nursery": {
				"noExcessiveLinesPerFile": {
					"options": {
						"maxLines": 3,
						"skipBlankLines": true
					}
				}
			}
		}
	}
}

js
const a = 1;

const b = 2;

const c = 3;

Suppressions

If you need to exceed the line limit in a specific file, you can suppress this rule at the top of the file:

json
{
	"linter": {
		"rules": {
			"nursery": {
				"noExcessiveLinesPerFile": {
					"options": {
						"maxLines": 2
					}
				}
			}
		}
	}
}

js
// biome-ignore lint/nursery/noExcessiveLinesPerFile: generated file
const a = 1;
const b = 2;
const c = 3;
</TabItem> </Tabs>