Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint rules for no-unused-enums

There's a no-unused-vars eslint rule that can warn/error if a variable is unused: https://eslint.org/docs/rules/no-unused-vars

If I want to ensure that there are no unused enums, what would be the best lint rule to implement?

Example scenario:

export enum MyEnum {
  ONE = 'ONE',
  TWO = 'TWO',
  THREE = 'THREE',
}

Here MyEnum.ONE and MyEnum.TWO are both used, but MyEnum.THREE is not referenced.


if (type === MyEnum.ONE) {
  ...
}

if (type === MyEnum.TWO) {
  ...
}

It would be great if a lint rule could warn/error that MyEnum.THREE is unused so that it may be removed.

like image 561
tester Avatar asked Nov 19 '25 20:11

tester


1 Answers

There is no existing rule to do this so you've got a couple of options:

  1. Write your own rule (and publish it for the rest of us to use!)
  2. If are you IntelliJ, then there is an inspection in the IDE ("Unused global symbol") that can give you the warning.

As an aside, make sure you are using the Typescript version of no-unused-vars rule. Even then it doesn't detect unused exported variables because each file is analysed individually.

like image 149
matt helliwell Avatar answered Nov 21 '25 13:11

matt helliwell