Index

no-unused-private-class-members

禁止未使用的私有类成员

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

💡 hasSuggestions

此规则报告的一些问题可通过编辑器 建议 手动修复

在代码中声明但未使用的私有类成员很可能是由于重构不完整导致的错误。这样的类成员占用代码空间,并可能导致读者困惑。

🌐 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.

  • 如果从不读取私有字段或方法的值,则认为该私有字段或方法未使用。
  • 如果从不访问(读取或写入)私有访问器,则认为它未使用。

此规则的错误代码示例:

🌐 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;
    }
}

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

何时不使用

🌐 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 中引入。

资源