id-length
Enforce minimum and maximum identifier lengths
Very short identifier names like e
, x
, _t
or very long ones like hashGeneratorResultOutputContainerObject
can make code harder to read and potentially less maintainable. To prevent this, one may enforce a minimum and/or maximum identifier length.
var x = 5; // too short; difficult to understand its purpose without context
Rule Details
This rule enforces a minimum and/or maximum identifier length convention.
This rule counts graphemes instead of using String length
.
Options
Examples of incorrect code for this rule with the default options:
/*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
var = 5;
obj. = document.body;
var foo = function () { };
try {
dangerousStuff();
} catch () {
// ignore as many do
}
var myObj = { : 1 };
() => { a * a };
class { }
class Foo { () {} }
class Bar { () {} }
class Baz { = 1 }
class Qux { = 1 }
function bar(...) { }
function baz([]) { }
var [] = arr;
var { prop: []} = {};
function qux({}) { }
var { } = {};
var { prop: } = {};
({ prop: obj. } = {});
Examples of correct code for this rule with the default options:
/*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
var num = 5;
function _f() { return 42; }
function _func() { return 42; }
obj.el = document.body;
var foo = function (evt) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
var myObj = { apple: 1 };
(num) => { num * num };
function bar(num = 0) { }
class MyClass { }
class Foo { method() {} }
class Bar { #method() {} }
class Baz { field = 1 }
class Qux { #field = 1 }
function baz(...args) { }
function qux([longName]) { }
var { prop } = {};
var { prop: [longName] } = {};
var [longName] = arr;
function foobar({ prop }) { }
function foobaz({ a: prop }) { }
var { prop } = {};
var { a: prop } = {};
({ prop: obj.longName } = {});
var data = { "x": 1 }; // excused because of quotes
data["y"] = 3; // excused because of calculated property access
This rule has an object option:
"min"
(default: 2) enforces a minimum identifier length"max"
(default: Infinity) enforces a maximum identifier length"properties": always
(default) enforces identifier length convention for property names"properties": never
ignores identifier length convention for property names"exceptions"
allows an array of specified identifier names"exceptionPatterns"
array of strings representing regular expression patterns, allows identifiers that match any of the patterns.
min
Examples of incorrect code for this rule with the { "min": 4 }
option:
/*eslint id-length: ["error", { "min": 4 }]*/
var = 5;
. = document.body;
function () { };
try {
dangerousStuff();
} catch () {
// ignore as many do
}
var myObj = { : 1 };
() => { val * val };
class { }
class { () {} }
function (...) { }
var { } = {};
var { prop: } = {};
var [] = arr;
var { prop: []} = {};
({ prop: . } = {});
Examples of correct code for this rule with the { "min": 4 }
option:
/*eslint id-length: ["error", { "min": 4 }]*/
var value = 5;
function func() { return 42; }
object.element = document.body;
var foobar = function (event) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
var myObj = { apple: 1 };
(value) => { value * value };
function foobaz(value = 0) { }
class MyClass { }
class Foobar { method() {} }
function barbaz(...args) { }
var { prop } = {};
var [longName] = foo;
var { a: [prop] } = {};
var { a: longName } = {};
({ prop: object.name } = {});
var data = { "x": 1 }; // excused because of quotes
data["y"] = 3; // excused because of calculated property access
max
Examples of incorrect code for this rule with the { "max": 10 }
option:
/*eslint id-length: ["error", { "max": 10 }]*/
var = 5;
function () { return 42; }
obj. = document.body;
var foo = function () { /* do stuff */ };
try {
dangerousStuff();
} catch () {
// ignore as many do
}
() => { return !reallyLongArgName; };
var [] = arr;
Examples of correct code for this rule with the { "max": 10 }
option:
/*eslint id-length: ["error", { "max": 10 }]*/
var varName = 5;
function funcName() { return 42; }
obj.propName = document.body;
var foo = function (arg) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
(arg) => { return !arg; };
var [first] = arr;
properties
Examples of correct code for this rule with the { "properties": "never" }
option:
/*eslint id-length: ["error", { "properties": "never" }]*/
var myObj = { a: 1 };
({ a: obj.x.y.z } = {});
({ prop: obj.i } = {});
exceptions
Examples of additional correct code for this rule with the { "exceptions": ["x", "y", "z", "ζ"] }
option:
/*eslint id-length: ["error", { "exceptions": ["x", "y", "z", "ζ"] }]*/
var x = 5;
function y() { return 42; }
obj.x = document.body;
var foo = function (x) { /* do stuff */ };
try {
dangerousStuff();
} catch (x) {
// ignore as many do
}
(x) => { return x * x; };
var [x] = arr;
const { z } = foo;
const { a: ζ } = foo;
exceptionPatterns
Examples of additional correct code for this rule with the { "exceptionPatterns": ["E|S", "[x-z]"] }
option:
/*eslint id-length: ["error", { "exceptionPatterns": ["E|S", "[x-z]"] }]*/
var E = 5;
function S() { return 42; }
obj.x = document.body;
var foo = function (x) { /* do stuff */ };
try {
dangerousStuff();
} catch (x) {
// ignore as many do
}
(y) => {return y * y};
var [E] = arr;
const { y } = foo;
const { a: z } = foo;
Related Rules
Version
This rule was introduced in ESLint v1.0.0.