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): [switch-exhaustiveness-check] add requireDefaultForNonUnion option #7880

Merged
merged 9 commits into from Nov 19, 2023
Merged

feat(eslint-plugin): [switch-exhaustiveness-check] add requireDefaultForNonUnion option #7880

merged 9 commits into from Nov 19, 2023

Conversation

ST-DDT
Copy link
Contributor

@ST-DDT ST-DDT commented Nov 4, 2023

PR Checklist

Overview

This PR adds the requireDefaultForNonUnion option to the switch-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.

const value: number = Math.floor(Math.random() * 3);

switch (value) {
  case 0:
    return 0;
  case 1:
    return 1;
}

->

const value: number = Math.floor(Math.random() * 3);

switch (value) {
  case 0:
    return 0;
  case 1:
    return 1;
  default: { throw new Error('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.

@typescript-eslint
Copy link
Contributor

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.

Copy link

netlify bot commented Nov 4, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 9a8ae4c
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/655a1fdfe30cfc0008def515
😎 Deploy Preview https://deploy-preview-7880--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: 90 (🔴 down 8 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.

@ST-DDT ST-DDT changed the title rule(switch-exhaustiveness-check): add requireDefaultForNonUnion option feat(switch-exhaustiveness-check): add requireDefaultForNonUnion option Nov 4, 2023
@ST-DDT ST-DDT changed the title feat(switch-exhaustiveness-check): add requireDefaultForNonUnion option feat: add requireDefaultForNonUnion option to switch-exhaustiveness-check Nov 4, 2023
@bradzacher bradzacher changed the title feat: add requireDefaultForNonUnion option to switch-exhaustiveness-check feat(eslint-plugin): [switch-exhaustiveness-check] add requireDefaultForNonUnion option Nov 10, 2023
@bradzacher bradzacher added the enhancement: plugin rule option New rule option for an existing eslint-plugin rule label Nov 10, 2023
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.

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.

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready label Nov 11, 2023
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
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.

✨ swell, thanks!

@JoshuaKGoldberg
Copy link
Member

Ah, merge conflicts. I'll take care of them and merge.

@JoshuaKGoldberg
Copy link
Member

Integration test failures are unrelated. 5dbf622

@JoshuaKGoldberg JoshuaKGoldberg merged commit 4cfcd45 into typescript-eslint:main Nov 19, 2023
54 of 55 checks passed
@ST-DDT ST-DDT deleted the rule/switch-exhaustiveness-check/requireDefaultForNonUnion branch November 19, 2023 22:54
@ArnaudBarre
Copy link
Contributor

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

@ST-DDT
Copy link
Contributor Author

ST-DDT commented Nov 20, 2023

@ArnaudBarre Should we create a new issue for that?

@JoshuaKGoldberg
Copy link
Member

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 switch statements. Sometimes you really just want to switch over a few values! It's IMO not our place as a linter to tell people not to use a totally valid pattern of code like that.

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 evaluating community engagement label on it. Just posting now as a warning before you go through the effort of filing an issue. 😄

@ST-DDT
Copy link
Contributor Author

ST-DDT commented Nov 20, 2023

Vylpes pushed a commit to Vylpes/random-bunny that referenced this pull request Nov 27, 2023
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 (@&#8203;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 ([#&#8203;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 ([#&#8203;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 ([#&#8203;7806](typescript-eslint/typescript-eslint#7806)) ([a034d0a](typescript-eslint/typescript-eslint@a034d0a)), closes [#&#8203;7768](typescript-eslint/typescript-eslint#7768)

##### Features

-   \[member-ordering] add accessor support for member-ordering ([#&#8203;7927](typescript-eslint/typescript-eslint#7927)) ([3c8312d](typescript-eslint/typescript-eslint@3c8312d))
-   **eslint-plugin:** \[switch-exhaustiveness-check] add requireDefaultForNonUnion option ([#&#8203;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` ([#&#8203;7553](typescript-eslint/typescript-eslint#7553)) ([be2777c](typescript-eslint/typescript-eslint@be2777c))
-   **eslint-plugin:** \[no-unnecessary-qualifier] handle nested namespace id ([#&#8203;7883](typescript-eslint/typescript-eslint#7883)) ([a668f5b](typescript-eslint/typescript-eslint@a668f5b))

##### Features

-   add `no-unsafe-unary-minus` rule ([#&#8203;7390](typescript-eslint/typescript-eslint#7390)) ([c4709c2](typescript-eslint/typescript-eslint@c4709c2))
-   add types for flat config files ([#&#8203;7273](typescript-eslint/typescript-eslint#7273)) ([66cd0c0](typescript-eslint/typescript-eslint@66cd0c0))
-   allow typescript@5.3.0-RC as devDependency ([#&#8203;7821](typescript-eslint/typescript-eslint#7821)) ([b6c40b4](typescript-eslint/typescript-eslint@b6c40b4))
-   **eslint-plugin:** no-unsafe-enum-comparison handles switch cases ([#&#8203;7898](typescript-eslint/typescript-eslint#7898)) ([72cb9e4](typescript-eslint/typescript-eslint@72cb9e4))
-   **utils:** add ESLint `CodePath` selector types ([#&#8203;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 ([#&#8203;7854](typescript-eslint/typescript-eslint#7854)) ([11e57c5](typescript-eslint/typescript-eslint@11e57c5))
-   **eslint-plugin:** \[require-await] add support for "await using" ([#&#8203;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 ([#&#8203;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 ([#&#8203;7841](typescript-eslint/typescript-eslint#7841)) ([7ad86ee](typescript-eslint/typescript-eslint@7ad86ee))
-   **eslint-plugin:** \[no-unused-expressions] handle TSInstantiationExpression expression ([#&#8203;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 ([#&#8203;7841](typescript-eslint/typescript-eslint#7841)) ([7ad86ee](typescript-eslint/typescript-eslint@7ad86ee))
-   **eslint-plugin:** \[no-unused-expressions] handle TSInstantiationExpression expression ([#&#8203;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 ([#&#8203;7674](typescript-eslint/typescript-eslint#7674)) ([7e52f27](typescript-eslint/typescript-eslint@7e52f27))
-   **eslint-plugin:** \[no-unsafe-return] allow returning anything if explicitly returning any ([#&#8203;7708](typescript-eslint/typescript-eslint#7708)) ([c6124b2](typescript-eslint/typescript-eslint@c6124b2))

##### Features

-   **eslint-plugin:** \[max-params] don't count `this: void` parameter ([#&#8203;7696](typescript-eslint/typescript-eslint#7696)) ([6398d3f](typescript-eslint/typescript-eslint@6398d3f)), closes [#&#8203;7538](typescript-eslint/typescript-eslint#7538)
-   **eslint-plugin:** \[naming-convention] add support for default and namespace imports ([#&#8203;7269](typescript-eslint/typescript-eslint#7269)) ([bb15aae](typescript-eslint/typescript-eslint@bb15aae))
-   **eslint-plugin:** \[no-restricted-imports] support import = require ([#&#8203;7709](typescript-eslint/typescript-eslint#7709)) ([4c8edcf](typescript-eslint/typescript-eslint@4c8edcf))
-   **eslint-plugin:** \[no-unsafe-enum-comparison] add switch suggestion ([#&#8203;7691](typescript-eslint/typescript-eslint#7691)) ([53d5263](typescript-eslint/typescript-eslint@53d5263)), closes [#&#8203;7643](typescript-eslint/typescript-eslint#7643)
-   **eslint-plugin:** \[prefer-readonly] private fields support ([#&#8203;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>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 28, 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 enhancement: plugin rule option New rule option for an existing eslint-plugin rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[switch-exhaustiveness-check] Require default case if discriminant type is not a union
4 participants