Skip to content

Rule proposal: no-unused-private-class-members #4571

@alumni

Description

@alumni

Description

To complement the original no-unused-private-class-members rule which works with typescript but doesn't detect the private keyword:

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.

— From: https://eslint.org/docs/rules/no-unused-private-class-members#no-unused-private-class-members

See also: eslint/eslint#14859.

Fail

class Foo {
    #unusedMember: number;
    private anotherUnusedMember: number;
}

class Foo {
    #usedOnlyInWrite: number;

    constructor(private anotherUsedOnlyInWrite: number) {
        // noop
    }

    method() {
        this.#usedOnlyInWrite = 42;
    }
}

class Foo {
    #usedOnlyToUpdateItself = 5;

    method() {
        this.#usedOnlyToUpdateItself++;
    }
}

class Foo {
    #unusedMethod() {}
    private anotherUnusedMethod() {}
}

class Foo {
    get #unusedAccessor() {}
    set #unusedAccessor(value) {}
    private get anotherUnusedAccessor() {}
    private set anotherUnusedAccessor(value) {}
}

Pass

class Foo {
    #usedMember = 35;
    private anotherUsedMember = 7;

    method() {
        return this.#usedMember + this.anotherUsedMember;
    }
}

class Foo {
    constructor(private readonly usedMember: number) {
        // noop
    }

    doSomething() {
        return this.usedMember / 42;
    }
}


class Foo {
    #usedMethod() {
        return 35;
    }

    private anotherUsedMethod() {
        return 7;
    }

    anotherMethod() {
        return this.#usedMethod() + this.anotherUsedMethod();
    }
}

class Foo {
    get #usedAccessor() {}
    set #usedAccessor(value) {}
    private get anotherUsedAccessor() {}
    private set anotherUsedAccessor(value) {}
    
    method() {
        this.#usedAccessor = 42;
        this.anotherUsedAccessor = 42;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: new base rule extensionNew base rule extension required to handle a TS specific casepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy