Skip to content

Add ignore_codingkeys parameter to nesting#5650

Merged
SimplyDanny merged 15 commits intorealm:mainfrom
braker1nine:ignore_coding_key
Jul 9, 2025
Merged

Add ignore_codingkeys parameter to nesting#5650
SimplyDanny merged 15 commits intorealm:mainfrom
braker1nine:ignore_coding_key

Conversation

@braker1nine
Copy link
Contributor

Resolves #5641

This very explicitly targets enum's named CodingKeys which conform to CodingKey. To provide an exception for Codable users who have to provide custom keys.

OK ✅

struct Outer {
    struct Inner: Codable {
        let identifier: Int
        enum CodingKeys: String, CodingKey {
            case identifier = "id"
        }
    }
}

Not OK ❌

struct Outer {
    struct Inner {
        let identifier: Int
        enum SomeKeys: String, CodingKey {
            case identifier = "id"
        }
    }
}

@SwiftLintBot
Copy link

SwiftLintBot commented Jul 2, 2024

18 Messages
📖 Building this branch resulted in a binary size of 25246.57 KiB vs 25245.04 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 1.05 s vs 1.05 s on main (0% slower).
📖 Linting Alamofire with this PR took 1.41 s vs 1.41 s on main (0% slower).
📖 Linting Brave with this PR took 9.75 s vs 9.75 s on main (0% slower).
📖 Linting DuckDuckGo with this PR took 25.82 s vs 25.85 s on main (0% faster).
📖 Linting Firefox with this PR took 13.67 s vs 13.69 s on main (0% faster).
📖 Linting Kickstarter with this PR took 11.19 s vs 11.07 s on main (1% slower).
📖 Linting Moya with this PR took 0.56 s vs 0.56 s on main (0% slower).
📖 Linting NetNewsWire with this PR took 3.21 s vs 3.2 s on main (0% slower).
📖 Linting Nimble with this PR took 0.85 s vs 0.85 s on main (0% slower).
📖 Linting PocketCasts with this PR took 9.22 s vs 9.02 s on main (2% slower).
📖 Linting Quick with this PR took 0.49 s vs 0.49 s on main (0% slower).
📖 Linting Realm with this PR took 4.86 s vs 4.85 s on main (0% slower).
📖 Linting Sourcery with this PR took 2.47 s vs 2.47 s on main (0% slower).
📖 Linting Swift with this PR took 5.66 s vs 5.64 s on main (0% slower).
📖 Linting VLC with this PR took 1.48 s vs 1.48 s on main (0% slower).
📖 Linting Wire with this PR took 22.8 s vs 22.73 s on main (0% slower).
📖 Linting WordPress with this PR took 12.89 s vs 12.93 s on main (0% faster).

Generated by 🚫 Danger

Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution! Looks good in principle.

What about the following example?

struct Outer {
  enum CodingKeys: String, CodingKey {
    case id
    
    struct S {}
  }
}

I think, the rule should trigger on S.

You need to run IntegrationTests locally and fix all linting issues it reports and update the configuration set reference file.

@braker1nine
Copy link
Contributor Author

FYI I'm out of town until next weekend. I'll jump back into this once I'm back

@braker1nine
Copy link
Contributor Author

FYI I'm out of town until next weekend. I'll jump back into this once I'm back

It wasn't a year long vacation but here we are a year later 😓

What about the following example?

struct Outer {
  enum CodingKeys: String, CodingKey {
    case id
    
    struct S {}
  }
}

I think, the rule should trigger on S.

I've updated this so that this will now trigger the rule.

You need to run IntegrationTests locally and fix all linting issues it reports and update the configuration set reference file.

I think I've got this done? I moved the EnumDeclSyntax extension into its own file because the SwiftSyntax+SwiftLint file was too long and failing linting.

I don't fully understand what is failing now?

Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

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

Glad you are back! 🥳 I have a few more comments ...

verifyRule(description, ruleConfiguration: ["ignore_typealiases_and_associatedtypes": true])
}

func testNestingWithoutCodingKeys() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can move these examples directly into NestingRuleExamples. Any options deviating from the default will be added to the online documentation, so it's clear why an example triggers or why it doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I might need just a little bit more clarification what you mean by this? I was thinking because these are not using the default configuration they shouldn't be in the default examples.

But you're saying they should?

Copy link
Collaborator

Choose a reason for hiding this comment

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

In the documentation that includes these examples later, we now also show the configuration in case it deviates from the default one. So having them as normal examples is even nicer as people also see how options influence the rule's behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahhh okay I didn't realize that Example has a configuration option. That's why I was confused!

Okay I updated these and I removed the tests. Which I'm not 100% sure about... but I thought the testing covered all the default examples. Is that wrong?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The feature that configurations are added to the examples in the documentation is quite new and we won't convert existing tests. But we should prefer this way of documenting examples from now on.

@SimplyDanny
Copy link
Collaborator

For the build, please rebase onto the current main. The results are outdated and there is conflict as well.

Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

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

Looks good now. Thanks for the updates, @braker1nine!

@SimplyDanny SimplyDanny enabled auto-merge (squash) July 9, 2025 18:43
@SimplyDanny SimplyDanny merged commit c97cf24 into realm:main Jul 9, 2025
20 checks passed
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.

Rule Request: Ignore CodingKey enums in Nesting

3 participants