Skip to content

Commit

Permalink
Update infix operator spacing rule to clarify rule for operator defin…
Browse files Browse the repository at this point in the history
…itions like 'static func ==(lhs: T, rhs: T) -> Bool' (#286)
  • Loading branch information
calda authored Aug 21, 2024
1 parent 88926c4 commit 5a74d86
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2450,24 +2450,34 @@ _You can enable the following settings in Xcode by running [this script](resourc

### Operators

* <a id='infix-operator-spacing'></a>(<a href='#infix-operator-spacing'>link</a>) **Infix operators should have a single space on either side.** Prefer parenthesis to visually group statements with many operators rather than varying widths of whitespace. This rule does not apply to range operators (e.g. `1...3`) and postfix or prefix operators (e.g. `guest?` or `-1`). [![SwiftFormat: spaceAroundOperators](https://img.shields.io/badge/SwiftFormat-spaceAroundOperators-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#spacearoundoperators)
* <a id='infix-operator-spacing'></a>(<a href='#infix-operator-spacing'>link</a>) **Infix operators should have a single space on either side.** However, in operator definitions, omit the trailing space between the operator and the open parenthesis. This rule does not apply to range operators (e.g. `1...3`). [![SwiftFormat: spaceAroundOperators](https://img.shields.io/badge/SwiftFormat-spaceAroundOperators-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#spacearoundoperators)

<details>

```swift
// WRONG
let capacity = 1+2
let capacity = currentCapacity ?? 0
let mask = (UIAccessibilityTraitButton|UIAccessibilityTraitSelected)
let capacity = currentCapacity??0
let capacity=newCapacity
let latitude = region.center.latitude - region.span.latitudeDelta/2.0
let latitude = region.center.latitude-region.span.latitudeDelta/2.0

// RIGHT
let capacity = 1 + 2
let capacity = currentCapacity ?? 0
let mask = (UIAccessibilityTraitButton | UIAccessibilityTraitSelected)
let capacity = newCapacity
let latitude = region.center.latitude - (region.span.latitudeDelta / 2.0)
let latitude = region.center.latitude - region.span.latitudeDelta / 2.0
```

```swift
// WRONG
static func == (_ lhs: MyView, _ rhs: MyView) -> Bool {
lhs.id == rhs.id
}

// RIGHT
static func ==(_ lhs: MyView, _ rhs: MyView) -> Bool {
lhs.id == rhs.id
}
```

</details>
Expand Down
2 changes: 1 addition & 1 deletion Sources/AirbnbSwiftFormatTool/airbnb.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
--redundanttype inferred # redundantType, propertyType
--typeblanklines preserve # blankLinesAtStartOfScope, blankLinesAtEndOfScope
--emptybraces spaced # emptyBraces
--operatorfunc no-space # spaceAroundOperators
--ranges preserve # spaceAroundOperators
--operatorfunc no-space # spaceAroundOperators
--someAny disabled # opaqueGenericParameters
--elseposition same-line # elseOnSameLine
--guardelse next-line # elseOnSameLine
Expand Down

0 comments on commit 5a74d86

Please sign in to comment.