Skip to content

Commit

Permalink
Merge pull request #49 from dafurman/David/fixExpansionSpacing
Browse files Browse the repository at this point in the history
Fix leading spacing on property and function declaration expansion
  • Loading branch information
Matejkob committed Dec 31, 2023
2 parents 4fc1535 + 999e2f8 commit 9491fc7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
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

0 comments on commit 9491fc7

Please sign in to comment.