Skip to content

Switch statement indentation #276

@jhaber

Description

@jhaber

I've noticed a few issues with indentation related to switch statements (I can open separate issues if you'd prefer). I was running these examples with:
prettier --write "Example.java" --print-width 90 (I also tried with #255 applied but observed the same behavior)

The first issue seems to manifest when there is a comment on the line preceding the default case. In this case, the default keyword gets indented incorrectly (removing the comment fixes the issue).

Input:

public class Example {

  public int method() {
    switch ("abc") {
      case "a":
        return 1;
      case "b":
        return 2;
      case "c":
        return 3;
      // default case
      default:
        return 3;
    }
  }
}

Output:

public class Example {

  public int method() {
    switch ("abc") {
      case "a":
        return 1;
      case "b":
        return 2;
      case "c":
        return 3;
      // default case
        default:
        return 3;
    }
  }
}

The second issue is when there is a comment on one of the other case labels. When this happens, the whole switch statement appears to get mangled (removing the comment also fixes this issue).

Input:

public class Example {

  public int method() {
    switch ("abc") {
      case "a":
        return 1;
      case "b":
        return 2;
      // case c
      case "c":
        return 3;
      default:
        return 3;
    }
  }
}

Output:

public class Example {

  public int method() {
    switch ("abc") { case "a": return 1; case "b": return 2; // case c
        case "c": return 3; default: return 3; }
  }
}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions