id-length
强制执行标识符长度的最小值和最大值
非常短的标识符名称(如 e
、x
、_t
)或非常长的名称(如 hashGeneratorResultOutputContainerObject
)会使代码更难阅读,并可能降低可维护性。为了防止这种情况,可以强制执行标识符长度的最小值和/或最大值。
var x = 5; // too short; difficult to understand its purpose without context
规则详细信息
此规则强制执行标识符长度的最小值和/或最大值约定。
此规则计算 图符,而不是使用 String length
。
选项
此规则使用默认选项的不正确代码示例
在游乐场中打开
/*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. } = {});
此规则使用默认选项的正确代码示例
在游乐场中打开
/*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
此规则具有对象选项
"min"
(默认值:2)强制执行标识符长度的最小值"max"
(默认值:Infinity)强制执行标识符长度的最大值"properties": always
(默认值)强制执行属性名称的标识符长度约定"properties": never
忽略属性名称的标识符长度约定"exceptions"
允许指定标识符名称的数组"exceptionPatterns"
是表示正则表达式模式的字符串数组,允许与任何模式匹配的标识符。
min
此规则使用 { "min": 4 }
选项的不正确代码示例
在游乐场中打开
/*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: . } = {});
此规则使用 { "min": 4 }
选项的正确代码示例
在游乐场中打开
/*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
此规则使用 { "max": 10 }
选项的不正确代码示例
在游乐场中打开
/*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;
此规则使用 { "max": 10 }
选项的正确代码示例
在游乐场中打开
/*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
此规则使用 { "properties": "never" }
选项的正确代码示例
在游乐场中打开
/*eslint id-length: ["error", { "properties": "never" }]*/
var myObj = { a: 1 };
({ a: obj.x.y.z } = {});
({ prop: obj.i } = {});
exceptions
此规则使用 { "exceptions": ["x", "y", "z", "ζ"] }
选项的附加正确代码示例
在游乐场中打开
/*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
此规则使用 { "exceptionPatterns": ["E|S", "[x-z]"] }
选项的附加正确代码示例
在游乐场中打开
/*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;
相关规则
版本
此规则是在 ESLint v1.0.0 中引入的。