id-length

强制最小和最大标识符长度

很短的标识符名称(如 ex_t)或很长的标识符名称(如 hashGeneratorResultOutputContainerObject)会使代码更难阅读并且可能更难维护。为了防止这种情况,可以强制执行最小和/或最大标识符长度。

var x = 5; // too short; difficult to understand its purpose without context

规则详情

此规则强制执行最小和/或最大标识符长度约定。

此规则计算 graphemes 而不是使用 String length

选项

具有默认选项的此规则的错误代码示例:

/*eslint id-length: "error"*/     // default is minimum 2-chars ({ "min": 2 })
/*eslint-env es6*/

var x = 5;
obj.e = document.body;
var foo = function (e) { };
try {
    dangerousStuff();
} catch (e) {
    // ignore as many do
}
var myObj = { a: 1 };
(a) => { a * a };
class x { }
class Foo { x() {} }
class Foo { #x() {} }
class Foo { x = 1 }
class Foo { #x = 1 }
function foo(...x) { }
function foo([x]) { }
var [x] = arr;
var { prop: [x]} = {};
function foo({x}) { }
var { x } = {};
var { prop: a} = {};
({ prop: obj.x } = {});

KTsoa0toLm5KcPzNEsDTZtOKJITyaxpnTponqgo2dmUqzPwYSJr6htkbXXU90M2SR6qAC6K4LLZwV2DKChvPmA==

/*eslint id-length: "error"*/     // default is minimum 2-chars ({ "min": 2 })
/*eslint-env es6*/

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 foo(num = 0) { }
class MyClass { }
class Foo { method() {} }
class Foo { #method() {} }
class Foo { field = 1 }
class Foo { #field = 1 }
function foo(...args) { }
function foo([longName]) { }
var { prop } = {};
var { prop: [longName] } = {};
var [longName] = arr;
function foo({ prop }) { }
function foo({ 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

zaHvPcgsugX4MUASJPYCXtmNqHTCIN7Divdsz+quHFroUBdd/PBMuxfBLX/bmPvZ

    ReqM2eCmWkVnFSqTqsKgzlPwCQ1qQSdhzMnR+GVnTnwgrrCCu7r7eUJMnJDGlQNU7yd5KB0TragB2M3kTQIg7bsBGRYcUC8i4u4UoOB3EgLrQyawq8eLn6XMPcmBzQXkkLprbtOTQZu6UFJOAdEXnbXrRuMAb3EJZ76jbwg+Y8BfAWNbX28Jq01SbXjAH26FKeXes+GErwkKczh1R0Sbgcm46SWG6Kh9TnyRTdgR8qiCDgPSuCCIxp24qp7I6xbUh1wot+uWVt3dlZ93YLixcQGYuP2pHzUy5YnScGSEPCHqFvWBcz6iZ+yYMPB5a7S1v+OyjA2Nduk/OGnQlOg7eQLCoGbnkPl2TwZOvV0wX/zj0o9WNOQreGNtM8fq+kIXvvlvKDCnfhkDTPGnBdyayiD2KYu5+4rflTWBBbqGMJa5k0K/9KwcJUlWb9u0qNiqCuMPD2F2ZJg/Lg7qgUSB3Q3KImwElZtaBQXSz+1/fHNhvoW9zQne6NKXsQLKOt5VfGHF6ipS7IZ1WX4FrbxVvavPklKZNxCCXGtl0i+BK/Xm1/zRlyrWKF2z4v3qbAQSq+mbCY+e9dqgrDrJM2xZSQQnVlxngxFuLddXrZEdXDcrEQSfetmQV7YMfIPQ53XwZz9DVlm4+46OruO17DWw3RakkVr5alWkNK2XMowZNkQDqWi9VGP6IEgFkPdjSJdChyvlnLYVCMHEBrH/vAAOvr46yttdPsiRzrR8ORmsAPM=

min

Bv4sMSnguAQJ4kJJZCKoglS20nScBOwEBql8q7hmHtGemV61HLfPtxgBDWkSLpQWoJpUFxUo7YBiLTd0ahQgFHfiIBH5onV8b2hb+7TR9qQ=

/*eslint id-length: ["error", { "min": 4 }]*/
/*eslint-env es6*/

var val = 5;
obj.e = document.body;
function foo (e) { };
try {
    dangerousStuff();
} catch (e) {
    // ignore as many do
}
var myObj = { a: 1 };
(val) => { val * val };
class x { }
class Foo { x() {} }
function foo(...x) { }
var { x } = {};
var { prop: a} = {};
var [x] = arr;
var { prop: [x]} = {};
({ prop: obj.x } = {});

Bv4sMSnguAQJ4kJJZCKoglS20nScBOwEBql8q7hmHtGemV61HLfPtxgBDWkSLpQW+zIFAA8XEcOve0R38S+rgseCcT3j6qLyZh8nYuUoGzo=

/*eslint id-length: ["error", { "min": 4 }]*/
/*eslint-env es6*/

var value = 5;
function func() { return 42; }
obj.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 foobar(value = 0) { }
class MyClass { }
class Foobar { method() {} }
function foobar(...args) { }
var { prop } = {};
var [longName] = foo;
var { a: [prop] } = {};
var { a: longName } = {};
({ prop: obj.name } = {});
var data = { "x": 1 };  // excused because of quotes
data["y"] = 3;  // excused because of calculated property access

max

Bv4sMSnguAQJ4kJJZCKogsBMFwwr3KqlcAXXjYjXOVF3igpAvNZ/1PtXq6f3qkxgPihfim+19ZXIwAWWJvWs7fsc/e9ySslzjg2Z6/lfYX0=

/*eslint id-length: ["error", { "max": 10 }]*/
/*eslint-env es6*/

var reallyLongVarName = 5;
function reallyLongFuncName() { return 42; }
obj.reallyLongPropName = document.body;
var foo = function (reallyLongArgName) { /* do stuff */ };
try {
    dangerousStuff();
} catch (reallyLongErrorName) {
    // ignore as many do
}
(reallyLongArgName) => { return !reallyLongArgName; };
var [reallyLongFirstElementName] = arr;

Bv4sMSnguAQJ4kJJZCKogsBMFwwr3KqlcAXXjYjXOVF3igpAvNZ/1PtXq6f3qkxge1fAef+pH+YZ+qMYvxbjBchAHEa+L0i+fYGyCHXJTvY=

/*eslint id-length: ["error", { "max": 10 }]*/
/*eslint-env es6*/

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

Bv4sMSnguAQJ4kJJZCKogtwzj1Maqeib3TtnxWSyLNn4wLLJgXMEaQ4DqX4s2qCv5ru0oD94ioblLD0GmJdtJjOmbj9HKzQm/lAwM4XULV1qBtd+YJGpaCpRzaJEeM4Y

/*eslint id-length: ["error", { "properties": "never" }]*/
/*eslint-env es6*/

var myObj = { a: 1 };
({ a: obj.x.y.z } = {});
({ prop: obj.i } = {});

exceptions

Bv4sMSnguAQJ4kJJZCKogsQGF7Jj4w4AHqWbSJMifCzPsQIFZhTcDXwCm4CU5Mce3Y92gHkea7L0Xjgldn2gEvb9UAz/LXhjxuHCn/+jBsGWI4i6WhTy3JswM+RKgonQ

/*eslint id-length: ["error", { "exceptions": ["x"] }]*/
/*eslint-env es6*/

var x = 5;
function x() { 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 { x } = foo;
const { a: x } = foo;

exceptionPatterns

Bv4sMSnguAQJ4kJJZCKogsQGF7Jj4w4AHqWbSJMifCwjPKG2dSpYVk05kKeQ4IefGzHYgm+tEP494903b7DoJjcrSwMaZE90Z7ET62CPmTPfjiCH3kJSog3bhtMI72onUfpdVs6HXMNv/I0YxlFFRA==

/*eslint id-length: ["error", { "exceptionPatterns": ["E|S", "[x-z]"] }]*/
/*eslint-env es6*/

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;