nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.62k stars 623 forks source link

consistentSwitchCaseSpacing can't match SwiftLint #1726

Open jshier opened 3 weeks ago

jshier commented 3 weeks ago

I was eager to try the new consistentSwitchCaseSpacing rule. However, without any configuration it can't match the styling SwiftLint checks with its vertical_whitespace_between_cases rule. Namely, it has a few points.

  1. Single line cases don't have the rule enforced, so case value: "returnValue" doesn't need spacing.
  2. Extraneous spacing around single line cases is ignored, similar to the various "keep" settings for other rules.
  3. Only when the case has more than one line is the spacing enforced, and we want it always enforced.

For instance, this is a common pattern:

switch value {
  case .one: "one"
  case .two: "two"

  case .three: "three" // This is special.

  default:
    break
}

or even just

switch value {
  case .one: "one"
  case .two: "two"
  case .three: "three"

  default:
    break
}

but this would have enforced spacing because the cases have a second line:

switch value {
  case .one: 
    "one"

  case .two: 
    "two"

  case .three: 
    "three"

  default:
    break
}

So it would be nice if the rule could be configured to match the SwiftLint behavior, or otherwise have options.