Files
SwiftLint/Source/SwiftLintFramework/Models/AccessControlLevel.swift
T
Keith Smiley 925fb26d83 Add LowerACLThanBodyRule
This new rule validates that if a type/function/variable definition has
an ACL specifier, it is more restrictive than the containing body's
level. This is intended to lint a peculiarity of SE-0025 where it is
stated:

> The compiler should not warn when a broader level of access control is
used within a type with more restrictive access, such as internal within
a private type. This allows the designer of the type to select the
access they would use were they to make the type more widely accessible.

I think this is an anti-goal because when a type is made more open, it
should be a concious decision at that time to make it public. This is of
course an opt-in rule as well. This also has the added benefit of
linting this compiler bug: https://bugs.swift.org/browse/SR-2925
2018-04-04 10:16:12 -07:00

65 lines
1.7 KiB
Swift

//
// AccessControlLevel.swift
// SwiftLint
//
// Created by Marcelo Fabri on 23/04/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
public enum AccessControlLevel: String, CustomStringConvertible {
case `private` = "source.lang.swift.accessibility.private"
case `fileprivate` = "source.lang.swift.accessibility.fileprivate"
case `internal` = "source.lang.swift.accessibility.internal"
case `public` = "source.lang.swift.accessibility.public"
case `open` = "source.lang.swift.accessibility.open"
internal init?(description value: String) {
switch value {
case "private": self = .private
case "fileprivate": self = .fileprivate
case "internal": self = .internal
case "public": self = .public
case "open": self = .open
default: return nil
}
}
init?(identifier value: String) {
self.init(rawValue: value)
}
public var description: String {
switch self {
case .private: return "private"
case .fileprivate: return "fileprivate"
case .internal: return "internal"
case .public: return "public"
case .open: return "open"
}
}
// Returns true if is `private` or `fileprivate`
var isPrivate: Bool {
return self == .private || self == .fileprivate
}
}
extension AccessControlLevel: Comparable {
private var priority: Int {
switch self {
case .private: return 1
case .fileprivate: return 2
case .internal: return 3
case .public: return 4
case .open: return 5
}
}
public static func < (lhs: AccessControlLevel, rhs: AccessControlLevel) -> Bool {
return lhs.priority < rhs.priority
}
}