Linter · Rules
id-length
Enforce a minimum and a maximum length for identifier names
A one-letter name such as e, n or x sends you back to the declaration to find out what it holds. A very long name makes every line that uses it hard to scan.
The rule checks a name where your code introduces it: variables, parameters, functions, classes, imports, object keys and class members. By default a name needs at least 2 characters, and there's no maximum.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"id-length" = "error" # or "warn"
Every option, set to its default. The ones that are commented out are unset by default.
bunfig.toml
[lint.rules]
"id-length" = ["error", {
min = 2,
# max = <number>,
properties = "always", # "always" or "never"
exceptions = [], # list of strings
exceptionPatterns = [], # list of strings
}]
Examples
Reported:
const n = items.length;
const prices = items.map((x) => x.price);
try {
save();
} catch (e) {
report(e);
}
const point = { y: 0 };
Not reported:
const count = items.length;
const prices = items.map((item) => item.price);
try {
save();
} catch (error) {
report(error);
}
Type parameters aren't checked:
function first<T>(items: T[]): T {
return items[0];
}
Options
bunfig.toml
[lint.rules]
id-length = ["error", { min = 3, max = 30, exceptions = ["i", "id"] }]
| Option | Default | What it does |
|---|---|---|
min | 2 | The shortest name allowed. |
max | none | The longest name allowed. |
properties | "always" | "never" stops checking object keys and the properties you assign to. |
exceptions | [] | Names that are always allowed. |
exceptionPatterns | [] | Regular expressions. A name that matches one is always allowed. |
checkGeneric | true | Only for an oxlint config, where the rule also checks type parameters such as the T in Box<T>. false stops checking them. bunfig.toml rejects this option. |
const numberOfItemsInTheShoppingCart = cart.items.length;
for (let i = 0; i < items.length; i++) {
render(items[i]);
}
function move(x, y, z) {
return [x + 1, y + 1, z + 1];
}
properties
const point = { x: 0, y: 0 };
point.x = 10;
Variables and class members are still checked:
const p = { x: 0, y: 0 };
class Point {
x = 0;
}