Skip to content

Commit cb44ec5

Browse files
authored
Merge pull request #125 from Matejkob/create-pull-request/patch
Apply swift-format changes
2 parents db59947 + 2babb55 commit cb44ec5

File tree

8 files changed

+53
-40
lines changed

8 files changed

+53
-40
lines changed

Examples/Sources/ViewModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protocol ServiceProtocol {
1717
func append(name: (any Codable) -> (any Codable)?)
1818
func get() async throws -> any Codable
1919
func read() -> String!
20-
func wrapDataInArray<T>(_ data: T) -> Array<T>
20+
func wrapDataInArray<T>(_ data: T) -> [T]
2121
}
2222

2323
final class ViewModel {
@@ -44,8 +44,8 @@ final class ViewModel {
4444
_ = try await service.fetchConfig(arg: 2)
4545
config.removeAll()
4646
}
47-
48-
func wrapData<T>(_ data: T) -> Array<T> {
47+
48+
func wrapData<T>(_ data: T) -> [T] {
4949
service.wrapDataInArray(data)
5050
}
5151
}

Examples/Tests/ViewModelTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ final class ViewModelTests: XCTestCase {
6161
serviceSpy.wrapDataInArrayReturnValue = [123]
6262
XCTAssertEqual(sut.wrapData(1), [123])
6363
XCTAssertEqual(serviceSpy.wrapDataInArrayReceivedData as? Int, 1)
64-
64+
6565
// ⚠️ The following would cause a fatal error, because an Array<String> will be returned by wrapData(), but we provided an Array<Int> to wrapDataInArrayReturnValue. ⚠️
6666
// XCTAssertEqual(sut.wrapData("hi"), ["hello"])
6767
}

Sources/SpyableMacro/Extensions/FunctionDeclSyntax+Extensions.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ extension FunctionDeclSyntax {
1212
/// Ex: `func foo() -> T` will create `var fooReturnValue: Any!`, which will be used in the spy method implementation as `fooReturnValue as! T`
1313
var forceCastType: TypeSyntax? {
1414
guard !genericTypes.isEmpty,
15-
let returnType = signature.returnClause?.type,
16-
returnType.containsGenericType(from: genericTypes) == true else {
15+
let returnType = signature.returnClause?.type,
16+
returnType.containsGenericType(from: genericTypes) == true
17+
else {
1718
return nil
1819
}
1920
return returnType.trimmed

Sources/SpyableMacro/Extensions/TypeSyntax+Extensions.swift

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ extension TypeSyntax {
2828
guard !genericTypes.isEmpty else { return self }
2929

3030
// TODO: An improvement upon this could be to throw an error here, instead of falling back to `self`. This could be ultimately used to emit a diagnostic about the unsupported TypeSyntax for a better user experience.
31-
return TypeSyntax(fromProtocol: asTypeSyntaxSupportingGenerics?.erasingGenericTypes(genericTypes)) ?? self
31+
return TypeSyntax(
32+
fromProtocol: asTypeSyntaxSupportingGenerics?.erasingGenericTypes(genericTypes)) ?? self
3233
}
3334

3435
/// Recurses through type syntaxes to find all `IdentifierTypeSyntax` leaves, and checks each of them to see if its name exists in `genericTypes`.
@@ -39,8 +40,10 @@ extension TypeSyntax {
3940
func containsGenericType(from genericTypes: Set<String>) -> Bool {
4041
guard !genericTypes.isEmpty else { return false }
4142

42-
return if let type = self.as(IdentifierTypeSyntax.self),
43-
genericTypes.contains(type.name.text) {
43+
return
44+
if let type = self.as(IdentifierTypeSyntax.self),
45+
genericTypes.contains(type.name.text)
46+
{
4447
true
4548
} else {
4649
nestedTypeSyntaxes.contains { $0.containsGenericType(from: genericTypes) }
@@ -64,7 +67,7 @@ private protocol TypeSyntaxSupportingGenerics: TypeSyntaxProtocol {
6467
}
6568

6669
private let typeSyntaxesSupportingGenerics: [TypeSyntaxSupportingGenerics.Type] = [
67-
IdentifierTypeSyntax.self, // Start with IdentifierTypeSyntax for the sake of efficiency when looping through this array, as it's the most common TypeSyntax.
70+
IdentifierTypeSyntax.self, // Start with IdentifierTypeSyntax for the sake of efficiency when looping through this array, as it's the most common TypeSyntax.
6871
ArrayTypeSyntax.self,
6972
GenericArgumentClauseSyntax.self,
7073
TupleTypeSyntax.self,
@@ -82,7 +85,7 @@ extension IdentifierTypeSyntax: TypeSyntaxSupportingGenerics {
8285
if let genericArgumentClause {
8386
copy = copy.with(
8487
\.genericArgumentClause,
85-
genericArgumentClause.erasingGenericTypes(genericTypes)
88+
genericArgumentClause.erasingGenericTypes(genericTypes)
8689
)
8790
}
8891
return copy
@@ -105,14 +108,14 @@ extension GenericArgumentClauseSyntax: TypeSyntaxSupportingGenerics {
105108
fileprivate func erasingGenericTypes(_ genericTypes: Set<String>) -> Self {
106109
with(
107110
\.arguments,
108-
GenericArgumentListSyntax {
109-
for argumentElement in arguments {
110-
argumentElement.with(
111+
GenericArgumentListSyntax {
112+
for argumentElement in arguments {
113+
argumentElement.with(
111114
\.argument,
112-
argumentElement.argument.erasingGenericTypes(genericTypes)
113-
)
114-
}
115-
}
115+
argumentElement.argument.erasingGenericTypes(genericTypes)
116+
)
117+
}
118+
}
116119
)
117120
}
118121
}
@@ -124,13 +127,13 @@ extension TupleTypeSyntax: TypeSyntaxSupportingGenerics {
124127
fileprivate func erasingGenericTypes(_ genericTypes: Set<String>) -> Self {
125128
with(
126129
\.elements,
127-
TupleTypeElementListSyntax {
128-
for element in elements {
129-
element.with(
130+
TupleTypeElementListSyntax {
131+
for element in elements {
132+
element.with(
130133
\.type,
131-
element.type.erasingGenericTypes(genericTypes))
132-
}
133-
}
134+
element.type.erasingGenericTypes(genericTypes))
135+
}
136+
}
134137
)
135138
}
136139
}

Sources/SpyableMacro/Factories/ClosureFactory.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,24 @@ struct ClosureFactory {
7272
/*
7373
func f() -> String!
7474
*/
75-
if let implicitlyUnwrappedType = functionReturnClause.type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) {
75+
if let implicitlyUnwrappedType = functionReturnClause.type.as(
76+
ImplicitlyUnwrappedOptionalTypeSyntax.self)
77+
{
7678
var functionReturnClause = functionReturnClause
7779
/*
7880
`() -> String!` is not a valid code
7981
so we have to convert it to `() -> String?
8082
*/
81-
functionReturnClause.type = TypeSyntax(OptionalTypeSyntax(wrappedType: implicitlyUnwrappedType.wrappedType))
83+
functionReturnClause.type = TypeSyntax(
84+
OptionalTypeSyntax(wrappedType: implicitlyUnwrappedType.wrappedType))
8285
return functionReturnClause
8386
/*
8487
func f() -> Any
8588
func f() -> Any?
8689
*/
8790
} else {
88-
return functionReturnClause.with(\.type, functionReturnClause.type.erasingGenericTypes(genericTypes))
91+
return functionReturnClause.with(
92+
\.type, functionReturnClause.type.erasingGenericTypes(genericTypes))
8993
}
9094
/*
9195
func f()

Sources/SpyableMacro/Factories/SpyFactory.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ struct SpyFactory {
125125
for functionDeclaration in functionDeclarations {
126126
let variablePrefix = variablePrefixFactory.text(for: functionDeclaration)
127127
let genericTypes = functionDeclaration.genericTypes
128-
let parameterList = parameterList(protocolFunctionDeclaration: functionDeclaration, genericTypes: genericTypes)
128+
let parameterList = parameterList(
129+
protocolFunctionDeclaration: functionDeclaration, genericTypes: genericTypes)
129130

130131
try callsCountFactory.variableDeclaration(variablePrefix: variablePrefix)
131132
try calledFactory.variableDeclaration(variablePrefix: variablePrefix)

Tests/SpyableMacroTests/Extensions/UT_TypeSyntax+ContainsGenericType.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ final class UT_TypeSyntax_ContainsGenericType: XCTestCase {
7777
containsGenericType genericTypes: Set<String>
7878
) -> Bool {
7979
TypeSyntax(
80-
TupleTypeSyntax(elements: TupleTypeElementListSyntax {
81-
TupleTypeElementSyntax(type: IdentifierTypeSyntax(
82-
name: .identifier(identifier)
83-
))
84-
})
80+
TupleTypeSyntax(
81+
elements: TupleTypeElementListSyntax {
82+
TupleTypeElementSyntax(
83+
type: IdentifierTypeSyntax(
84+
name: .identifier(identifier)
85+
))
86+
})
8587
)
8688
.containsGenericType(from: genericTypes)
8789
}

Tests/SpyableMacroTests/Extensions/UT_TypeSyntax+ErasingGenericType.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ final class UT_TypeSyntax_ErasingGenericTypes: XCTestCase {
7575
TupleTypeSyntax(
7676
leadingTrivia: .space,
7777
elements: TupleTypeElementListSyntax {
78-
TupleTypeElementSyntax(type: IdentifierTypeSyntax(
79-
name: .identifier(identifier)
80-
))
81-
TupleTypeElementSyntax(type: IdentifierTypeSyntax(
82-
leadingTrivia: .space,
83-
name: .identifier("Unerased")
84-
))
78+
TupleTypeElementSyntax(
79+
type: IdentifierTypeSyntax(
80+
name: .identifier(identifier)
81+
))
82+
TupleTypeElementSyntax(
83+
type: IdentifierTypeSyntax(
84+
leadingTrivia: .space,
85+
name: .identifier("Unerased")
86+
))
8587
},
8688
trailingTrivia: .space
8789
)

0 commit comments

Comments
 (0)