absolute/min-var-length
Disallow variable names shorter than the configured minimum length, unless an outer variable with a longer name starting with the same characters exists.
#Rule Details
Disallows variable names shorter than minLength, which pushes toward names that read on their own. It covers declarators (including destructured and defaulted bindings), function and arrow parameters, and catch clause parameters.
A short name is allowed when it is a clear abbreviation of an outer name — an outer identifier that is at least minLength long, longer than the short name, and starts with the same characters (so i is fine inside a scope that has items). You can also exempt exact names with allowedVars.
#Examples
// options: [{ minLength: 3 }]
const x = 1;
function fn(a) { return a; }
const { q } = query;
const [p, c] = pair; // two reports
try {} catch (e) {} // 'e' is too shortAbbreviation of an outer name
// options: [{ minLength: 3 }]
const total = 0;
function process(items: number[]) {
return items.map((i) => i * 2); // 'i' allowed: outer 'items' starts with "i"
}// options: [{ minLength: 3, allowedVars: ['id'] }]
const id = '42'; // exempted by allowedVars#Options
This rule accepts the following options:
| Option | Type | Default | Description |
|---|---|---|---|
minLength | number | 1 | Minimum allowed identifier length. Names shorter than this are reported unless exempted. |
allowedVars | string[] | [] | Exact identifier names that are always permitted regardless of length. |
{
rules: {
'absolute/min-var-length': [
'error',
{ minLength: 3, allowedVars: ['_', 'id', 't'] }
]
}
}#Related Rules
#When Not To Use It
Math-heavy code, short-lived loop indices, and well-known conventions (x/y coordinates, e for events) can read better short. Use allowedVars to carve out exceptions, or keep minLength at 1 if terse names are the house style.