Skip to content
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

Enhancement: [no-unsafe-enum-comparison] Suggestion fixer to switch to enum value #7643

Closed
4 tasks done
JoshuaKGoldberg opened this issue Sep 14, 2023 · 0 comments · Fixed by #7691
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@JoshuaKGoldberg
Copy link
Member

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/no-unsafe-enum-comparison

Description

If the rule detects an unsafe enum<>literal comparison that happens to match with a member of the enum's type, the rule report could include a suggestion to switch the literal to that literal value.

- fruit === "apple";
+ fruit === Fruit.Apple;

Fail

enum Fruit {
  Apple = 'apple',
}

declare let fruit: Fruit;

fruit === "apple";

Pass

enum Fruit {
  Apple = 'apple',
}

declare let fruit: Fruit;

fruit === Fruit.Apple;

Additional Info

No response

@JoshuaKGoldberg JoshuaKGoldberg added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look enhancement: plugin rule option New rule option for an existing eslint-plugin rule labels Sep 14, 2023
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Sep 14, 2023
StyleShit added a commit to StyleShit/typescript-eslint that referenced this issue Sep 24, 2023
StyleShit added a commit to StyleShit/typescript-eslint that referenced this issue Sep 24, 2023
JoshuaKGoldberg pushed a commit that referenced this issue Oct 19, 2023
#7691)

* feat(eslint-plugin): [no-unsafe-enum-comparison] add switch suggestion

Closes #7643

* refactor + add tests

* remove "void fixer"

* fix lint

* wip

* beautify

* just changing a wrong variable name

* just making it look & feel better and consistent

* add failing test

* wip

* added some more tests to cover stupid cases

* moar stupid tests
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants