max-lines
Enforce a maximum number of lines per file
A file that keeps growing ends up doing many unrelated things, and finding anything in it means scrolling. This rule reports any file with more lines than the maximum, which is 300 by default.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"max-lines" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"max-lines" = ["error", {
max = 300,
skipBlankLines = false,
skipComments = false,
}]Examples#
With a maximum of 4, this 5-line file is reported:
example.jswith
41 problemimport { readFile } from "node:fs/promises";
const text = await readFile("notes.txt", "utf8");
console.log(text.split("\n").length);File has too many lines (7). Maximum allowed is 4.Not reported, with the work moved to another file:
example.jswith
4No problemsimport { countLines } from "./count-lines.js";
console.log(await countLines("notes.txt"));Options#
bunfig.toml
[lint.rules]
max-lines = ["error", { max = 500, skipBlankLines = true, skipComments = true }]| Option | Default | What it does |
|---|---|---|
max | 300 | The number of lines a file can have. |
skipBlankLines | false | Don't count lines that are empty or only have whitespace. |
skipComments | false | Don't count lines that only have a comment. A line with code and a comment still counts. |
A number alone is short for { "max": number }.
With skipBlankLines, the first example has 3 lines:
example.jswith
{ "max": 4, "skipBlankLines": true }No problemsimport { readFile } from "node:fs/promises";
const text = await readFile("notes.txt", "utf8");
console.log(text.split("\n").length);With skipComments, so does this one:
example.jswith
{ "max": 4, "skipComments": true }No problems// Prints the number of lines in notes.txt.
// Run it with: bun count.js
import { readFile } from "node:fs/promises";
const text = await readFile("notes.txt", "utf8");
console.log(text.split("\n").length);When not to use it#
Generated files, fixtures and big data tables are long for a good reason. Turn the rule off for them.