mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
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.
56 lines
2.3 KiB
Swift
56 lines
2.3 KiB
Swift
/// A rule configuration that allows specifying thresholds for `warning` and `error` severities.
|
|
public struct SeverityLevelsConfiguration<Parent: Rule>: RuleConfiguration, InlinableOptionType {
|
|
/// The threshold for a violation to be a warning.
|
|
@ConfigurationElement(key: "warning")
|
|
public var warning: Int = 12
|
|
/// The threshold for a violation to be an error.
|
|
@ConfigurationElement(key: "error")
|
|
public var error: Int?
|
|
|
|
/// Create a `SeverityLevelsConfiguration` based on the sepecified `warning` and `error` thresholds.
|
|
///
|
|
/// - parameter warning: The threshold for a violation to be a warning.
|
|
/// - parameter error: The threshold for a violation to be an error.
|
|
public init(warning: Int, error: Int? = nil) {
|
|
self.warning = warning
|
|
self.error = error
|
|
}
|
|
|
|
/// The rule parameters that define the thresholds that should map to each severity.
|
|
public var params: [RuleParameter<Int>] {
|
|
if let error {
|
|
return [RuleParameter(severity: .error, value: error),
|
|
RuleParameter(severity: .warning, value: warning)]
|
|
}
|
|
return [RuleParameter(severity: .warning, value: warning)]
|
|
}
|
|
|
|
public mutating func apply(configuration: Any) throws {
|
|
if let configurationArray = [Int].array(of: configuration), configurationArray.isNotEmpty {
|
|
warning = configurationArray[0]
|
|
error = (configurationArray.count > 1) ? configurationArray[1] : nil
|
|
} else if let configDict = configuration as? [String: Any?] {
|
|
if let warningValue = configDict[$warning.key] {
|
|
if let warning = warningValue as? Int {
|
|
self.warning = warning
|
|
} else {
|
|
throw Issue.invalidConfiguration(ruleID: Parent.identifier)
|
|
}
|
|
}
|
|
if let errorValue = configDict[$error.key] {
|
|
if errorValue == nil {
|
|
self.error = nil
|
|
} else if let error = errorValue as? Int {
|
|
self.error = error
|
|
} else {
|
|
throw Issue.invalidConfiguration(ruleID: Parent.identifier)
|
|
}
|
|
} else {
|
|
self.error = nil
|
|
}
|
|
} else {
|
|
throw Issue.nothingApplied(ruleID: Parent.identifier)
|
|
}
|
|
}
|
|
}
|