Skip to content

feat(eslint-plugin): [no-unused-private-class-members] new extension rule #10913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
description: 'Disallow unused private class members.'
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-unused-private-class-members** for documentation.

This rule extends the base [`eslint/no-unused-private-class-members`](https://eslint.org/docs/rules/no-unused-private-class-members) rule.
It adds support for members declared with TypeScript's `private` keyword.

## Options

This rule has no options.

## Examples

<Tabs>
<TabItem value="❌ Incorrect">

```ts
class A {
private foo = 123;
}
```

</TabItem>
<TabItem value="✅ Correct">

```tsx
class A {
private foo = 123;

constructor() {
console.log(this.foo);
}
}
```

</TabItem>
</Tabs>

## Limitations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about also mentioning assigning this to a variable as a limitaiton?

class Test1 {
  private bar: number;

  foo() {
    const self = this;

    return function () {
      // ❌ NOT detected as a usage
      self.bar;
    }
  }
}


This rule does not detect the following cases:

(1) Private members only used using private only without `this`:

```ts
class Foo {
private prop = 123;

method(a: Foo) {
// ✅ Detected as a usage
const prop = this.prop;

// ❌ NOT detected as a usage
const otherProp = a.prop;
}
}
```

(2) Usages of the private member outside of the class:

```ts
class Foo {
private prop = 123;
}

const instance = new Foo();
// ❌ NOT detected as a usage
console.log(foo['prop']);
```

## 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.
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/configs/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export = {
'@typescript-eslint/no-unsafe-unary-minus': 'error',
'no-unused-expressions': 'off',
'@typescript-eslint/no-unused-expressions': 'error',
'no-unused-private-class-members': 'off',
'@typescript-eslint/no-unused-private-class-members': 'error',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
'no-use-before-define': 'off',
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import noUnsafeReturn from './no-unsafe-return';
import noUnsafeTypeAssertion from './no-unsafe-type-assertion';
import noUnsafeUnaryMinus from './no-unsafe-unary-minus';
import noUnusedExpressions from './no-unused-expressions';
import noUnusedPrivateClassMembers from './no-unused-private-class-members';
import noUnusedVars from './no-unused-vars';
import noUseBeforeDefine from './no-use-before-define';
import noUselessConstructor from './no-useless-constructor';
Expand Down Expand Up @@ -220,6 +221,7 @@ const rules = {
'no-unsafe-type-assertion': noUnsafeTypeAssertion,
'no-unsafe-unary-minus': noUnsafeUnaryMinus,
'no-unused-expressions': noUnusedExpressions,
'no-unused-private-class-members': noUnusedPrivateClassMembers,
'no-unused-vars': noUnusedVars,
'no-use-before-define': noUseBeforeDefine,
'no-useless-constructor': noUselessConstructor,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ESLintUtils } from '@typescript-eslint/utils';

import { createRule } from '../util';
import { analyzeClassMemberUsage } from '../util/class-scope-analyzer/classScopeAnalyzer';

type Options = [];
export type MessageIds = 'unusedPrivateClassMember';

export default createRule<Options, MessageIds>({
name: 'no-unused-private-class-members',
meta: {
type: 'problem',
docs: {
description: 'Disallow unused private class members',
extendsBaseRule: true,
requiresTypeChecking: false,
},

messages: {
unusedPrivateClassMember:
"Private class member '{{classMemberName}}' is defined but never used.",
},

schema: [],
},
defaultOptions: [],
create(context) {
return {
'Program:exit'(node) {
const result = analyzeClassMemberUsage(
node,
ESLintUtils.nullThrows(
context.sourceCode.scopeManager,
'Missing required scope manager',
),
);

for (const classScope of result.values()) {
for (const member of [
...classScope.members.instance.values(),
...classScope.members.static.values(),
]) {
if (!member.isPrivate() && !member.isHashPrivate()) {
continue;
}

if (member.referenceCount === 0) {
context.report({
node: member.node.key,
messageId: 'unusedPrivateClassMember',
data: { classMemberName: member.name },
});
}
}
}
},
};
},
});
Loading
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