mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
b83e0991b9
The MIT license doesn't require that all files be prepended with this licensing or copyright information. Realm confirmed that they're ok with this change. This will enable some companies to contribute to SwiftLint and the date & authorship information will remain accessible via git source control.
52 lines
1.6 KiB
Swift
52 lines
1.6 KiB
Swift
import Foundation
|
|
import SourceKittenFramework
|
|
|
|
private func toExplicitInitMethod(typeName: String) -> String {
|
|
return "\(typeName).init"
|
|
}
|
|
|
|
public struct DiscouragedDirectInitConfiguration: RuleConfiguration, Equatable {
|
|
public var severityConfiguration = SeverityConfiguration(.warning)
|
|
|
|
public var consoleDescription: String {
|
|
return severityConfiguration.consoleDescription + ", types: \(discouragedInits.sorted(by: <))"
|
|
}
|
|
|
|
public var severity: ViolationSeverity {
|
|
return severityConfiguration.severity
|
|
}
|
|
|
|
private(set) public var discouragedInits: Set<String>
|
|
|
|
private let defaultDiscouragedInits = [
|
|
"Bundle",
|
|
"UIDevice"
|
|
]
|
|
|
|
init() {
|
|
discouragedInits = Set(defaultDiscouragedInits + defaultDiscouragedInits.map(toExplicitInitMethod))
|
|
}
|
|
|
|
// MARK: - RuleConfiguration
|
|
|
|
public mutating func apply(configuration: Any) throws {
|
|
guard let configuration = configuration as? [String: Any] else {
|
|
throw ConfigurationError.unknownConfiguration
|
|
}
|
|
|
|
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))
|
|
}
|
|
}
|
|
|
|
// MARK: - Equatable
|
|
|
|
public static func == (lhs: DiscouragedDirectInitConfiguration, rhs: DiscouragedDirectInitConfiguration) -> Bool {
|
|
return lhs.discouragedInits == rhs.discouragedInits && lhs.severityConfiguration == rhs.severityConfiguration
|
|
}
|
|
}
|