Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SwiftUI properties sort #289

Merged
merged 4 commits into from
Sep 3, 2024
Merged

Add SwiftUI properties sort #289

merged 4 commits into from
Sep 3, 2024

Conversation

miguel-jimenez-0529
Copy link
Contributor

@miguel-jimenez-0529 miguel-jimenez-0529 commented Aug 22, 2024

Summary

This PR adds the SwiftUI property declaration sort rule to the style guide.

Reasoning

its preferred that SwiftUI properties are sorted by their dynamic property type. The order is determined by the first time a type appears:

    // 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<Double>
      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<Double>
      private let step: Double.Stride
    }

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@@ -25,6 +25,7 @@
--typeattributes prev-line # wrapAttributes
--wrapternary before-operators # wrap
--structthreshold 20 # organizeDeclarations
--sortswiftuiprops first-appearance-sort
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@calda I noticed there is a #something at the end of each, not sure what to add here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the name of the rule that uses the argument, so:

Suggested change
--sortswiftuiprops first-appearance-sort
--sortswiftuiprops first-appearance-sort #organizeDeclarations

Copy link
Member

@calda calda Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put this lower down below the --typeorder option

@miguel-jimenez-0529 miguel-jimenez-0529 marked this pull request as ready for review August 22, 2024 16:56
Copy link
Member

@calda calda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@jqsilver
Copy link
Contributor

Why not have a standardized order for property types?

@miguel-jimenez-0529
Copy link
Contributor Author

Why not have a standardized order for property types?

@jqsilver we don't want to have a strong opinion on how folks define their modifiers but at the same time we want some consistency. This rule feels like a good balance between allowing you to define the order you want but ensuring a consistent order.

@calda calda merged commit 0e09033 into master Sep 3, 2024
5 checks passed
@calda calda deleted the mj-swift-ui-properties-sort branch September 3, 2024 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants