-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from Matejkob/access-levels
Add Access Level Inheritance
- Loading branch information
Showing
8 changed files
with
414 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import Spyable | ||
|
||
// MARK: - Open | ||
|
||
// Only classes and overridable class members can be declared 'open'. | ||
|
||
// MARK: - Public | ||
|
||
@Spyable | ||
public protocol PublicServiceProtocol { | ||
var name: String { get } | ||
var anyProtocol: any Codable { get set } | ||
var secondName: String? { get } | ||
var address: String! { get } | ||
var added: () -> Void { get set } | ||
var removed: (() -> Void)? { get set } | ||
|
||
func initialize(name: String, _ secondName: String?) | ||
func fetchConfig(arg: UInt8) async throws -> [String: String] | ||
func fetchData(_ name: (String, count: Int)) async -> (() -> Void) | ||
func save(name: any Codable, surname: any Codable) | ||
func insert(name: (any Codable)?, surname: (any Codable)?) | ||
func append(name: (any Codable) -> (any Codable)?) | ||
func get() async throws -> any Codable | ||
func read() -> String! | ||
func wrapDataInArray<T>(_ data: T) -> [T] | ||
} | ||
|
||
func testPublicServiceProtocol() { | ||
let spy = PublicServiceProtocolSpy() | ||
|
||
spy.name = "Spy" | ||
} | ||
|
||
// MARK: - Package | ||
|
||
@Spyable | ||
package protocol PackageServiceProtocol { | ||
var name: String { get } | ||
var anyProtocol: any Codable { get set } | ||
var secondName: String? { get } | ||
var address: String! { get } | ||
var added: () -> Void { get set } | ||
var removed: (() -> Void)? { get set } | ||
|
||
func initialize(name: String, _ secondName: String?) | ||
func fetchConfig(arg: UInt8) async throws -> [String: String] | ||
func fetchData(_ name: (String, count: Int)) async -> (() -> Void) | ||
func save(name: any Codable, surname: any Codable) | ||
func insert(name: (any Codable)?, surname: (any Codable)?) | ||
func append(name: (any Codable) -> (any Codable)?) | ||
func get() async throws -> any Codable | ||
func read() -> String! | ||
func wrapDataInArray<T>(_ data: T) -> [T] | ||
} | ||
|
||
func testPackageServiceProtocol() { | ||
let spy = PackageServiceProtocolSpy() | ||
|
||
spy.name = "Spy" | ||
} | ||
|
||
// MARK: - Internal | ||
|
||
@Spyable | ||
internal protocol InternalServiceProtocol { | ||
var name: String { get } | ||
var anyProtocol: any Codable { get set } | ||
var secondName: String? { get } | ||
var address: String! { get } | ||
var added: () -> Void { get set } | ||
var removed: (() -> Void)? { get set } | ||
|
||
func initialize(name: String, _ secondName: String?) | ||
func fetchConfig(arg: UInt8) async throws -> [String: String] | ||
func fetchData(_ name: (String, count: Int)) async -> (() -> Void) | ||
func save(name: any Codable, surname: any Codable) | ||
func insert(name: (any Codable)?, surname: (any Codable)?) | ||
func append(name: (any Codable) -> (any Codable)?) | ||
func get() async throws -> any Codable | ||
func read() -> String! | ||
func wrapDataInArray<T>(_ data: T) -> [T] | ||
} | ||
|
||
func testInternalServiceProtocol() { | ||
let spy = InternalServiceProtocolSpy() | ||
|
||
spy.name = "Spy" | ||
} | ||
|
||
// MARK: - Fileprivate | ||
|
||
@Spyable | ||
// swiftformat:disable:next | ||
private protocol FileprivateServiceProtocol { | ||
var name: String { get } | ||
var anyProtocol: any Codable { get set } | ||
var secondName: String? { get } | ||
var address: String! { get } | ||
var added: () -> Void { get set } | ||
var removed: (() -> Void)? { get set } | ||
|
||
func initialize(name: String, _ secondName: String?) | ||
func fetchConfig(arg: UInt8) async throws -> [String: String] | ||
func fetchData(_ name: (String, count: Int)) async -> (() -> Void) | ||
func save(name: any Codable, surname: any Codable) | ||
func insert(name: (any Codable)?, surname: (any Codable)?) | ||
func append(name: (any Codable) -> (any Codable)?) | ||
func get() async throws -> any Codable | ||
func read() -> String! | ||
func wrapDataInArray<T>(_ data: T) -> [T] | ||
} | ||
|
||
func testFileprivateServiceProtocol() { | ||
let spy = FileprivateServiceProtocolSpy() | ||
|
||
spy.name = "Spy" | ||
} | ||
|
||
// MARK: - Private | ||
|
||
@Spyable | ||
private protocol PrivateServiceProtocol { | ||
var name: String { get } | ||
var anyProtocol: any Codable { get set } | ||
var secondName: String? { get } | ||
var address: String! { get } | ||
var added: () -> Void { get set } | ||
var removed: (() -> Void)? { get set } | ||
|
||
func initialize(name: String, _ secondName: String?) | ||
func fetchConfig(arg: UInt8) async throws -> [String: String] | ||
func fetchData(_ name: (String, count: Int)) async -> (() -> Void) | ||
func save(name: any Codable, surname: any Codable) | ||
func insert(name: (any Codable)?, surname: (any Codable)?) | ||
func append(name: (any Codable) -> (any Codable)?) | ||
func get() async throws -> any Codable | ||
func read() -> String! | ||
func wrapDataInArray<T>(_ data: T) -> [T] | ||
} | ||
|
||
func testPrivateServiceProtocol() { | ||
let spy = PrivateServiceProtocolSpy() | ||
|
||
spy.name = "Spy" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Sources/SpyableMacro/Macro/AccessLevelModifierRewriter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import SwiftSyntax | ||
|
||
final class AccessLevelModifierRewriter: SyntaxRewriter { | ||
let newAccessLevel: DeclModifierSyntax | ||
|
||
init(newAccessLevel: DeclModifierSyntax) { | ||
/// Property / method must be declared `fileprivate` because it matches a requirement in `private` protocol. | ||
if newAccessLevel.name.text == TokenSyntax.keyword(.private).text { | ||
self.newAccessLevel = DeclModifierSyntax(name: .keyword(.fileprivate)) | ||
} else { | ||
self.newAccessLevel = newAccessLevel | ||
} | ||
} | ||
|
||
override func visit(_ node: DeclModifierListSyntax) -> DeclModifierListSyntax { | ||
if node.parent?.is(FunctionParameterSyntax.self) == true { | ||
return node | ||
} | ||
|
||
return DeclModifierListSyntax { | ||
newAccessLevel | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.