Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged

Conversation

calda
Copy link
Collaborator

@calda calda commented Feb 1, 2024

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"
  }

@calda calda force-pushed the cal--switch-statement-blank-lines branch from b7972fb to 03a7cc7 Compare February 1, 2024 23:15
Copy link

codecov bot commented Feb 1, 2024

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 ⚠️
Additional details and impacted files
@@             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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nicklockwood nicklockwood merged commit f2ca8ca into nicklockwood:develop Feb 3, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants