AbsoluteJS

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.

Code QualityProblem
View on GitHub

#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

Incorrect
TS
// 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 short
Correct

Abbreviation of an outer name

TS
// options: [{ minLength: 3 }]
const total = 0;
function process(items: number[]) {
	return items.map((i) => i * 2); // 'i' allowed: outer 'items' starts with "i"
}
TS
// options: [{ minLength: 3, allowedVars: ['id'] }]
const id = '42'; // exempted by allowedVars

#Options

This rule accepts the following options:

OptionTypeDefaultDescription
minLengthnumber1Minimum allowed identifier length. Names shorter than this are reported unless exempted.
allowedVarsstring[][]Exact identifier names that are always permitted regardless of length.
JS
{
	rules: {
		'absolute/min-var-length': [
			'error',
			{ minLength: 3, allowedVars: ['_', 'id', 't'] }
		]
	}
}

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

#Resources