question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Detect unused private members in classes

See original GitHub issue

What rule do you want to change? no-unused-vars (arguably this could be a separate rule as well, something like no-unused-private-fields) Does this change cause the rule to produce more or fewer warnings? More How will the change be implemented? (New option, new default behavior, etc.)? New default behavior Please provide some example code that this change will affect:

class MyClass {
  #unusedField = 5;
  #usedFieldOnlyInWrite = 42;
  #usedWhenReadingAndWriting = 500;
  someMethod() {
    this.#usedFieldOnlyInWrite = 42;
    this.#usedWhenReadingAndWriting += 1;
  }
  someOtherMethod() {
    return this.#usedWhenReadingAndWriting;
  }
}

What does the rule currently do for this code? Nothing What will the rule do after it’s changed? Report unused fields declared in a class. In the above example, it should report that both #unusedField and #usedFieldOnlyInWrite are unused. This should be possible, as private fields are only concerned in a particular class context, so you can collect all fields in a class, iterate through all usages and mark does that are never read as unused. Are you willing to submit a pull request to implement this change? I would be able to give it a go, but I am not sure how difficult this would be.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Aug 3, 2021

Makes sense to add this rule. If we want to include private methods in the same rule (I don’t see a reason why we shouldn’t), a name like no-unused-private-class-members might be more appropriate.

1reaction
TimvdLippecommented, Dec 1, 2021

I included this rule in DevTools today and it found a whole bunch of unused fields 🎉 https://crrev.com/c/3310610 I didn’t encounter any further issues nor false positives, so I think we are good here 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-unused-private-class-members - Pluggable JavaScript Linter
Private class members that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such...
Read more >
Rule to detect Unused Public classes or methods
Thank you for your suggestion. Similar rules exist for unused private methods and unused private fields. We do not intend to create ...
Read more >
Remove unused classes, properties, and functions
Unfortunately, you can only detect unused private members. This is because the code analyzer assumes public members might be used by other programs....
Read more >
Is there an eslint rule for detecting unused class properties?
You can get TypeScript to pick this up for you, by turning on the noUnusedLocals option in tsconfig.json . ("Locals" apparently includes ...
Read more >
Detecting Unused Private Properties, Methods, and Constants
Detecting Unused Private Properties, Methods, and Constants ; class Foo ; { private ; Bar $bar ; // only written - never read!...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found