版本

id-length

強制最小和最大識別符號長度

❄️ Frozen

此規則目前為凍結狀態,不接受功能請求。

非常短的識別符號名稱(如 ex_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 x = 5;
obj.e = document.body;
const foo = function (e) { };
try {
    dangerousStuff();
} catch (e) {
    // ignore as many do
}
const myObj = { a: 1 };
(a) => { a * a };
class y { }
class Foo { x() {} }
class Bar { #x() {} }
class Baz { x = 1 }
class Qux { #x = 1 }
function bar(...x) { }
function baz([x]) { }
const [z] = arr;
const { prop: [i]} = {};
function qux({x}) { }
const { j } = {};
const { prop: a} = {};
({ prop: obj.x } = {});

使用預設選項時,此規則的正確程式碼範例

在 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 val = 5;
obj.e = document.body;
function foo (e) { };
try {
    dangerousStuff();
} catch (e) {
    // ignore as many do
}
const myObj = { a: 1 };
(val) => { val * val };
class y { }
class Foo { x() {} }
function bar(...x) { }
const { x } = {};
const { prop: a} = {};
const [i] = arr;
const { prop: [num]} = {};
({ prop: obj.x } = {});

使用 { "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 reallyLongVarName = 5;
function reallyLongFuncName() { return 42; }
obj.reallyLongPropName = document.body;
const foo = function (reallyLongArgName) { /* do stuff */ };
try {
    dangerousStuff();
} catch (reallyLongErrorName) {
    // ignore as many do
}
(reallyLongArgName) => { return !reallyLongArgName; };
const [reallyLongFirstElementName] = 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 中引入。

資源

變更語言