-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): no-unsafe-enum-comparison handles switch cases #7898
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
feat(eslint-plugin): no-unsafe-enum-comparison handles switch cases #7898
Conversation
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
Thanks for the PR, @JoshuaKGoldberg! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
PR Checklist
switch
statements #7509Overview
Recreates #7541. Description pasted following.
Currently, the
no-unsafe-enum-comparison
rule handles comparisons (e.g.BinaryExpression
) but it does not handle switch statements. My PR makes the rule handle both.This was an oversight in my original design of this rule, and I consider this to be a bug. However, I have marked the PR as
feat
instead offix
to be more conservative; feel free to change it if you wish.Code Change Summary
I refactored the logic in the
BinaryExpression
selector and put it inside of a function calledisMismatchedComparison
.Then, I can call that function from multiple selectors. The changes should be pretty straightforward to understand.
Other Notes
I have reworked the documentation for this rule a bit, as the original text is now no longer accurate, because TypeScript has made number enums more safe in version 5.0. Thus, I rewrote the document to focus on the string case, adding a line to emphasize that the rule is still useful if you choose to use string enums over number enums.