no-unused-private-class-members
禁止未使用的私有类成员
在 配置文件 中使用来自 @eslint/js
的 recommended
配置可以启用此规则
已声明且未在代码中任何位置使用的私有类成员很可能是由于重构不完整而导致的错误。这样的类成员会占用代码中的空间,并可能导致读者混淆。
¥Private class members that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such class members take up space in the code and can lead to confusion by readers.
规则详情
¥Rule Details
此规则报告未使用的私有类成员。
¥This rule reports unused private class members.
-
如果从不读取私有字段或方法的值,则认为该私有字段或方法未使用。
¥A private field or method is considered to be unused if its value is never read.
-
如果从不访问(读取或写入)私有访问器,则认为它未使用。
¥A private accessor is considered to be unused if it is never accessed (read or write).
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-unused-private-class-members: "error"*/
class A {
#unusedMember = 5;
}
class B {
#usedOnlyInWrite = 5;
method() {
this.#usedOnlyInWrite = 42;
}
}
class C {
#usedOnlyToUpdateItself = 5;
method() {
this.#usedOnlyToUpdateItself++;
}
}
class D {
#unusedMethod() {}
}
class E {
get #unusedAccessor() {}
set #unusedAccessor(value) {}
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-unused-private-class-members: "error"*/
class A {
#usedMember = 42;
method() {
return this.#usedMember;
}
}
class B {
#usedMethod() {
return 42;
}
anotherMethod() {
return this.#usedMethod();
}
}
class C {
get #usedAccessor() {}
set #usedAccessor(value) {}
method() {
this.#usedAccessor = 42;
}
}
何时不使用
¥When Not To Use It
如果你不想收到关于未使用的私有类成员的通知,你可以安全地关闭此规则。
¥If you don’t want to be notified about unused private class members, you can safely turn this rule off.
版本
此规则是在 ESLint v8.1.0 中引入。