question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cases in switch statements not indented

See original GitHub issue

prettier is printing cases at the same indentation level as their parent switch statement. I indent cases inside a switch statement.

Now:

switch (something) {
case "foo":
  // ...
  break;
case "bar":
  // ...
  break;
}

Ideally:

switch (something) {
  case "foo":
    // ...
    break;
  case "bar":
    // ...
    break;
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
dvlsgcommented, Aug 3, 2017

Just some additional insight into why it may be helpful to indent case statements:

// without braces for case
const val = 'bar';
switch (val) {
case 'foo':
  const b = 'bar';
  break;
case 'bar':
  const b = 'baz'; // SyntaxError: Identifier 'b' has already been declared
  console.log(b);
  break;
} // looks okay
// with braces for each case, no indentation
const val = 'bar';
switch (val) {
case 'foo': {
  const b = 'bar';
  break;
}
case 'bar': {
  const b = 'baz'; // works!
  console.log(b);
  break;
}
} // two braces in a row at the same indent looks odd
// with braces for each case, with indentation
const val = 'bar';
switch (val) {
  case 'foo': {
    const b = 'bar';
    break;
  }
  case 'bar': {
    const b = 'baz'; // works!
    console.log(b);
    break;
  }
} // looks "normal" to me
3reactions
azzcommented, May 3, 2017

My opinion is that due to case ending in a :, it should be treated the same as a label, and not indented.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Why the strange indentation on switch statements?
In Eclipse (Java) the default case of the switch is automatically indented, it is not "un-dented" like the cases are. I would think...
Read more >
Reason for non-indented case statements in switch (go fmt)
I think the reason is that the current formatting already makes clear what's the switch scope and what are the scopes of the...
Read more >
Swift's SWITCH indenting is driving me mad... any way to fix ...
One case for cases being at the same indentation would be that case statements aren't really code. it's more like multiple if statements,...
Read more >
C++ Switch Statement Indentation - Visual Studio Feedback
Using Ctrl-K Ctrl-F to format a switch statement results in switch statement contents being cascaded, not indented by case. The example switch statement...
Read more >
Allow case statement indentation to align with the switch
Create an option to align case statement with its parent switch. ... It is controlled by special option: "Setting -> Editor -> Code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found