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 ignore_codingkeys parameter to nesting #5650

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Source/SwiftLintBuiltInRules/Rules/Metrics/NestingRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ private extension NestingRule {
}

override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
validate(forFunction: false, triggeringToken: node.enumKeyword)
if !(configuration.ignoreCodingKeys && node.isCodingKey) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if !(configuration.ignoreCodingKeys && node.isCodingKey) {
if !configuration.ignoreCodingKeys || !node.isCodingKey {

validate(forFunction: false, triggeringToken: node.enumKeyword)
}
return .visitChildren
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
@ConfigurationElement(key: "ignore_codingkeys")
@ConfigurationElement(key: "ignore_coding_keys")

private(set) var ignoreCodingKeys = false

func severity(with config: Severity, for level: Int) -> ViolationSeverity? {
if let error = config.error, level > error {
Expand Down
19 changes: 19 additions & 0 deletions Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,25 @@ public extension EnumDeclSyntax {
return rawValueTypes.contains(identifier)
}
}

/// True if this enum is a `CodingKey`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// 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.

var isCodingKey: Bool {
Copy link
Collaborator

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?

guard let inheritedTypeCollection = inheritanceClause?.inheritedTypes else {
return false
}

guard self.name.text == "CodingKeys" else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
guard self.name.text == "CodingKeys" else {
guard name.text == "CodingKeys" else {

Can also be added to the previous guard.

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
guard let identifier = element.type.as(IdentifierTypeSyntax.self)?.name.text else {
return false
}
return identifier == "CodingKey"
element.type.as(IdentifierTypeSyntax.self)?.name.text == "CodingKey"

}
}
}

public extension FunctionDeclSyntax {
Expand Down
34 changes: 34 additions & 0 deletions Tests/SwiftLintFrameworkTests/NestingRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
enum Example: String, CodingKey {
enum Example: String, CodingKey {

case id
}
}
}
""")
])

let description = NestingRule.description.with(nonTriggeringExamples: nonTriggeringExamples, triggeringExamples: triggeringExamples)

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

}
Loading