mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +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
```
102 lines
2.9 KiB
Swift
102 lines
2.9 KiB
Swift
import SourceKittenFramework
|
|
|
|
extension SwiftDeclarationAttributeKind {
|
|
static var attributesRequiringFoundation: Set<SwiftDeclarationAttributeKind> {
|
|
return [
|
|
.objc,
|
|
.objcName,
|
|
.objcMembers,
|
|
.objcNonLazyRealization
|
|
]
|
|
}
|
|
|
|
enum ModifierGroup: String, CustomDebugStringConvertible {
|
|
case `override`
|
|
case acl
|
|
case setterACL
|
|
case owned
|
|
case mutators
|
|
case final
|
|
case typeMethods
|
|
case `required`
|
|
case `convenience`
|
|
case `lazy`
|
|
case `dynamic`
|
|
case atPrefixed
|
|
|
|
init?(rawAttribute: String) {
|
|
let allModifierGroups: Set<SwiftDeclarationAttributeKind.ModifierGroup> = [
|
|
.acl, .setterACL, .mutators, .override, .owned, .atPrefixed, .dynamic, .final, .typeMethods,
|
|
.required, .convenience, .lazy
|
|
]
|
|
let modifierGroup = allModifierGroups.first {
|
|
$0.swiftDeclarationAttributeKinds.contains(where: { $0.rawValue == rawAttribute })
|
|
}
|
|
|
|
if let modifierGroup = modifierGroup {
|
|
self = modifierGroup
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
var swiftDeclarationAttributeKinds: Set<SwiftDeclarationAttributeKind> {
|
|
switch self {
|
|
case .acl:
|
|
return [
|
|
.private,
|
|
.fileprivate,
|
|
.internal,
|
|
.public,
|
|
.open
|
|
]
|
|
case .setterACL:
|
|
return [
|
|
.setterPrivate,
|
|
.setterFilePrivate,
|
|
.setterInternal,
|
|
.setterPublic,
|
|
.setterOpen
|
|
]
|
|
case .mutators:
|
|
return [
|
|
.mutating,
|
|
.nonmutating
|
|
]
|
|
case .override:
|
|
return [.override]
|
|
case .owned:
|
|
return [.weak]
|
|
case .final:
|
|
return [.final]
|
|
case .typeMethods:
|
|
return []
|
|
case .required:
|
|
return [.required]
|
|
case .convenience:
|
|
return [.convenience]
|
|
case .lazy:
|
|
return [.lazy]
|
|
case .dynamic:
|
|
return [.dynamic]
|
|
case .atPrefixed:
|
|
return [
|
|
.objc,
|
|
.nonobjc,
|
|
.objcMembers,
|
|
.ibaction,
|
|
.iboutlet,
|
|
.ibdesignable,
|
|
.ibinspectable,
|
|
.nsManaged,
|
|
.nsCopying
|
|
]
|
|
}
|
|
}
|
|
|
|
var debugDescription: String {
|
|
return self.rawValue
|
|
}
|
|
}
|
|
}
|