nicklockwood / SwiftFormat

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

Add new rules related to blank lines in switch statements #1621

Closed calda closed 5 months ago

calda commented 5 months ago

This PR adds two new rules related to blank lines in switch statements.

blankLineAfterMultilineSwitchCase

This rule inserts a blank line following any switch statement case that spans multiple lines:

  func handle(_ action: SpaceshipAction) {
      switch action {
      case .engageWarpDrive:
          navigationComputer.destination = targetedDestination
          await warpDrive.spinUp()
          warpDrive.activate()
+
      case let .scanPlanet(planet):
          scanner.target = planet
          scanner.scanAtmosphere()
          scanner.scanBiosphere()
          scanner.scanForArticialLife()
+
      case .handleIncomingEnergyBlast:
          await energyShields.prepare()
          energyShields.engage()
      }
  }

consistentSwitchStatementSpacing

This rule ensures that the cases in a switch statement have consistent spacing. If the majority of cases have a blank line, then all cases should have a blank line. This is complimentary with the above rule, and improves cases like this:

  func handle(_ action: SpaceshipAction) {
      switch action {
      case .engageWarpDrive:
          navigationComputer.destination = targetedDestination
          await warpDrive.spinUp()
          warpDrive.activate()

      case .enableArtificialGravity:
          artificialGravityEngine.enable(strength: .oneG)
+
      case let .scanPlanet(planet):
          scanner.target = planet
          scanner.scanAtmosphere()
          scanner.scanBiosphere()
          scanner.scanForArtificialLife()

      case .handleIncomingEnergyBlast:
          energyShields.engage()
      }
  }

This rule also works in the other direction, and will remove unnecessary blank lines if the majority of cases don't have one:

  var name: PlanetType {
  switch self {
  case .mercury:
      "Mercury"
-
  case .venus:
      "Venus"
  case .earth:
      "Earth"
  case .mars:
      "Mars"
-
  case .jupiter:
      "Jupiter"
  case .saturn:
      "Saturn"
  case .uranus:
      "Uranus"
  case .neptune:
      "Neptune"
  }

Combined example

These two rules compliment each other and combine nicely. When both rules are enabled, they will take a complex switch statement like the one below and add a blank line after all of the cases:

  func handle(_ action: SpaceshipAction) {
      switch action {
      case .engageWarpDrive:
          navigationComputer.destination = targetedDestination
          await warpDrive.spinUp()
          warpDrive.activate()
+
      case .enableArtificialGravity:
          artificialGravityEngine.enable(strength: .oneG)
+
      case let .scanPlanet(planet):
          scanner.target = planet
          scanner.scanAtmosphere()
          scanner.scanBiosphere()
          scanner.scanForArtificialLife()
+
      case .handleIncomingEnergyBlast:
          energyShields.engage()
      }
  }

while preserving the common pattern where simple switch statements don't use any blank lines:

  // Unchanged!
  var name: PlanetType {
  switch self {
  case .mercury:
      "Mercury"
  case .venus:
      "Venus"
  case .earth:
      "Earth"
  case .mars:
      "Mars"
  case .jupiter:
      "Jupiter"
  case .saturn:
      "Saturn"
  case .uranus:
      "Uranus"
  case .neptune:
      "Neptune"
  }
codecov[bot] commented 5 months ago

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (0fb8edc) 95.07% compared to head (d96e18b) 95.15%.

Files Patch % Lines
Sources/FormattingHelpers.swift 98.88% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #1621 +/- ## =========================================== + Coverage 95.07% 95.15% +0.07% =========================================== Files 20 20 Lines 22095 22319 +224 =========================================== + Hits 21007 21237 +230 + Misses 1088 1082 -6 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.