Skip to content

Commit

Permalink
Add rule to insert a blank line following a switch case with a multi-…
Browse files Browse the repository at this point in the history
…line body
  • Loading branch information
calda committed Feb 2, 2024
1 parent 508132e commit 369bee9
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,161 @@ _You can enable the following settings in Xcode by running [this script](resourc

</details>

* <a id='blank-line-after-multiline-switch-case'></a>(<a href='#blank-line-after-multiline-switch-case'>link</a>) **Insert a blank line following a switch case with a multi-line body.** For consistency within a switch statement, if any case has a multi-line body then all cases should include a trailing blank line. The last switch case doesn't need a blank line, since it is already followed by a closing brace. [![SwiftFormat: blankLineAfterMultilineSwitchCase](https://img.shields.io/badge/SwiftFormat-blankLineAfterMultilineSwitchCase-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#blankLineAfterMultilineSwitchCase) [![SwiftFormat: consistentSwitchStatementSpacing](https://img.shields.io/badge/SwiftFormat-consistentSwitchStatementSpacing-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#consistentSwitchStatementSpacing)

<details>

#### Why?

Like with [declarations in a file](https://github.com/airbnb/swift#newline-between-scope-siblings), inserting a blank line between scopes makes them easier to visually differentiate.

Switch statements with multi-lines lines can become visiually busy and cramped without blank lines between the cases. In this example, all of the cases start to bleed together, making it more difficult to read the code at a glance:

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

Blank lines between the individual cases make the switch statement much easier to read:

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

case .enableArtificialGravity:
artificialGravityEngine.enable(strength: .oneG)

case .scanPlanet(let planet):
scanner.target = planet
scanner.scanAtmosphere()
scanner.scanBiosphere()
scanner.scanForArtificialLife()

case .handleIncomingEnergyBlast:
energyShields.engage()
}
}
```

#### Examples

```swift
// WRONG. These switch cases should be followed by a blank line.
func handle(_ action: SpaceshipAction) {
switch action {
case .engageWarpDrive:
navigationComputer.destination = targetedDestination
warpDrive.spinUp()
warpDrive.activate()
case .enableArtificialGravity:
artificialGravityEngine.enable(strength: .oneG)
case .scanPlanet(let planet):
scanner.target = planet
scanner.scanAtmosphere()
scanner.scanBiosphere()
scanner.scanForArtificialLife()
case .handleIncomingEnergyBlast:
energyShields.engage()
}
}

// WRONG. While the `.enableArtificialGravity` case isn't multi-line, the other cases are.
// For consistency, it should also include a trailing blank line.
func handle(_ action: SpaceshipAction) {
switch action {
case .engageWarpDrive:
navigationComputer.destination = targetedDestination
warpDrive.spinUp()
warpDrive.activate()

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

case .handleIncomingEnergyBlast:
energyShields.engage()
}
}

// RIGHT. All of the cases have a trailing blank line.
func handle(_ action: SpaceshipAction) {
switch action {
case .engageWarpDrive:
navigationComputer.destination = targetedDestination
warpDrive.spinUp()
warpDrive.activate()

case .enableArtificialGravity:
artificialGravityEngine.enable(strength: .oneG)

case .scanPlanet(let planet):
scanner.target = planet
scanner.scanAtmosphere()
scanner.scanBiosphere()
scanner.scanForArtificialLife()

case .handleIncomingEnergyBlast:
energyShields.engage()
}
}

// RIGHT. Since none of the cases are multi-line, blank lines are not required.
func handle(_ action: SpaceshipAction) {
switch action {
case .engageWarpDrive:
warpDrive.engage()
case .enableArtificialGravity:
artificialGravityEngine.enable(strength: .oneG)
case .scanPlanet(let planet):
scanner.scan(planet)
case .handleIncomingEnergyBlast:
energyShields.engage()
}
}

// ALSO RIGHT. Blank lines are still permitted after single-line switch cases if it helps with readability.
func handle(_ action: SpaceshipAction) {
switch action {
case .engageWarpDrive:
warpDrive.engage()

case .enableArtificialGravity:
artificialGravityEngine.enable(strength: .oneG)

case .scanPlanet(let planet):
scanner.scan(planet)

case .handleIncomingEnergyBlast:
energyShields.engage()
}
}
```

</details>

* <a id='wrap-guard-else'></a>(<a href='#wrap-guard-else'>link</a>) **Add a line break before the `else` keyword in a multi-line guard statement.** For single-line guard statements, keep the `else` keyword on the same line as the `guard` keyword. The open brace should immediately follow the `else` keyword. [![SwiftFormat: elseOnSameLine](https://img.shields.io/badge/SwiftFormat-elseOnSameLine-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#elseOnSameLine)

<details>
Expand Down
2 changes: 2 additions & 0 deletions Sources/AirbnbSwiftFormatTool/airbnb.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,5 @@
--rules preferForLoop
--rules conditionalAssignment
--rules wrapMultilineConditionalAssignment
--rules blankLineAfterMultilineSwitchCase
--rules consistentSwitchStatementSpacing

0 comments on commit 369bee9

Please sign in to comment.