mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
34f429b0a3
This has never worked for two reasons: 1. We've used dictionaries to represent cache descriptions, which don't guarantee stable ordering of keys across invocations. This is true both on Darwin and Linux, but in practice ordering varies significantly more on Linux. 2. Storing a `TimeInterval` value in a `[String: Any]` dictionary and retrieving it again will not be dynamically castable to `Double` or `TimeInterval` but will be castable to `Int`.
60 lines
1.8 KiB
Swift
60 lines
1.8 KiB
Swift
//
|
|
// DiscouragedInitConfiguration.swift
|
|
// SwiftLint
|
|
//
|
|
// Created by Ornithologist Coder on 8/1/17.
|
|
// Copyright © 2017 Realm. All rights reserved.
|
|
//
|
|
|
|
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
|
|
}
|
|
}
|