mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
660920a5f8
Sometimes we hit issues with SourceKit that cause this rule to produce false positives of some imports. While we would prefer to fix all of these cases whenever possible, this gives us a temporary escape hatch to tell the rule never to remove imports of specific modules until the fixes land in SwiftLint or Swift itself. Currently this is for us to opt out of https://bugs.swift.org/browse/SR-13120
66 lines
3.0 KiB
Swift
66 lines
3.0 KiB
Swift
/// The configuration payload mapping an imported module to a set of modules that are allowed to be
|
|
/// transitively imported.
|
|
public struct TransitiveModuleConfiguration: Equatable {
|
|
/// The module imported in a source file.
|
|
public let importedModule: String
|
|
/// The set of modules that can be transitively imported by `importedModule`.
|
|
public let transitivelyImportedModules: [String]
|
|
|
|
init(configuration: Any) throws {
|
|
guard let configurationDict = configuration as? [String: Any],
|
|
Set(configurationDict.keys) == ["module", "allowed_transitive_imports"],
|
|
let importedModule = configurationDict["module"] as? String,
|
|
let transitivelyImportedModules = configurationDict["allowed_transitive_imports"] as? [String]
|
|
else {
|
|
throw ConfigurationError.unknownConfiguration
|
|
}
|
|
self.importedModule = importedModule
|
|
self.transitivelyImportedModules = transitivelyImportedModules
|
|
}
|
|
}
|
|
|
|
public struct UnusedImportConfiguration: RuleConfiguration, Equatable {
|
|
public var consoleDescription: String {
|
|
return [
|
|
"severity: \(severity.consoleDescription)",
|
|
"require_explicit_imports: \(requireExplicitImports)",
|
|
"allowed_transitive_imports: \(allowedTransitiveImports)",
|
|
"always_keep_imports: \(alwaysKeepImports)"
|
|
].joined(separator: ", ")
|
|
}
|
|
|
|
public private(set) var severity: SeverityConfiguration
|
|
public private(set) var requireExplicitImports: Bool
|
|
public private(set) var allowedTransitiveImports: [TransitiveModuleConfiguration]
|
|
/// A set of modules to never remove the imports of.
|
|
public private(set) var alwaysKeepImports: [String]
|
|
|
|
public init(severity: ViolationSeverity, requireExplicitImports: Bool,
|
|
allowedTransitiveImports: [TransitiveModuleConfiguration],
|
|
alwaysKeepImports: [String]) {
|
|
self.severity = SeverityConfiguration(severity)
|
|
self.requireExplicitImports = requireExplicitImports
|
|
self.allowedTransitiveImports = allowedTransitiveImports
|
|
self.alwaysKeepImports = alwaysKeepImports
|
|
}
|
|
|
|
public mutating func apply(configuration: Any) throws {
|
|
guard let configurationDict = configuration as? [String: Any] else {
|
|
throw ConfigurationError.unknownConfiguration
|
|
}
|
|
|
|
if let severityConfiguration = configurationDict["severity"] {
|
|
try severity.apply(configuration: severityConfiguration)
|
|
}
|
|
if let requireExplicitImports = configurationDict["require_explicit_imports"] as? Bool {
|
|
self.requireExplicitImports = requireExplicitImports
|
|
}
|
|
if let allowedTransitiveImports = configurationDict["allowed_transitive_imports"] as? [Any] {
|
|
self.allowedTransitiveImports = try allowedTransitiveImports.map(TransitiveModuleConfiguration.init)
|
|
}
|
|
if let alwaysKeepImports = configurationDict["always_keep_imports"] as? [String] {
|
|
self.alwaysKeepImports = alwaysKeepImports
|
|
}
|
|
}
|
|
}
|