nicoespeon / abracadabra

Automated refactorings for VS Code (JS & TS) ✨ It's magic ✨
https://marketplace.visualstudio.com/items?itemName=nicoespeon.abracadabra
MIT License
805 stars 48 forks source link

Convert Switch case with only a default case to if-statement #568

Closed 11joselu closed 2 years ago

11joselu commented 2 years ago

Describe what's missing

Convert Switch case with only a default case to if-statement

How would it work

Given this input

 switch (true) {
    default:
        myVar = 'Hello';
        break;
    }

Expected behavior

The output should be

if (true) {
  myVar = 'Hello';
 }

Screenshots

Right now the extension is not able to detect this case

image

Additional information

How we arrive into a switch with only a default case?

Doing Tennis Refactoring Kata After some refactor I endend at L34 with

switch (tempScore) {
      default:
        score += scores[tempScore];
        break;
    }

To simplify more the code. I can convert that switch to if an then apply "Remove Dead Code". vscode-abracadabra

nicoespeon commented 2 years ago

Excellent suggestion and thanks for the detailed report @11joselu 👏 The Tennis refactoring kata is a great one. Hopefully, it will be even faster to do with Abracadabra now 😉

@all-contributors please add @11joselu for ideas

allcontributors[bot] commented 2 years ago

@nicoespeon

I've put up a pull request to add @11joselu! :tada: