mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
With the binding of configurations to their associated rule types "unknown configuration" errors can be made more specific mentioning also the rule's identifier in the printed message.
42 lines
1.3 KiB
Swift
42 lines
1.3 KiB
Swift
private func toExplicitInitMethod(typeName: String) -> String {
|
|
return "\(typeName).init"
|
|
}
|
|
|
|
struct DiscouragedDirectInitConfiguration: SeverityBasedRuleConfiguration, Equatable {
|
|
typealias Parent = DiscouragedDirectInitRule
|
|
|
|
var severityConfiguration = SeverityConfiguration<Parent>(.warning)
|
|
|
|
var consoleDescription: String {
|
|
return "severity: \(severityConfiguration.consoleDescription)" + ", types: \(discouragedInits.sorted(by: <))"
|
|
}
|
|
|
|
private(set) var discouragedInits: Set<String>
|
|
|
|
private let defaultDiscouragedInits = [
|
|
"Bundle",
|
|
"NSError",
|
|
"UIDevice"
|
|
]
|
|
|
|
init() {
|
|
discouragedInits = Set(defaultDiscouragedInits + defaultDiscouragedInits.map(toExplicitInitMethod))
|
|
}
|
|
|
|
// MARK: - RuleConfiguration
|
|
|
|
mutating func apply(configuration: Any) throws {
|
|
guard let configuration = configuration as? [String: Any] else {
|
|
throw Issue.unknownConfiguration(ruleID: Parent.identifier)
|
|
}
|
|
|
|
if let severityString = configuration["severity"] as? String {
|
|
try severityConfiguration.apply(configuration: severityString)
|
|
}
|
|
|
|
if let types = [String].array(of: configuration["types"]) {
|
|
discouragedInits = Set(types + types.map(toExplicitInitMethod))
|
|
}
|
|
}
|
|
}
|