Files
SwiftLint/Source/SwiftLintFramework/Rules/RuleConfigurations/UnusedImportConfiguration.swift
T
JP Simard d10ccacb45 Add unused_import config options to require imports for each module used (#3123)
For example, if `CGFloat` is used in a file where only `UIKit` is imported but not `CoreGraphics`, this will be a violation even if the file previously compiled.

This is because Swift allows referencing some declarations that are only transitively imported.

Enabling the `require_explicit_imports` configuration option will require that the module of every declaration referenced in a source file be explicitly imported.

This will add significant noise to the imports list, but has a few advantages:

1. It will be easier to understand all the dependencies explicitly referenced in a source file.
2. Correcting the `unused_import` rule will no longer introduce compilation errors in files that compiled prior to the correction.

If missing imports are added to a file when correcting it, the `sorted_imports` rule will be automatically run on that file.

If you with to allow some imports to be implicitly importable transitively, you may specify the `allowed_transitive_imports` configuration:

```yaml
unused_import:
  require_explicit_imports: true
  allowed_transitive_imports:
    - module: Foundation
      allowed_transitive_imports:
        - CoreFoundation
        - Darwin
        - ObjectiveC
```
2020-02-22 14:39:07 -08:00

58 lines
2.6 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)"
].joined(separator: ", ")
}
public private(set) var severity: SeverityConfiguration
public private(set) var requireExplicitImports: Bool
public private(set) var allowedTransitiveImports: [TransitiveModuleConfiguration]
public init(severity: ViolationSeverity, requireExplicitImports: Bool,
allowedTransitiveImports: [TransitiveModuleConfiguration]) {
self.severity = SeverityConfiguration(severity)
self.requireExplicitImports = requireExplicitImports
self.allowedTransitiveImports = allowedTransitiveImports
}
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)
}
}
}