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

/// keep-sorted for switch statements #17

Open
3 tasks done
GauBen opened this issue May 14, 2024 · 2 comments
Open
3 tasks done

/// keep-sorted for switch statements #17

GauBen opened this issue May 14, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@GauBen
Copy link
Contributor

GauBen commented May 14, 2024

Clear and concise description of the problem

Hey there, I'm back with another suggestion: /// keep-sorted for switch statements. The idea comes from this file

Suggested solution

Basic

/// keep-sorted
switch (x) {
  case "b":
    doB();
    break
  case "a":
    doA();
    break;
}

// ->

/// keep-sorted
switch (x) {
  case "a":
    doA();
    break
  case "b":
    doB();
    break;
}

Complex/weird

/// keep-sorted
switch (x) {
  case "fallback":
    falling();
  case "c":
  case "b":
    common();
    break;
  case "d":
  case "a":
    aOrD();
    break;
}

// ->

/// keep-sorted
switch (x) {
  case "a": // `a` comes before `fallback`
  case "d":
    aOrD();
    break;
  case "fallback":
    falling();
  case "b":
  case "c":
    common();
    break;
}

Alternative

No response

Additional context

No response

Validations

@GauBen GauBen added the enhancement New feature or request label May 14, 2024
@antfu
Copy link
Owner

antfu commented May 14, 2024

I am not so sure about this. Switch is order sensetive, and this move could potentially change the logic by a lot. personally, I don't think it's a common case where you write switch with orthogonal logic while having so many cases to sort.

@GauBen
Copy link
Contributor Author

GauBen commented May 14, 2024

Maybe if the rule is limited to static identifiers and forbids falling-through cases (only aliases, like the file in the first message) it might be safe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants