Skip to content

Commit

Permalink
Add SwiftUI Property declaration to sub section ordering list (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-jimenez-0529 authored Aug 1, 2024
1 parent a09950f commit 77b4f2d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ let package = Package(

.binaryTarget(
name: "swiftformat",
url: "https://github.com/calda/SwiftFormat/releases/download/0.55-beta-6/SwiftFormat.artifactbundle.zip",
checksum: "c4faeb1068eece2b644192afc1a35a82a81935794034d2efaa3f44ddd8a4b8d4"),
url: "https://github.com/calda/SwiftFormat/releases/download/0.55-beta-9/SwiftFormat.artifactbundle.zip",
checksum: "95bdb70c7f236c1208a96595193cda17ec188630efd7cee35e3d210160a01e5f"),

.binaryTarget(
name: "SwiftLintBinary",
Expand Down
116 changes: 92 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3886,51 +3886,119 @@ _You can enable the following settings in Xcode by running [this script](resourc
* <a id='subsection-organization'></a>(<a href='#subsection-organization'>link</a>) **Within each top-level section, place content in the following order.** This allows a new reader of your code to more easily find what they are looking for. [![SwiftFormat: organizeDeclarations](https://img.shields.io/badge/SwiftFormat-organizeDeclarations-008489.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#organizeDeclarations)
* Nested types and type aliases
* Static properties
* Class properties
* Static property with body
* Class properties with body
* SwiftUI dynamic properties (@State, @Environment, @Binding, etc)
* Instance properties
* Instance properties with body
* Static methods
* Class methods
* Instance methods

* <a id='newline-between-subsections'></a>(<a href='#newline-between-subsections'>link</a>) **Add empty lines between property declarations of different kinds.** (e.g. between static properties and instance properties.) [![SwiftFormat: organizeDeclarations](https://img.shields.io/badge/SwiftFormat-organizeDeclarations-008489.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#organizeDeclarations)

<details>

Computed properties and properties with property observers should appear at the end of the set of declarations of the same kind. (e.g. instance properties.)

```swift
// WRONG
static let gravityEarth: CGFloat = 9.8
static let gravityMoon: CGFloat = 1.6
var gravity: CGFloat
```swift
// WRONG
class PlanetView: UIView {
static var startOfTime { -CGFloat.greatestFiniteMagnitude / 0 }

// RIGHT
static let gravityEarth: CGFloat = 9.8
static let gravityMoon: CGFloat = 1.6
var atmosphere: Atmosphere {
didSet {
print("oh my god, the atmosphere changed")
}
}

var gravity: CGFloat
```
override class var layerClass: AnyClass {
PlanetLayer.self
}

var gravity: CGFloat

static let speedOfLight: CGFloat = 300_000
}

// RIGHT
class PlanetView: UIView {

static let speedOfLight: CGFloat = 300_000
static var startOfTime { -CGFloat.greatestFiniteMagnitude / 0 }

override class var layerClass: AnyClass {
PlanetLayer.self
}

var gravity: CGFloat
var atmosphere: Atmosphere {
didSet {
print("oh my god, the atmosphere changed")
}
}
}
```

SwiftUI Properties are a special type of property that lives inside SwiftUI views. These views conform to the [`DynamicProperty`](https://developer.apple.com/documentation/swiftui/dynamicproperty) protocol and cause the view's body to re-compute. Given this common functionality and also a similar syntax, it is preferred to group them.

```swift
// WRONG

struct CustomSlider: View {

// MARK: Internal

var body: some View {
...
}

// MARK: Private

@Binding private var value: Value
private let range: ClosedRange<Double>
@Environment(\.sliderStyle) private var style
private let step: Double.Stride
@Environment(\.layoutDirection) private var layoutDirection
}

// RIGHT

struct CustomSlider: View {

// MARK: Internal

var body: some View {
...
}

// MARK: Private

@Environment(\.sliderStyle) private var style
@Environment(\.layoutDirection) private var layoutDirection
@Binding private var value: Value

private let range: ClosedRange<Double>
private let step: Double.Stride
}
```
</details>

* <a id='computed-properties-at-end'></a>(<a href='#computed-properties-at-end'>link</a>) **Computed properties and properties with property observers should appear at the end of the set of declarations of the same kind.** (e.g. instance properties.) [![SwiftFormat: organizeDeclarations](https://img.shields.io/badge/SwiftFormat-organizeDeclarations-008489.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#organizeDeclarations)

* <a id='newline-between-subsections'></a>(<a href='#newline-between-subsections'>link</a>) **Add empty lines between property declarations of different kinds.** (e.g. between static properties and instance properties.) [![SwiftFormat: organizeDeclarations](https://img.shields.io/badge/SwiftFormat-organizeDeclarations-008489.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#organizeDeclarations)

<details>

```swift
// WRONG
var atmosphere: Atmosphere {
didSet {
print("oh my god, the atmosphere changed")
}
}
static let gravityEarth: CGFloat = 9.8
static let gravityMoon: CGFloat = 1.6
var gravity: CGFloat

// RIGHT
static let gravityEarth: CGFloat = 9.8
static let gravityMoon: CGFloat = 1.6

var gravity: CGFloat
var atmosphere: Atmosphere {
didSet {
print("oh my god, the atmosphere changed")
}
}
```

</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 @@ -28,7 +28,7 @@
--enumthreshold 20 # organizeDeclarations
--organizetypes class,struct,enum,extension,actor # organizeDeclarations
--visibilityorder beforeMarks,instanceLifecycle,open,public,package,internal,fileprivate,private # organizeDeclarations
--typeorder nestedType,staticProperty,staticPropertyWithBody,classPropertyWithBody,instanceProperty,instancePropertyWithBody,staticMethod,classMethod,instanceMethod # organizeDeclarations
--typeorder nestedType,staticProperty,staticPropertyWithBody,classPropertyWithBody,swiftUIPropertyWrapper,instanceProperty,instancePropertyWithBody,staticMethod,classMethod,instanceMethod # organizeDeclarations
--extensionacl on-declarations # extensionAccessControl
--patternlet inline # hoistPatternLet
--redundanttype inferred # redundantType, propertyType
Expand Down

0 comments on commit 77b4f2d

Please sign in to comment.