mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
b83e0991b9
The MIT license doesn't require that all files be prepended with this licensing or copyright information. Realm confirmed that they're ok with this change. This will enable some companies to contribute to SwiftLint and the date & authorship information will remain accessible via git source control.
64 lines
2.3 KiB
Swift
64 lines
2.3 KiB
Swift
import Foundation
|
|
import SourceKittenFramework
|
|
|
|
public struct PrivateOutletRule: ASTRule, OptInRule, ConfigurationProviderRule {
|
|
public var configuration = PrivateOutletRuleConfiguration(allowPrivateSet: false)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "private_outlet",
|
|
name: "Private Outlets",
|
|
description: "IBOutlets should be private to avoid leaking UIKit to higher layers.",
|
|
kind: .lint,
|
|
nonTriggeringExamples: [
|
|
"class Foo {\n @IBOutlet private var label: UILabel?\n}\n",
|
|
"class Foo {\n @IBOutlet private var label: UILabel!\n}\n",
|
|
"class Foo {\n var notAnOutlet: UILabel\n}\n",
|
|
"class Foo {\n @IBOutlet weak private var label: UILabel?\n}\n",
|
|
"class Foo {\n @IBOutlet private weak var label: UILabel?\n}\n"
|
|
],
|
|
triggeringExamples: [
|
|
"class Foo {\n @IBOutlet ↓var label: UILabel?\n}\n",
|
|
"class Foo {\n @IBOutlet ↓var label: UILabel!\n}\n"
|
|
]
|
|
)
|
|
|
|
public func validate(file: File, kind: SwiftDeclarationKind,
|
|
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
|
|
guard kind == .varInstance else {
|
|
return []
|
|
}
|
|
|
|
// Check if IBOutlet
|
|
let isOutlet = dictionary.enclosedSwiftAttributes.contains(.iboutlet)
|
|
guard isOutlet else { return [] }
|
|
|
|
// Check if private
|
|
let isPrivate = isPrivateLevel(identifier: dictionary.accessibility)
|
|
let isPrivateSet = isPrivateLevel(identifier: dictionary.setterAccessibility)
|
|
|
|
if isPrivate || (configuration.allowPrivateSet && isPrivateSet) {
|
|
return []
|
|
}
|
|
|
|
// Violation found!
|
|
let location: Location
|
|
if let offset = dictionary.offset {
|
|
location = Location(file: file, byteOffset: offset)
|
|
} else {
|
|
location = Location(file: file.path)
|
|
}
|
|
|
|
return [
|
|
StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severityConfiguration.severity,
|
|
location: location)
|
|
]
|
|
}
|
|
|
|
private func isPrivateLevel(identifier: String?) -> Bool {
|
|
return identifier.flatMap(AccessControlLevel.init(identifier:))?.isPrivate ?? false
|
|
}
|
|
}
|