-
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?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
private(set) var ignoreCodingKeys = false | ||||||
|
||||||
func severity(with config: Severity, for level: Int) -> ViolationSeverity? { | ||||||
if let error = config.error, level > error { | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
var isCodingKey: Bool { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||
guard let inheritedTypeCollection = inheritanceClause?.inheritedTypes else { | ||||||||||||||
return false | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
guard self.name.text == "CodingKeys" else { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Can also be added to the previous |
||||||||||||||
return false | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return inheritedTypeCollection.contains { element in | ||||||||||||||
guard let identifier = element.type.as(IdentifierTypeSyntax.self)?.name.text else { | ||||||||||||||
return false | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return identifier == "CodingKey" | ||||||||||||||
Comment on lines
+199
to
+203
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public extension FunctionDeclSyntax { | ||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -551,4 +551,38 @@ final class NestingRuleTests: SwiftLintTestCase { | |||||
|
||||||
verifyRule(description, ruleConfiguration: ["ignore_typealiases_and_associatedtypes": true]) | ||||||
} | ||||||
|
||||||
func testNestingWithoutCodingKeys() { | ||||||
var nonTriggeringExamples = NestingRule.description.nonTriggeringExamples | ||||||
nonTriggeringExamples.append(contentsOf: [ | ||||||
.init(""" | ||||||
struct Outer { | ||||||
struct Inner { | ||||||
enum CodingKeys: String, CodingKey { | ||||||
case id | ||||||
} | ||||||
} | ||||||
} | ||||||
""" | ||||||
) | ||||||
]) | ||||||
|
||||||
var triggeringExamples = NestingRule.description.triggeringExamples | ||||||
triggeringExamples.append(contentsOf: [ | ||||||
.init(""" | ||||||
struct Outer { | ||||||
struct Inner { | ||||||
enum Example: String, CodingKey { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
case id | ||||||
} | ||||||
} | ||||||
} | ||||||
""") | ||||||
]) | ||||||
|
||||||
let description = NestingRule.description.with(nonTriggeringExamples: nonTriggeringExamples, triggeringExamples: triggeringExamples) | ||||||
|
||||||
verifyRule(description, ruleConfiguration: ["ignore_codingkeys": true ]) | ||||||
} | ||||||
|
||||||
} |
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.