Skip to content

fix(eslint-plugin): [switch-exhaustive-check] handle infinite types #6922

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

Closed
wants to merge 14 commits into from

Conversation

cparros
Copy link
Contributor

@cparros cparros commented Apr 17, 2023

PR Checklist

Overview

I have updated this rule to handle infinite types. It will not report an error.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @cparros!

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 Apr 17, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit a9c2a77
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/64add9d653387a0008992a74
😎 Deploy Preview https://deploy-preview-6922--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@cparros cparros changed the title fix(eslint-plugin): [switch-exhaustive-check] handle infinite types DRAFT: fix(eslint-plugin): [switch-exhaustive-check] handle infinite types Apr 17, 2023
@codecov
Copy link

codecov bot commented Apr 17, 2023

Codecov Report

Merging #6922 (634096e) into main (faea3ff) will decrease coverage by 0.02%.
The diff coverage is 88.88%.

❗ Current head 634096e differs from pull request most recent head a9c2a77. Consider uploading reports for the commit a9c2a77 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6922      +/-   ##
==========================================
- Coverage   87.43%   87.41%   -0.02%     
==========================================
  Files         386      386              
  Lines       13192    13200       +8     
  Branches     3872     3874       +2     
==========================================
+ Hits        11534    11539       +5     
- Misses       1292     1294       +2     
- Partials      366      367       +1     
Flag Coverage Δ
unittest 87.41% <88.88%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...nt-plugin/src/rules/switch-exhaustiveness-check.ts 92.98% <88.88%> (-4.98%) ⬇️

@bradzacher
Copy link
Member

heads up @cparros - no need to label the PR title with DRAFT: - you can just raise the PR as a draft PR in github. I've converted this PR to a draft for you 😄

@bradzacher bradzacher marked this pull request as draft April 17, 2023 01:03
@bradzacher bradzacher changed the title DRAFT: fix(eslint-plugin): [switch-exhaustive-check] handle infinite types fix(eslint-plugin): [switch-exhaustive-check] handle infinite types Apr 17, 2023
@bradzacher bradzacher added the bug Something isn't working label Apr 17, 2023
@cparros
Copy link
Contributor Author

cparros commented Jun 12, 2023

Sorry for the long time between commits here. Update but in the tests a intersecting type is still failing I'm definitely missing something whenI look at this, I feel dumb ha

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.

Looks very close! Just one last oddity in logic to smooth out. 🙌

),
);

if (!isFiniteType && !isSymbolType && !isStringLiteralOrBoolean) {
Copy link
Member

Choose a reason for hiding this comment

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

[Performance] This probably will never be a real issue, but - you can short circuit each of these cases to not have to go through later checks after an earlier one is true.

if (unionTypes.every((isLiteralType)) {
  return;
}

if (unionTypes.some( ... )) {
  return;
}

We've been bit in the past by doing unnecessary work that was fine when written but ended up being a lot of extra cycles.

const isStringLiteralOrBoolean = unionTypes.some(type =>
isTypeFlagSet(
type,
ts.TypeFlags.BooleanLiteral || ts.TypeFlags.StringLiteral,
Copy link
Member

Choose a reason for hiding this comment

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

[Bug] || is the logical or operator. I'm guessing you meant | as the bitwise or.

But, this feels weird to me. It's generally not ideal for us to be hardcoding to specific primitives. What about bigint literals, number literals, etc.? This code should handle them too. ts.TypeFlags.Literal should be what you want to use.

Could you also add some test cases with bigint literals (1n) and template literal strings (a)?

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Jun 28, 2023
case 'bar': {
result = 42;
break;
}
Copy link
Member

Choose a reason for hiding this comment

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

Just posting here so it doesn't get lost - tests should end up including bigints, booleans, symbols, unique symbols, and other fun wacky combinations/things just to be safe.

@JoshuaKGoldberg
Copy link
Member

👋 Hey @cparros! Just checking in, is this still something you have time for? No worries if not - I just don't want to leave it hanging.

@JoshuaKGoldberg JoshuaKGoldberg added the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Oct 17, 2023
@JoshuaKGoldberg
Copy link
Member

Closing this PR as it's been stale for a few weeks without activity. Feel free to reopen @cparros if you have time - but no worries if not! If anybody wants to drive it forward, please do post your own PR - and if you use this as a start, consider adding co-author attribution at the end of your PR description. Thanks! 😊

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party bug Something isn't working stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [switch-exhaustiveness-check] invalid fix for union with infinite types
4 participants