mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
e6bb673444
Use different issues to make inlineable configurations work in both scenarios, namely being used standalone and nested as part of another configuration. In the latter case, finding no values to parse in a raw configuration is okay, while as a standalone configuration it's not.
36 lines
1.4 KiB
Swift
36 lines
1.4 KiB
Swift
/// A rule configuration that allows specifying the desired severity level for violations.
|
|
public struct SeverityConfiguration<Parent: Rule>: SeverityBasedRuleConfiguration, InlinableOptionType {
|
|
/// Configuration with a warning severity.
|
|
public static var error: Self { Self(.error) }
|
|
/// Configuration with an error severity.
|
|
public static var warning: Self { Self(.warning) }
|
|
|
|
@ConfigurationElement(key: "severity")
|
|
var severity = ViolationSeverity.warning
|
|
|
|
public var severityConfiguration: SeverityConfiguration {
|
|
self
|
|
}
|
|
|
|
/// Create a `SeverityConfiguration` with the specified severity.
|
|
///
|
|
/// - parameter severity: The severity that should be used when emitting violations.
|
|
public init(_ severity: ViolationSeverity) {
|
|
self.severity = severity
|
|
}
|
|
|
|
public mutating func apply(configuration: Any) throws {
|
|
let configString = configuration as? String
|
|
let configDict = configuration as? [String: Any]
|
|
if let severityString: String = configString ?? configDict?[$severity.key] as? String {
|
|
if let severity = ViolationSeverity(rawValue: severityString.lowercased()) {
|
|
self.severity = severity
|
|
} else {
|
|
throw Issue.invalidConfiguration(ruleID: Parent.identifier)
|
|
}
|
|
} else {
|
|
throw Issue.nothingApplied(ruleID: Parent.identifier)
|
|
}
|
|
}
|
|
}
|