eslint-plugin-perfectionist
eslint-plugin-perfectionist copied to clipboard
Feature: (sort-switch-case) Would you consider sorting the `case` blocks in a `switch` statement?
Describe the rule
Can you add a sort-switch-case rule? It will sort the case statements, their order does not affect the execution result, but it can improve readability, reduce code merge conflicts, and eliminate the need to worry about their order when writing code.
Code example
Input
switch (type) {
case 'save': {
break;
}
case 'edit': {
break;
}
default:
case 'close': {
break;
}
}
Output
switch (type) {
default:
case 'close': {
break;
}
case 'edit': {
break;
}
case 'save': {
break;
}
}
Additional comments
No response
Validations
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
That's a cool idea. I think we should add that.
The form switch(true) will become more popular with the release of typescript 5.3 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html#switch-true-narrowing
That also makes this feature more difficult to implement if switching the order of cases causes type errors, but maybe that is just revealing a bug
@Pyrolistical Yes, thank you for the comment. Safety is our priority.
Thank you for your issue!
It was a great idea. If you have any more suggestions for improving the plugin, feel free to create a new issue.
This feature was implemented in 5311118ebea707e0b885c4cf6ecbd84d10ea08d2 and released in v3.0.0.
If you'd like, you can support the release with a retweet: https://x.com/azat_io_en/status/1815367279191761054
@azat-io Hi, thank you for implementing this!! but it seems that it cannot be used together with unicorn/switch-case-braces. And there is a slight issue with sorting when case and default are used together.