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

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

Conversation

StyleShit
Copy link
Contributor

@StyleShit StyleShit commented Sep 24, 2023

Closes #7643

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @StyleShit!

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.

@netlify
Copy link

netlify bot commented Sep 24, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 52af550
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/653168ebad3b8700084a1c0d
😎 Deploy Preview https://deploy-preview-7691--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 4 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@StyleShit StyleShit force-pushed the tweak/no-unsafe-enum-comparison-suggestion branch from 8725058 to 0471e12 Compare September 24, 2023 12:07
@StyleShit StyleShit changed the title tweak(eslint-plugin): [no-unsafe-enum-comparison] add switch suggestion feat(eslint-plugin): [no-unsafe-enum-comparison] add switch suggestion Sep 24, 2023
@StyleShit
Copy link
Contributor Author

@JoshuaKGoldberg

OK, should be better now, but I still can't figure out the types there 😓

@StyleShit StyleShit marked this pull request as ready for review October 3, 2023 12:27
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Really good start so far! The code looks clean and I like the general direction. 😄

Left a few comments and suggestions for how to overcome the // @ts-expect-errors. I suspect they'll end up showing you a bunch more test cases to add. Have fun!

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Oct 10, 2023
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Oct 17, 2023
* - Fruit.Apple | Vegetable.Lettuce --> [Fruit.Apple, Vegetable.Lettuce]
* - Fruit.Apple | Vegetable.Lettuce | 123 --> [Fruit.Apple, Vegetable.Lettuce]
* - T extends Fruit --> [Fruit]
*/
Copy link
Member

Choose a reason for hiding this comment

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

[Praise] Great comments, thanks for continuing the existing standard 🙂

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Looking great! Structure is solid, good test coverage, super readable code. Nice!

Just requesting changes on a bit more test coverage and some getText() -> name touchups.

Timmy from the Shaun the Sheep movie giving two thumbs up

packages/eslint-plugin/src/rules/enum-utils/shared.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/enum-utils/shared.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/enum-utils/shared.ts Outdated Show resolved Hide resolved
'a' = 1,
}
declare const stringKey: StringKey;
stringKey === StringKey['a'];
Copy link
Member

Choose a reason for hiding this comment

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

StringKey['a'];

Just confirming explicitly in case someone looks at this in the future: I think it's fine to have it look like StringKey['a'] instead of StringKey.a. They're functionally the same, and there are other rules around stylistic concerns.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Oct 17, 2023
return `${enumName}['${memberNameIdentifier.text}']`;

case ts.SyntaxKind.ComputedPropertyName:
return `${enumName}[${memberNameIdentifier.expression.getText()}]`;
Copy link
Contributor Author

@StyleShit StyleShit Oct 17, 2023

Choose a reason for hiding this comment

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

had to use .getText() here, there is no .text for some reason
seems like it's fine? See the tests for that as a reference

Copy link
Member

Choose a reason for hiding this comment

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

The type of memberNameIdentifier.expression is ts.Expression, which is a pretty wide type. You could as assert it to the union of types that are actually allowed as enum member names... or just go with .getText(). If the unit tests aren't showing any bad behavior, it's probably fine. 😎

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would probably keep it like this, seems fine in the tests (unless I'm missing some cases?)

Copy link
Member

Choose a reason for hiding this comment

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

The existing bugs in #7768 make me not too motivated to get every single possible edge case in this PR. The tests you've added are pretty great. Thanks!

@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Oct 17, 2023
Comment on lines +844 to +847
enum ComputedKey {
[\`test-
key\` /* with comment */] = 1,
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Why is this even legal?!)

Copy link
Member

Choose a reason for hiding this comment

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

to make our lives harder 🫠

Copy link
Contributor Author

@StyleShit StyleShit Oct 19, 2023

Choose a reason for hiding this comment

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

It's hard enough without it! 😭

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Nice! Great investigation resulting in a nice set of test coverage & fixing. Thanks for working with me on this on @StyleShit!

We can always tackle more edge cases in the followup #7768.

Crayon cartoon drawing of a purple cat/bear thing happily gesture-hugging and saying 'THANKYOU'

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready label Oct 19, 2023
@JoshuaKGoldberg JoshuaKGoldberg merged commit 53d5263 into typescript-eslint:main Oct 19, 2023
46 checks passed
@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
1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: [no-unsafe-enum-comparison] Suggestion fixer to switch to enum value
2 participants