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

Rule proposal: switch-case-limits #2439

Open
lo1tuma opened this issue Aug 29, 2024 · 4 comments
Open

Rule proposal: switch-case-limits #2439

lo1tuma opened this issue Aug 29, 2024 · 4 comments

Comments

@lo1tuma
Copy link
Contributor

lo1tuma commented Aug 29, 2024

Description

A rule that enforces the min or max amount of cases required per switch statement. E.g. a switch statement with just one case or two would be better expressed with if-else. At the same time a switch statement with too many cases might be better refactored to a lookup table.

{
  "switch-case-limits": [
    "error",
    { "min": 2, "max": 10 }
  ]
}

Fail

// min: 2
switch (foo) {
   case: 'bar':
      return 'BAR';
}
// max: 3
switch (foo) {
   case: 'bar1':
      return 'BAR1';
   case: 'bar2':
      return 'BAR2';
   case: 'bar3':
      return 'BAR3';
   case: 'bar4':
      return 'BAR4';
}

Pass

// min: 2
switch (foo) {
   case: 'bar1':
      return 'BAR1';
  case: 'bar2':
      return 'BAR2';
}
// max: 3
switch (foo) {
   case: 'bar1':
      return 'BAR1';
   case: 'bar2':
      return 'BAR2';
   case: 'bar3':
      return 'BAR3';
}

Proposed rule name

switch-case-limits

Additional Info

No response

@fregante
Copy link
Collaborator

fregante commented Sep 3, 2024

I agree that switches with one case unnecessarily verbose, I don't think a large number of cases is inherently bad though.

If they're a list of 15 case/return lines line in your example I think it's fine. Many cases are usually unreadable when each case has 30+ lines each. At that point the native complexity or max-lines-per-function might just do it.

@sindresorhus
Copy link
Owner

I also don't really see the problem with large switches. I do think the first example with the single case could be its own rule though.

@zanminkian
Copy link
Contributor

You can use the rule no-small-switch and max-switch-cases in eslint-plugin-sonarjs. IMO, no need to implement it here.

@lo1tuma
Copy link
Contributor Author

lo1tuma commented Sep 17, 2024

You can use the rule no-small-switch and max-switch-cases in eslint-plugin-sonarjs. IMO, no need to implement it here.

I’m using eslint-plugin-sonarjs v1, but they recently released v2 which brings in tons of framework-specific rules with additional dependencies that I don’t need or want. That’s why I’m looking for alternatives and I though eslint-plugin-unicorn could be a good fit.


At that point the native complexity or max-lines-per-function might just do it.

That’s a good point, the complexity rule should detect large switches.


I do think the first example with the single case could be its own rule though.

Should it be still configurable what the threshold of cases should be or just hardcoded to one (e.g. no-single-case-switch). Personally I would also say for two cases a simple if/else would be better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants