From 0e0903313758cec86bfd85f2a6c698a4c173ce2c Mon Sep 17 00:00:00 2001 From: Miguel Jimenez Date: Tue, 3 Sep 2024 17:48:10 -0600 Subject: [PATCH] Add SwiftUI properties sort (#289) --- Package.swift | 4 +-- README.md | 33 ++++++++++++++++++- .../AirbnbSwiftFormatTool/airbnb.swiftformat | 1 + 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Package.swift b/Package.swift index 0e901c2..09955a2 100644 --- a/Package.swift +++ b/Package.swift @@ -42,8 +42,8 @@ let package = Package( .binaryTarget( name: "swiftformat", - url: "https://github.com/calda/SwiftFormat/releases/download/0.55-beta-11/SwiftFormat.artifactbundle.zip", - checksum: "421884ecccc34b75135e9aa4b80e21ee6d9985084896fe8d58e07b28d9a2e0f6"), + url: "https://github.com/calda/SwiftFormat/releases/download/0.55-beta-12/SwiftFormat.artifactbundle.zip", + checksum: "8783cefc0837416759f81064df7907cc60ddca6d3f8c3b301b123a2193a8585b"), .binaryTarget( name: "SwiftLintBinary", diff --git a/README.md b/README.md index a31400b..c86de79 100644 --- a/README.md +++ b/README.md @@ -3936,7 +3936,7 @@ _You can enable the following settings in Xcode by running [this script](resourc * Static properties * Static property with body * Class properties with body - * SwiftUI dynamic properties (@State, @Environment, @Binding, etc) + * SwiftUI dynamic properties (@State, @Environment, @Binding, etc), grouped by type * Instance properties * Instance properties with body * Static methods @@ -4029,6 +4029,37 @@ _You can enable the following settings in Xcode by running [this script](resourc private let step: Double.Stride } ``` + + Additionally, within the grouping of SwiftUI properties, it is preferred that the properties are also grouped by their dynamic property type. The group order applied by the formatter is determined by the first time a type appears: + + ```swift + // WRONG + struct CustomSlider: View { + + @Binding private var value: Value + @State private var foo = Foo() + @Environment(\.sliderStyle) private var style + @State private var bar = Bar() + @Environment(\.layoutDirection) private var layoutDirection + + private let range: ClosedRange + private let step: Double.Stride + } + + // RIGHT + struct CustomSlider: View { + + @Binding private var value: Value + @State private var foo = Foo() + @State private var bar = Bar() + @Environment(\.sliderStyle) private var style + @Environment(\.layoutDirection) private var layoutDirection + + private let range: ClosedRange + private let step: Double.Stride + } + ``` + diff --git a/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat b/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat index 34fab1c..2873a4d 100644 --- a/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat +++ b/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat @@ -29,6 +29,7 @@ --organizetypes class,struct,enum,extension,actor # organizeDeclarations --visibilityorder beforeMarks,instanceLifecycle,open,public,package,internal,fileprivate,private # organizeDeclarations --typeorder nestedType,staticProperty,staticPropertyWithBody,classPropertyWithBody,swiftUIPropertyWrapper,instanceProperty,instancePropertyWithBody,staticMethod,classMethod,instanceMethod # organizeDeclarations +--sortswiftuiprops first-appearance-sort #organizeDeclarations --extensionacl on-declarations # extensionAccessControl --patternlet inline # hoistPatternLet --redundanttype inferred # redundantType, propertyType