-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 ignore_codingkeys
parameter to nesting
#5650
base: main
Are you sure you want to change the base?
Conversation
This only returns true when the name of the enum is CodingKeys and it conforms to CodingKey
This prevents nested CodingKeys (which may be necessary for functioning Codable conformance) from triggering nesting violations
Generated by 🚫 Danger |
There was a problem hiding this 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.
@@ -184,6 +184,25 @@ public extension EnumDeclSyntax { | |||
return rawValueTypes.contains(identifier) | |||
} | |||
} | |||
|
|||
/// True if this enum is a `CodingKey` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// True if this enum is a `CodingKey` | |
/// True if this enum is a `CodingKey`. For that, it has to be named `CodingKeys` and must conform to the `CodingKey` protocol. |
@@ -184,6 +184,25 @@ public extension EnumDeclSyntax { | |||
return rawValueTypes.contains(identifier) | |||
} | |||
} | |||
|
|||
/// True if this enum is a `CodingKey` | |||
var isCodingKey: Bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The enum itself is not a coding key, but it defines the coding keys, doesn't it? So what about the name definesCodingKeys
instead?
return false | ||
} | ||
|
||
guard self.name.text == "CodingKeys" else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guard self.name.text == "CodingKeys" else { | |
guard name.text == "CodingKeys" else { |
Can also be added to the previous guard
.
guard let identifier = element.type.as(IdentifierTypeSyntax.self)?.name.text else { | ||
return false | ||
} | ||
|
||
return identifier == "CodingKey" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guard let identifier = element.type.as(IdentifierTypeSyntax.self)?.name.text else { | |
return false | |
} | |
return identifier == "CodingKey" | |
element.type.as(IdentifierTypeSyntax.self)?.name.text == "CodingKey" |
@@ -64,7 +64,9 @@ private extension NestingRule { | |||
} | |||
|
|||
override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind { | |||
validate(forFunction: false, triggeringToken: node.enumKeyword) | |||
if !(configuration.ignoreCodingKeys && node.isCodingKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if !(configuration.ignoreCodingKeys && node.isCodingKey) { | |
if !configuration.ignoreCodingKeys || !node.isCodingKey { |
.init(""" | ||
struct Outer { | ||
struct Inner { | ||
enum Example: String, CodingKey { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enum Example: String, CodingKey { | |
↓enum Example: String, CodingKey { |
@@ -15,6 +15,8 @@ struct NestingConfiguration: RuleConfiguration { | |||
private(set) var alwaysAllowOneTypeInFunctions = false | |||
@ConfigurationElement(key: "ignore_typealiases_and_associatedtypes") | |||
private(set) var ignoreTypealiasesAndAssociatedtypes = false | |||
@ConfigurationElement(key: "ignore_codingkeys") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ConfigurationElement(key: "ignore_codingkeys") | |
@ConfigurationElement(key: "ignore_coding_keys") |
@@ -16,6 +16,10 @@ | |||
improvements done in the [SwiftSyntax](https://github.com/apple/swift-syntax) | |||
library. | |||
|
|||
* Add `ignore_codingkeys` parameter to `nesting` rule. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain briefly what this option does.
* Add `ignore_codingkeys` parameter to `nesting` rule. | |
* Add `ignore_coding_keys` parameter to `nesting` rule. |
FYI I'm out of town until next weekend. I'll jump back into this once I'm back |
Resolves #5641
This very explicitly targets
enum
's namedCodingKeys
which conform toCodingKey
. To provide an exception forCodable
users who have to provide custom keys.OK ✅
Not OK ❌