Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leading spacing on property and function declaration expansion #49

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Sources/SpyableMacro/Factories/SpyFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ struct SpyFactory {
associatedtypeDeclList: assosciatedtypeDeclarations)

let variableDeclarations = protocolDeclaration.memberBlock.members
.compactMap { $0.decl.as(VariableDeclSyntax.self) }
.compactMap { $0.decl.as(VariableDeclSyntax.self)?.removingLeadingSpaces }

let functionDeclarations = protocolDeclaration.memberBlock.members
.compactMap { $0.decl.as(FunctionDeclSyntax.self) }
.compactMap { $0.decl.as(FunctionDeclSyntax.self)?.removingLeadingSpaces }

return try ClassDeclSyntax(
name: identifier,
Expand Down Expand Up @@ -165,3 +165,21 @@ struct SpyFactory {
)
}
}

private extension SyntaxProtocol {
/// - Returns: `self` with leading space `Trivia` removed.
var removingLeadingSpaces: Self {
with(
\.leadingTrivia, Trivia(
pieces: leadingTrivia
.filter {
if case .spaces = $0 {
false
} else {
true
}
}
)
)
}
}
10 changes: 5 additions & 5 deletions Tests/SpyableMacroTests/Macro/UT_SpyableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final class UT_SpyableMacro: XCTestCase {
}
}
var underlyingAnyProtocol: (any Codable)!
var secondName: String?
var secondName: String?
var added: () -> Void {
get {
underlyingAdded
Expand All @@ -74,7 +74,7 @@ final class UT_SpyableMacro: XCTestCase {
}
}
var underlyingAdded: (() -> Void)!
var removed: (() -> Void)?
var removed: (() -> Void)?
var logoutCallsCount = 0
var logoutCalled: Bool {
return logoutCallsCount > 0
Expand All @@ -91,7 +91,7 @@ final class UT_SpyableMacro: XCTestCase {
var initializeNameSecondNameReceivedArguments: (name: String, secondName: String?)?
var initializeNameSecondNameReceivedInvocations: [(name: String, secondName: String?)] = []
var initializeNameSecondNameClosure: ((String, String?) -> Void)?
func initialize(name: String, secondName: String?) {
func initialize(name: String, secondName: String?) {
initializeNameSecondNameCallsCount += 1
initializeNameSecondNameReceivedArguments = (name, secondName)
initializeNameSecondNameReceivedInvocations.append((name, secondName))
Expand All @@ -104,7 +104,7 @@ final class UT_SpyableMacro: XCTestCase {
var fetchConfigThrowableError: (any Error)?
var fetchConfigReturnValue: [String: String]!
var fetchConfigClosure: (() async throws -> [String: String])?
func fetchConfig() async throws -> [String: String] {
func fetchConfig() async throws -> [String: String] {
fetchConfigCallsCount += 1
if let fetchConfigThrowableError {
throw fetchConfigThrowableError
Expand All @@ -123,7 +123,7 @@ final class UT_SpyableMacro: XCTestCase {
var fetchDataReceivedInvocations: [(String, count: Int)] = []
var fetchDataReturnValue: (() -> Void)!
var fetchDataClosure: (((String, count: Int)) async -> (() -> Void))?
func fetchData(_ name: (String, count: Int)) async -> (() -> Void) {
func fetchData(_ name: (String, count: Int)) async -> (() -> Void) {
fetchDataCallsCount += 1
fetchDataReceivedName = (name)
fetchDataReceivedInvocations.append((name))
Expand Down