id-length
强制最小和最大标识符长度
❄️ 已冻结
此规则目前已冻结,不接受功能请求。
非常短的标识符名称(如 e
、x
、_t
)或非常长的标识符名称(如 hashGeneratorResultOutputContainerObject
)会使代码更难阅读,并可能降低可维护性。 为了防止这种情况,可以强制执行最小和/或最大标识符长度。
const x = 5; // too short; difficult to understand its purpose without context
规则详情
此规则强制执行最小和/或最大标识符长度约定。
此规则计算字形,而不是使用 String length
。
选项
使用默认选项时,此规则的错误代码示例
在 Playground 中打开
/*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
const = 5;
obj. = document.body;
const foo = function () { };
try {
dangerousStuff();
} catch () {
// ignore as many do
}
const myObj = { : 1 };
() => { a * a };
class { }
class Foo { () {} }
class Bar { () {} }
class Baz { = 1 }
class Qux { = 1 }
function bar(...) { }
function baz([]) { }
const [] = arr;
const { prop: []} = {};
function qux({}) { }
const { } = {};
const { prop: } = {};
({ prop: obj. } = {});
使用默认选项时,此规则的正确代码示例
在 Playground 中打开
/*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
const num = 5;
function _f() { return 42; }
function _func() { return 42; }
obj.el = document.body;
const foo = function (evt) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
const 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]) { }
const { prop } = {};
const { prop: [name] } = {};
const [longName] = arr;
function foobar({ prop }) { }
function foobaz({ a: prop }) { }
const { a: property } = {};
({ prop: obj.longName } = {});
const 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 }
选项时,此规则的错误代码示例
在 Playground 中打开
/*eslint id-length: ["error", { "min": 4 }]*/
const = 5;
. = document.body;
function () { };
try {
dangerousStuff();
} catch () {
// ignore as many do
}
const myObj = { : 1 };
() => { val * val };
class { }
class { () {} }
function (...) { }
const { } = {};
const { prop: } = {};
const [] = arr;
const { prop: []} = {};
({ prop: . } = {});
使用 { "min": 4 }
选项时,此规则的正确代码示例
在 Playground 中打开
/*eslint id-length: ["error", { "min": 4 }]*/
const value = 5;
function func() { return 42; }
object.element = document.body;
const foobar = function (event) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
const myObj = { apple: 1 };
(value) => { value * value };
function foobaz(value = 0) { }
class MyClass { }
class Foobar { method() {} }
function barbaz(...args) { }
const { prop } = {};
const [longName] = foo;
const { a: [name] } = {};
const { a: record } = {};
({ prop: object.name } = {});
const data = { "x": 1 }; // excused because of quotes
data["y"] = 3; // excused because of calculated property access
max
使用 { "max": 10 }
选项时,此规则的错误代码示例
在 Playground 中打开
/*eslint id-length: ["error", { "max": 10 }]*/
const = 5;
function () { return 42; }
obj. = document.body;
const foo = function () { /* do stuff */ };
try {
dangerousStuff();
} catch () {
// ignore as many do
}
() => { return !reallyLongArgName; };
const [] = arr;
使用 { "max": 10 }
选项时,此规则的正确代码示例
在 Playground 中打开
/*eslint id-length: ["error", { "max": 10 }]*/
const varName = 5;
function funcName() { return 42; }
obj.propName = document.body;
const foo = function (arg) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
(arg) => { return !arg; };
const [first] = arr;
properties
使用 { "properties": "never" }
选项时,此规则的正确代码示例
在 Playground 中打开
/*eslint id-length: ["error", { "properties": "never" }]*/
const myObj = { a: 1 };
({ a: obj.x.y.z } = {});
({ prop: obj.i } = {});
exceptions
使用 { "exceptions": ["x", "y", "z", "ζ"] }
选项时,此规则的其他正确代码示例
在 Playground 中打开
/*eslint id-length: ["error", { "exceptions": ["x", "y", "z", "ζ", "i"] }]*/
const x = 5;
function y() { return 42; }
obj.x = document.body;
const foo = function (x) { /* do stuff */ };
try {
dangerousStuff();
} catch (x) {
// ignore as many do
}
(x) => { return x * x; };
const [i] = arr;
const { z } = foo;
const { a: ζ } = foo;
exceptionPatterns
使用 { "exceptionPatterns": ["E|S", "[x-z]"] }
选项时,此规则的其他正确代码示例
在 Playground 中打开
/*eslint id-length: ["error", { "exceptionPatterns": ["E|S|X", "[x-z]"] }]*/
const E = 5;
function S() { return 42; }
obj.x = document.body;
const foo = function (x) { /* do stuff */ };
try {
dangerousStuff();
} catch (x) {
// ignore as many do
}
(y) => {return y * y};
const [X] = arr;
const { y } = foo;
const { a: z } = foo;
相关规则
版本
此规则在 ESLint v1.0.0 中引入。