Skip to content

Commit

Permalink
Add validation hook to configuration parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny committed Oct 13, 2024
1 parent 9ebd6ae commit 72f1ed5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Source/SwiftLintCore/Models/Issue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public enum Issue: LocalizedError, Equatable {
/// Some configuration keys are invalid.
case invalidConfigurationKeys(ruleID: String, keys: Set<String>)

/// The configuration is inconsistent, that is options are mutually exclusive or one drives other values
/// irrelevant.
case inconsistentConfiguration(ruleID: String, message: String)

/// Used rule IDs are invalid.
case invalidRuleIDs(Set<String>)

Expand Down Expand Up @@ -154,6 +158,8 @@ public enum Issue: LocalizedError, Equatable {
return "'\(old)' has been renamed to '\(new)' and will be completely removed in a future release."
case let .invalidConfigurationKeys(id, keys):
return "Configuration for '\(id)' rule contains the invalid key(s) \(keys.formatted)."
case let .inconsistentConfiguration(id, message):
return "Inconsistent configuration for '\(id)' rule: \(message)"
case let .invalidRuleIDs(ruleIDs):
return "The key(s) \(ruleIDs.formatted) used as rule identifier(s) is/are invalid."
case let .ruleNotPresentInOnlyRules(id):
Expand Down
8 changes: 8 additions & 0 deletions Source/SwiftLintCore/Protocols/RuleConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public protocol RuleConfiguration: Equatable {
///
/// - throws: Throws if the configuration is not in the expected format.
mutating func apply(configuration: Any) throws

/// Run a sanity check on the configuration, perform optional postprocessing steps and/or warn about potential
/// issues.
mutating func validate() throws
}

/// A configuration for a rule that allows to configure at least the severity.
Expand All @@ -30,6 +34,10 @@ public extension SeverityBasedRuleConfiguration {

public extension RuleConfiguration {
var parameterDescription: RuleConfigurationDescription? { nil }

func validate() throws {
// Do nothing by default.
}
}

public extension RuleConfiguration {
Expand Down
3 changes: 3 additions & 0 deletions Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ enum AutoConfigParser: MemberMacro {
Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys).print()
}
"""
"""
try validate()
"""
}),
]
}
Expand Down
5 changes: 5 additions & 0 deletions Tests/MacroTests/AutoConfigParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ final class AutoConfigParserTests: XCTestCase {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys).print()
}
try validate()
}
}
""",
Expand Down Expand Up @@ -90,6 +91,7 @@ final class AutoConfigParserTests: XCTestCase {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys).print()
}
try validate()
}
}
""",
Expand Down Expand Up @@ -145,6 +147,7 @@ final class AutoConfigParserTests: XCTestCase {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys).print()
}
try validate()
}
}
""",
Expand All @@ -171,6 +174,7 @@ final class AutoConfigParserTests: XCTestCase {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys).print()
}
try validate()
}
}
""",
Expand Down Expand Up @@ -228,6 +232,7 @@ final class AutoConfigParserTests: XCTestCase {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys).print()
}
try validate()
}
}
""",
Expand Down

0 comments on commit 72f1ed5

Please sign in to comment.