Adjust accessibility to match enclosing container.

This commit is contained in:
Tadeas Kriz 2020-01-17 11:20:32 +01:00
parent cd58a7f09e
commit 1d31b0634c
3 changed files with 57 additions and 21 deletions

View File

@ -32,3 +32,25 @@ public enum Accessibility: String {
return self != .Private && self != .FilePrivate
}
}
extension Accessibility: Comparable {
/// How open is this accessibility. The higher number the more accessible.
private var openness: Int {
switch self {
case .Open:
return 4
case .Public:
return 3
case .Internal:
return 2
case .FilePrivate:
return 1
case .Private:
return 0
}
}
public static func < (lhs: Accessibility, rhs: Accessibility) -> Bool {
return lhs.openness < rhs.openness
}
}

View File

@ -21,15 +21,29 @@ public protocol ContainerToken: Token, HasAccessibility {
extension ContainerToken {
public func serialize() -> [String : Any] {
let properties = children.compactMap { $0 as? InstanceVariable }
func withAdjustedAccessibility(token: Token & HasAccessibility) -> Token & HasAccessibility {
// We only want to adjust tokens that are accessible and lower than the enclosing type
guard token.accessibility.isAccessible && token.accessibility < accessibility else { return token }
var mutableToken = token
mutableToken.accessibility = accessibility
return mutableToken
}
let accessibilityAdjustedChildren = children.map { child -> Token in
guard let childWithAccessibility = child as? HasAccessibility & Token else { return child }
return withAdjustedAccessibility(token: childWithAccessibility)
}
let properties = accessibilityAdjustedChildren.compactMap { $0 as? InstanceVariable }
InstanceVariable }
.filter { $0.accessibility.isAccessible }
.map { $0.serializeWithType() }
let methods = children.compactMap { $0 as? Method }
let methods = accessibilityAdjustedChildren.compactMap { $0 as? Method }
.filter { $0.accessibility.isAccessible && !$0.isInit && !$0.isDeinit }
.map { $0.serializeWithType() }
let initializers = children.compactMap { $0 as? Method }
let initializers = accessibilityAdjustedChildren.compactMap { $0 as? Method }
.filter { $0.accessibility.isAccessible && $0.isInit && !$0.isDeinit }
.map { $0.serializeWithType() }
@ -42,7 +56,7 @@ extension ContainerToken {
"name": name,
"accessibility": accessibility.sourceName,
"isAccessible": accessibility.isAccessible,
"children": children.map { $0.serializeWithType() },
"children": accessibilityAdjustedChildren.map { $0.serializeWithType() },
"properties": properties,
"methods": methods,
"initializers": implementation ? [] : initializers,

View File

@ -6,6 +6,7 @@
// Copyright © 2016 Brightify. All rights reserved.
//
#warning("TODO: Create a new library and move this file to the library to ensure the use case is the same as our users'")
import class Foundation.NSArray ;import Foundation
@available(iOS 42.0, *)
@ -267,23 +268,22 @@ public class InternalFieldsInPublicClass {
internal func function() { }
}
public class PublicClassWithInternalFieldsUsingPublicType {
internal var internalField: InternalType = InternalType()
var internalField2: InternalType = InternalType()
func internalFunc(internalType: InternalType) {
}
internal var internalField3: PublicType = PublicType()
var internalField4: PublicType = PublicType()
func internalFunc(publicType: PublicType) {
}
}
class InternalType {
}
public class PublicType {
}
#warning("TODO: When this file is moved to a separate library, uncomment and test this code compiles.")
//public class PublicClassWithInternalFieldsUsingPublicType {
// internal var internalField: InternalType = InternalType()
// var internalField2: InternalType = InternalType()
// func internalFunc(internalType: InternalType) {
// }
//
// internal var internalField3: PublicType = PublicType()
// var internalField4: PublicType = PublicType()
// func internalFunc(publicType: PublicType) {
// }
//}
//class InternalType {
//}
//public class PublicType {
//}
class FinalFields {
final var field: Int? = nil