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

fix(eslint-plugin): [switch-exhaustiveness-check] better support for intersections, infinite types, non-union values #8250

Merged
merged 6 commits into from
Jan 30, 2024

Conversation

auvred
Copy link
Member

@auvred auvred commented Jan 14, 2024

PR Checklist

Overview

I decided to fix both issues in one pr, because it was really hard to fix the first one without fixing the second.

This PR introduces better support for all combinations of unions, intersections, infinite types. It also fix suggestions more accurate.

In addition, this PR also includes several fixes for bugs that were present on main but wasn't mentioned in issues:

1. Provide correct fixes for missing unique symbols

Before (playground):

const a = Symbol('a')
const b = Symbol('b')
declare const value: typeof a | typeof b  

switch (value) {
  case a: break;
+ case unique symbol: { throw new Error('Not implemented yet: unique symbol case') }
}

After:

const a = Symbol('a')
const b = Symbol('b')
declare const value: typeof a | typeof b  

switch (value) {
  case a: break;
+ case b: { throw new Error('Not implemented yet: b case') }
}

2. Support intersections that includes literals

Before (playground):

declare const value: ('aa' & {a:1}) | 'bb'
switch (value) {
//      ^ Switch is not exhaustive. Cases not matched: "aa" & { a: 1; }
  case 'aa': break;
  case 'bb': break;
}

After (no false positive):

declare const value: ('aa' & {a:1}) | 'bb'
switch (value) {
  case 'aa': break;
  case 'bb': break;
}

3. Consistently add a default branch both for bare infinite types and unions of infinite types

Before (playground):

declare const value: number
switch (value) {
  case 1: break;
+ default: { throw new Error('default case') }
}


declare const value: number | string
switch (value) {
  case 1: break;
+ case string: { throw new Error('Not implemented yet: string case') }
+ case number: { throw new Error('Not implemented yet: number case') }
}

After:

declare const value: number
switch (value) {
  case 1: break;
+ default: { throw new Error('default case') }
}


declare const value: number | string
switch (value) {
  case 1: break;
+ default: { throw new Error('default case') }
}

I've removed isUnion from SwitchMetadata to avoid confusion: we shouldn't treat unions and non-unions differently. We should only care whether a type contains non-literal types or not

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @auvred!

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 Jan 14, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit e343dce
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/65a3d4bd5fd1b300085b2ae3
😎 Deploy Preview https://deploy-preview-8250--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: 98 (🟢 up 2 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.

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.

What a lovely, thoroughly tested PR! Thanks for sending this in! 👏

Comment on lines +128 to +131
for (const unionPart of tsutils.unionTypeParts(discriminantType)) {
for (const intersectionPart of tsutils.intersectionTypeParts(
unionPart,
)) {
Copy link
Member

Choose a reason for hiding this comment

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

[Non-Actionable] This feels like something we'd want a shared utility for... and, ha, I filed an issue for this back in August! #7466 (comment) -> JoshuaKGoldberg/ts-api-utils#258

Not requesting changes here. I'll go ahead and send that PR to ts-api-utils and then follow up in this repo.

Copy link
Member

Choose a reason for hiding this comment

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

@JoshuaKGoldberg JoshuaKGoldberg merged commit 82cb9dd into typescript-eslint:main Jan 30, 2024
59 checks passed
danvk pushed a commit to danvk/typescript-eslint that referenced this pull request Feb 4, 2024
…intersections, infinite types, non-union values (typescript-eslint#8250)

* feat(eslint-plugin): [switch-exhaustiveness-check] better support for intersections, infinite types, non-union values

* chore: try to fix weird diff with main

* refactor: no need to collect missing branches in function

* fix: provide valid fixes for unique symbols

* fix: valid fixes for unique symbols + few test cases for enums
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants