AphrodyBun GitHub

Runtime docs · Linter · Rules

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:

import { readFile } from "node:fs/promises";

const text = await readFile("notes.txt", "utf8");

console.log(text.split("\n").length);

Not reported, with the work moved to another file:

import { 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 }]
OptionDefaultWhat it does
max300The number of lines a file can have.
skipBlankLinesfalseDon't count lines that are empty or only have whitespace.
skipCommentsfalseDon'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:

import { readFile } from "node:fs/promises";

const text = await readFile("notes.txt", "utf8");

console.log(text.split("\n").length);

With skipComments, so does this one:

// 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.