eslint-plugin-command icon indicating copy to clipboard operation
eslint-plugin-command copied to clipboard

`/// keep-sorted` for `switch` statements

Open GauBen opened this issue 1 year ago • 2 comments

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

  • [X] Follow our Code of Conduct
  • [X] Read the Contributing Guide.
  • [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

GauBen avatar May 14 '24 17:05 GauBen

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.

antfu avatar May 14 '24 18:05 antfu

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

GauBen avatar May 14 '24 21:05 GauBen