Skip to content

Commit

Permalink
Add ignore_coding_keys parameter to nesting
Browse files Browse the repository at this point in the history
This prevents nested CodingKeys (which may be necessary
for functioning Codable conformance) from triggering
nesting violations
  • Loading branch information
braker1nine committed Jan 20, 2025
1 parent d785a93 commit 2f71da4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
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
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 @@ 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 {
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 ])
}
}

0 comments on commit 2f71da4

Please sign in to comment.