Files
SwiftLint/Source/SwiftLintFramework/Rules/PrivateOutletRule.swift
T
Norio Nomura b7862b9f6a Merge commit 'e0cafea80ffba746a103d8d5a25be8fcd0993c81' into swift3.0
* commit 'e0cafea80ffba746a103d8d5a25be8fcd0993c81':
  remove unnecessary file.invalidateCache()
  add changelog entry for #893
  fix up indentation in MarkRule.swift
  omit self if it can be inferred
  Corrections for mark rule
  Update regex part to be more precise.
  Add autocorrection to MarkRule

# Conflicts:
#	Source/SwiftLintFramework/Rules/ConditionalReturnsOnNewline.swift
#	Source/SwiftLintFramework/Rules/MarkRule.swift
#	Source/SwiftLintFramework/Rules/StatementPositionRule.swift
2016-11-30 22:26:51 +09:00

78 lines
2.9 KiB
Swift

//
// PrivateOutletRule.swift
// SwiftLint
//
// Created by Olivier Halligon on 12/08/2016.
// Copyright © 2016 Realm. All rights reserved.
//
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.",
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 validateFile(_ file: File,
kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard kind == .varInstance else {
return []
}
// Check if IBOutlet
let attributes = (dictionary["key.attributes"] as? [SourceKitRepresentable])?
.flatMap({ ($0 as? [String: SourceKitRepresentable]) as? [String: String] })
.flatMap({ $0["key.attribute"] }) ?? []
let isOutlet = attributes.contains("source.decl.attribute.iboutlet")
guard isOutlet else { return [] }
// Check if private
let accessibility = dictionary["key.accessibility"] as? String
let setterAccessiblity = dictionary["key.setter_accessibility"] as? String
let isPrivate = isPrivateLevel(identifier: accessibility)
let isPrivateSet = isPrivateLevel(identifier: setterAccessiblity)
if isPrivate || (configuration.allowPrivateSet && isPrivateSet) {
return []
}
// Violation found!
let location: Location
if let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }) {
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
}
}