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#
example.js4 problems
const nIdentifier name n is too short (< 2). = items.length;
const prices = items.map((xIdentifier name x is too short (< 2).) => x.price);
try {
save();
} catch (eIdentifier name e is too short (< 2).) {
report(e);
}
const point = { yIdentifier name y is too short (< 2).: 0 };example.jsNo problems
const count = items.length;
const prices = items.map((item) => item.price);
try {
save();
} catch (error) {
report(error);
}Type parameters aren't checked:
example.tsNo problems
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. |
example.jswith
{ "max": 20 }1 problemconst numberOfItemsInTheShoppingCartIdentifier name numberOfItemsInTheShoppingCart is too long (> 20). = cart.items.length;example.jswith
{ "exceptions": ["i"] }No problemsfor (let i = 0; i < items.length; i++) {
render(items[i]);
}example.jswith
{ "exceptionPatterns": ["^[xyz]$"] }No problemsfunction move(x, y, z) {
return [x + 1, y + 1, z + 1];
}properties#
example.jswith
{ "properties": "never" }No problemsconst point = { x: 0, y: 0 };
point.x = 10;Variables and class members are still checked:
example.jswith
{ "properties": "never" }2 problemsconst pIdentifier name p is too short (< 2). = { x: 0, y: 0 };
class Point {
xIdentifier name x is too short (< 2). = 0;
}