[styleguide-attributes] - Splits up attributes preferences to inline for functions, properties, and closures and separate line for classes, structs, and actors - MOB
MODIFY
.github/STYLEGUIDE.md
.github/STYLEGUIDE.md
@@ -743,13 +743,13 @@ Each guide is broken into a few sections. Sections contain a list of guidelines.
```
</details>
-* **PREFER attributes on the same line as what they are attributing.**
+* **PREFER attributes on the same line as what they are attributing for properties, functions, and closures.**
<details>
#### Why?
- Because we naturally read lines of code as a consistent line, introducing whitespace makes it that much harder to understand the full context of the line you're trying to understand. This is the same reasoning used for preferring a return on the same line as the final parameter in a function declaration.
+ Because we naturally read lines of code as a consistent line, introducing whitespace makes it that much harder to understand the full context of the line you're trying to understand. This is the same reasoning used for preferring a return on the same line as the final parameter in a function declaration. A notable exception to this preference is the `@available` attribute which should always be on its own line.
NOTE: This does not come at the cost of sanity. If you have an exceptionally large number of attributions then it might indicate that you've gotten overzealous, and it actually obscures your meaning, rather than add to it.
@@ -765,10 +765,60 @@ Each guide is broken into a few sections. Sections contain a list of guidelines.
// ...
}
+ // WRONG
+ @available(iOS 14.0, *) func doSomething() {
+ // ...
+ }
+
// RIGHT
@State var stateVar: Bool = true
@discardableResult func thing() -> Bool { true }
- @MainActor class MyClass {
+ @MainActor @ViewBuilder func createViews() {
+ // ...
+ }
+
+ // RIGHT
+ @available(iOS 14.0, *)
+ func doSomething() {
+ // ...
+ }
+ ```
+ </details>
+
+* **PREFER attributes on a separate line as what they are attributing for classes, structs, and actors.**
+
+ <details>
+
+ #### Why?
+ This is the convention set by Apple in the [Swift attribute docs](https://docs.swift.org/swift-book/ReferenceManual/Attributes.html). A notable exception to this preference is the `@available` attribute which should always be on its own line.
+
+
+ ```swift
+ // WRONG
+ @dynamicCallable class MyClass {
+ // ...
+ }
+
+ // WRONG
+ @MainActor struct MyStruct {
+ // ...
+ }
+
+ // WRONG
+ @available(iOS 14.0, *) @MainActor
+ struct MyStruct {
+ // ...
+ }
+
+ // RIGHT
+ @dynamicCallable class MyClass {
+ // ...
+ }
+
+ // RIGHT
+ @available(iOS 14.0, *)
+ @propertyWrapper
+ struct MyPropertyWrapper {
// ...
}
```

[styleguide-attributes] - Splits up attributes preferences to inline for functions, properties, and closures and separate line for classes, structs, and actors - MOB