Skip to content

New rule: sorted_collection_members - #6921

Draft
ZevEisenberg wants to merge 7 commits into
realm:mainfrom
ZevEisenberg:sorted-collection-members-rule
Draft

ZevEisenberg wants to merge 7 commits into
realm:mainfrom
ZevEisenberg:sorted-collection-members-rule

Conversation

@ZevEisenberg

@ZevEisenberg ZevEisenberg commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

I've got some more work I want to do in order to polish this up, but I wanted to get some early feedback on whether a rule like this would be accepted.

A couple of questions:

  • what would be the right syntax for a one-off enable or disable on a collection literal?
  • I didn't make it a correctable rule, because I didn't want to have to deal with preserving comments that may be interspersed. But it could probably be added, as long as the rule is either "can't autocorrect if there are interspersed comments" or "any comment lines stay stuck to the collection member that they are above."

Motivating use case: we have some arrays in our code, particularly in Package.swift, that look like this:

let package = Package(
  name: "Packages",
  platforms: [
    ...
  ],
  products: [
    ...
  ],
  dependencies: [
    ...
  ],
  targets: [
    // Please keep this array alphabetized!

    .target(
      name: "ACoolFeature",
      dependencies: [
        ...
      ]
    ),
    .target(
      name: "AnotherCoolFeature",
      dependencies: [
        ...
      ]
    ),
    ...
  ]
)

Instead of imploring future developers to keep a collection literal's members alphabetized, I'd like to propose an opt-in rule that can keep us honest:

// enable it once for a file or declaration, but you'd probably never want it enabled app-wide.

// swiftlint:enable sorted_collection_members
let package = Package(
  name: "Packages",
  platforms: [ // swiftlint:disable:this sorted_collection_members
    // enable or disable it ad-hoc if you need to
    ...
  ],
  products: [
    ...
  ],
  dependencies: [
    ...
  ],
  targets: [
    .target(
      name: "ACoolFeature",
      dependencies: [
        ...
      ]
    ),
    .target(
      name: "AnotherCoolFeature",
      dependencies: [
        ...
      ]
    ),
    ...
  ]
)
// swiftlint:disable sorted_collection_members

@ZevEisenberg
ZevEisenberg force-pushed the sorted-collection-members-rule branch from 0f0fc91 to 285c2ca Compare September 24, 2026 21:38
@ZevEisenberg
ZevEisenberg force-pushed the sorted-collection-members-rule branch from 285c2ca to 0a412a4 Compare September 24, 2026 21:45
@ZevEisenberg

Copy link
Copy Markdown
Contributor Author

Here's one possible weakness with this approach: it appears that, if a rule is disabled or not-opted-in globally, it cannot be enabled with swiftlint:enable:this sorted_collection_members on a per-literal basis. Does this sound right, and if so, is it something you'd be willing to entertain a PR to change?

@ZevEisenberg

ZevEisenberg commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor Author

If that's too drastic a change for this repo, I was able to get something similar working in SwiftFormat, so I may just go with that. I'll leave this PR open until I hear from a maintainer, though.

This branch has not been deployed

No deployments
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.

1 participant