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 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

### Enhancements

* None.

* Add `ignore_coding_keys` parameter to `nesting` rule.
[braker1nine](https://github.com/braker1nine)
[#5641](https://github.com/realm/SwiftLint/issues/5641)

### Bug Fixes

* None.
Expand Down
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.definesCodingKeys {
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_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
12 changes: 12 additions & 0 deletions Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@
return rawValueTypes.contains(identifier)
}
}

/// True if this enum is a `CodingKey`. For that, it has to be named `CodingKeys` and must conform to the `CodingKey` protocol.

Check failure on line 188 in Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift

View workflow job for this annotation

GitHub Actions / Lint Swift

Line should be 120 characters or less; currently it has 132 characters (line_length)
var definesCodingKeys: Bool {
guard let inheritedTypeCollection = inheritanceClause?.inheritedTypes,
name.text == "CodingKeys" else {

Check failure on line 191 in Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift

View workflow job for this annotation

GitHub Actions / Lint Swift

Lines should not have trailing whitespace (trailing_whitespace)
return false
}

return inheritedTypeCollection.contains { element in
element.type.as(IdentifierTypeSyntax.self)?.name.text == "CodingKey"
}
}
}

public extension FunctionDeclSyntax {
Expand Down
42 changes: 42 additions & 0 deletions Tests/BuiltInRulesTests/NestingRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,46 @@

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 {
case id
}
}
}
"""),
.init("""
struct Outer {
enum CodingKeys: String, CodingKey {
case id

Check failure on line 589 in Tests/BuiltInRulesTests/NestingRuleTests.swift

View workflow job for this annotation

GitHub Actions / Lint Swift

Lines should not have trailing whitespace (trailing_whitespace)
↓struct S {}
}
}
""")

Check failure on line 593 in Tests/BuiltInRulesTests/NestingRuleTests.swift

View workflow job for this annotation

GitHub Actions / Lint Swift

Multi-line collection literals should have trailing commas (trailing_comma)
])

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

Check failure on line 596 in Tests/BuiltInRulesTests/NestingRuleTests.swift

View workflow job for this annotation

GitHub Actions / Lint Swift

Line should be 120 characters or less; currently it has 140 characters (line_length)

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