mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
d10ccacb45
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
```
30 lines
852 B
Swift
30 lines
852 B
Swift
import Foundation
|
|
|
|
/// Represents unused or missing import statements.
|
|
enum ImportUsage {
|
|
/// The import is unused. Range is for the entire import statement.
|
|
case unused(module: String, range: NSRange)
|
|
/// The file is missing an explicit import of the `module`.
|
|
case missing(module: String)
|
|
|
|
/// The range where the violation for this import usage should be reported.
|
|
var violationRange: NSRange? {
|
|
switch self {
|
|
case .unused(_, let range):
|
|
return range
|
|
case .missing:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
/// The reason why this import usage is a violation.
|
|
var violationReason: String? {
|
|
switch self {
|
|
case .unused:
|
|
return nil
|
|
case .missing(let module):
|
|
return "Missing import for referenced module '\(module)'."
|
|
}
|
|
}
|
|
}
|