-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [switch-exhaustiveness-check] add requireDefaultForNonUnion option #7880
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): [switch-exhaustiveness-check] add requireDefaultForNonUnion option #7880
Conversation
Thanks for the PR, @ST-DDT! 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks! Just a little bit of nitpicking left - if you don't like the suggestions or don't have a chance to address them, no worries. We can merge roughly as-is.
packages/eslint-plugin/docs/rules/switch-exhaustiveness-check.md
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨ swell, thanks!
Ah, merge conflicts. I'll take care of them and merge. |
Integration test failures are unrelated. 5dbf622 |
Thank you very much for this! Love this <3 But more me this more close to the bug fix, this option should not exist and always be enabled. I don't think anyone choosing to enable this rule will be agaisnt having this check that really catch bugs or shows a missing union in your types |
@ArnaudBarre Should we create a new issue for that? |
That would be the general right practice, yes 🙂. But I can tell you right now that this rule is far opinionated to not have this as an option. As a user of it I would maybe only ever use it for unions. It'd be quite restrictive to have it apply to all I'm not saying this to shut your ideas down. If you feel strongly that I'm wrong here and we should discuss in an issue, then absolutely let's continue the conversation there. We can always discuss there, and even if it's not immediately accepted we can always put the |
Done: #7966 |
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint) | devDependencies | minor | [`6.8.0` -> `6.12.0`](https://renovatebot.com/diffs/npm/@typescript-eslint%2feslint-plugin/6.8.0/6.12.0) | --- ### Release Notes <details> <summary>typescript-eslint/typescript-eslint (@​typescript-eslint/eslint-plugin)</summary> ### [`v6.12.0`](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#6120-2023-11-20) [Compare Source](typescript-eslint/typescript-eslint@v6.11.0...v6.12.0) ##### Bug Fixes - **eslint-plugin:** \[class-methods-use-this] detect a problematic case for private/protected members if `ignoreClassesThatImplementAnInterface` is set ([#​7705](typescript-eslint/typescript-eslint#7705)) ([155aa1f](typescript-eslint/typescript-eslint@155aa1f)) - **eslint-plugin:** \[no-unnecessary-condition] fix false positive with computed member access and branded key type ([#​7706](typescript-eslint/typescript-eslint#7706)) ([f151b26](typescript-eslint/typescript-eslint@f151b26)) - **eslint-plugin:** \[switch-exhaustiveness-check] enum members with new line or single quotes are not being fixed correctly ([#​7806](typescript-eslint/typescript-eslint#7806)) ([a034d0a](typescript-eslint/typescript-eslint@a034d0a)), closes [#​7768](typescript-eslint/typescript-eslint#7768) ##### Features - \[member-ordering] add accessor support for member-ordering ([#​7927](typescript-eslint/typescript-eslint#7927)) ([3c8312d](typescript-eslint/typescript-eslint@3c8312d)) - **eslint-plugin:** \[switch-exhaustiveness-check] add requireDefaultForNonUnion option ([#​7880](typescript-eslint/typescript-eslint#7880)) ([4cfcd45](typescript-eslint/typescript-eslint@4cfcd45)) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ### [`v6.11.0`](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#6110-2023-11-13) [Compare Source](typescript-eslint/typescript-eslint@v6.10.0...v6.11.0) ##### Bug Fixes - **eslint-plugin:** \[explicit-function-return-type] support JSX attributes in `allowTypedFunctionExpressions` ([#​7553](typescript-eslint/typescript-eslint#7553)) ([be2777c](typescript-eslint/typescript-eslint@be2777c)) - **eslint-plugin:** \[no-unnecessary-qualifier] handle nested namespace id ([#​7883](typescript-eslint/typescript-eslint#7883)) ([a668f5b](typescript-eslint/typescript-eslint@a668f5b)) ##### Features - add `no-unsafe-unary-minus` rule ([#​7390](typescript-eslint/typescript-eslint#7390)) ([c4709c2](typescript-eslint/typescript-eslint@c4709c2)) - add types for flat config files ([#​7273](typescript-eslint/typescript-eslint#7273)) ([66cd0c0](typescript-eslint/typescript-eslint@66cd0c0)) - allow typescript@5.3.0-RC as devDependency ([#​7821](typescript-eslint/typescript-eslint#7821)) ([b6c40b4](typescript-eslint/typescript-eslint@b6c40b4)) - **eslint-plugin:** no-unsafe-enum-comparison handles switch cases ([#​7898](typescript-eslint/typescript-eslint#7898)) ([72cb9e4](typescript-eslint/typescript-eslint@72cb9e4)) - **utils:** add ESLint `CodePath` selector types ([#​7551](typescript-eslint/typescript-eslint#7551)) ([99a026f](typescript-eslint/typescript-eslint@99a026f)) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ### [`v6.10.0`](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#6100-2023-11-06) [Compare Source](typescript-eslint/typescript-eslint@v6.9.1...v6.10.0) ##### Bug Fixes - **eslint-plugin:** \[no-unused-vars] handle logical assignment ([#​7854](typescript-eslint/typescript-eslint#7854)) ([11e57c5](typescript-eslint/typescript-eslint@11e57c5)) - **eslint-plugin:** \[require-await] add support for "await using" ([#​7866](typescript-eslint/typescript-eslint#7866)) ([855abea](typescript-eslint/typescript-eslint@855abea)) ##### Features - **eslint-plugin:** \[ban-ts-comments] suggest ts-expect-error over ts-ignore ([#​7849](typescript-eslint/typescript-eslint#7849)) ([5e73a48](typescript-eslint/typescript-eslint@5e73a48)) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. #### [6.9.1](typescript-eslint/typescript-eslint@v6.9.0...v6.9.1) (2023-10-30) ##### Bug Fixes - **eslint-plugin:** \[naming-convention] allow PascalCase for imports ([#​7841](typescript-eslint/typescript-eslint#7841)) ([7ad86ee](typescript-eslint/typescript-eslint@7ad86ee)) - **eslint-plugin:** \[no-unused-expressions] handle TSInstantiationExpression expression ([#​7831](typescript-eslint/typescript-eslint#7831)) ([31988e0](typescript-eslint/typescript-eslint@31988e0)) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ### [`v6.9.1`](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#691-2023-10-30) [Compare Source](typescript-eslint/typescript-eslint@v6.9.0...v6.9.1) ##### Bug Fixes - **eslint-plugin:** \[naming-convention] allow PascalCase for imports ([#​7841](typescript-eslint/typescript-eslint#7841)) ([7ad86ee](typescript-eslint/typescript-eslint@7ad86ee)) - **eslint-plugin:** \[no-unused-expressions] handle TSInstantiationExpression expression ([#​7831](typescript-eslint/typescript-eslint#7831)) ([31988e0](typescript-eslint/typescript-eslint@31988e0)) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ### [`v6.9.0`](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#690-2023-10-23) [Compare Source](typescript-eslint/typescript-eslint@v6.8.0...v6.9.0) ##### Bug Fixes - **eslint-plugin:** \[no-confusing-void-expression] handle unfixable cases ([#​7674](typescript-eslint/typescript-eslint#7674)) ([7e52f27](typescript-eslint/typescript-eslint@7e52f27)) - **eslint-plugin:** \[no-unsafe-return] allow returning anything if explicitly returning any ([#​7708](typescript-eslint/typescript-eslint#7708)) ([c6124b2](typescript-eslint/typescript-eslint@c6124b2)) ##### Features - **eslint-plugin:** \[max-params] don't count `this: void` parameter ([#​7696](typescript-eslint/typescript-eslint#7696)) ([6398d3f](typescript-eslint/typescript-eslint@6398d3f)), closes [#​7538](typescript-eslint/typescript-eslint#7538) - **eslint-plugin:** \[naming-convention] add support for default and namespace imports ([#​7269](typescript-eslint/typescript-eslint#7269)) ([bb15aae](typescript-eslint/typescript-eslint@bb15aae)) - **eslint-plugin:** \[no-restricted-imports] support import = require ([#​7709](typescript-eslint/typescript-eslint#7709)) ([4c8edcf](typescript-eslint/typescript-eslint@4c8edcf)) - **eslint-plugin:** \[no-unsafe-enum-comparison] add switch suggestion ([#​7691](typescript-eslint/typescript-eslint#7691)) ([53d5263](typescript-eslint/typescript-eslint@53d5263)), closes [#​7643](typescript-eslint/typescript-eslint#7643) - **eslint-plugin:** \[prefer-readonly] private fields support ([#​7686](typescript-eslint/typescript-eslint#7686)) ([0e875bf](typescript-eslint/typescript-eslint@0e875bf)) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=--> Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/107 Reviewed-by: Vylpes <ethan@vylpes.com> Co-authored-by: Renovate Bot <renovate@vylpes.com> Co-committed-by: Renovate Bot <renovate@vylpes.com>
PR Checklist
Overview
This PR adds the
requireDefaultForNonUnion
option to theswitch-exhaustiveness-check
rule.If enabled, all switches on non-union types require a default case to ensure they are "exhaustive".
The rule is disabled by default, although I would recommend changing this in a future major release, because why would you not want all of your switch cases to be exhaustive.
The rule already has the check for union types so it was easy to use the else branch to add the check for the default case.
->
This cannot be achieved with the
default-case
rule, because that also requires the default case for union types which would hide the potential errors caused by extending the union with another case.