Skip to content

Commit

Permalink
Merge pull request #125 from Matejkob/create-pull-request/patch
Browse files Browse the repository at this point in the history
Apply swift-format changes
  • Loading branch information
Matejkob authored Nov 27, 2024
2 parents db59947 + 2babb55 commit cb44ec5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 40 deletions.
6 changes: 3 additions & 3 deletions Examples/Sources/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol ServiceProtocol {
func append(name: (any Codable) -> (any Codable)?)
func get() async throws -> any Codable
func read() -> String!
func wrapDataInArray<T>(_ data: T) -> Array<T>
func wrapDataInArray<T>(_ data: T) -> [T]
}

final class ViewModel {
Expand All @@ -44,8 +44,8 @@ final class ViewModel {
_ = try await service.fetchConfig(arg: 2)
config.removeAll()
}
func wrapData<T>(_ data: T) -> Array<T> {

func wrapData<T>(_ data: T) -> [T] {
service.wrapDataInArray(data)
}
}
2 changes: 1 addition & 1 deletion Examples/Tests/ViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class ViewModelTests: XCTestCase {
serviceSpy.wrapDataInArrayReturnValue = [123]
XCTAssertEqual(sut.wrapData(1), [123])
XCTAssertEqual(serviceSpy.wrapDataInArrayReceivedData as? Int, 1)

// ⚠️ The following would cause a fatal error, because an Array<String> will be returned by wrapData(), but we provided an Array<Int> to wrapDataInArrayReturnValue. ⚠️
// XCTAssertEqual(sut.wrapData("hi"), ["hello"])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ extension FunctionDeclSyntax {
/// Ex: `func foo() -> T` will create `var fooReturnValue: Any!`, which will be used in the spy method implementation as `fooReturnValue as! T`
var forceCastType: TypeSyntax? {
guard !genericTypes.isEmpty,
let returnType = signature.returnClause?.type,
returnType.containsGenericType(from: genericTypes) == true else {
let returnType = signature.returnClause?.type,
returnType.containsGenericType(from: genericTypes) == true
else {
return nil
}
return returnType.trimmed
Expand Down
39 changes: 21 additions & 18 deletions Sources/SpyableMacro/Extensions/TypeSyntax+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ extension TypeSyntax {
guard !genericTypes.isEmpty else { return self }

// 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.
return TypeSyntax(fromProtocol: asTypeSyntaxSupportingGenerics?.erasingGenericTypes(genericTypes)) ?? self
return TypeSyntax(
fromProtocol: asTypeSyntaxSupportingGenerics?.erasingGenericTypes(genericTypes)) ?? self
}

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

return if let type = self.as(IdentifierTypeSyntax.self),
genericTypes.contains(type.name.text) {
return
if let type = self.as(IdentifierTypeSyntax.self),
genericTypes.contains(type.name.text)
{
true
} else {
nestedTypeSyntaxes.contains { $0.containsGenericType(from: genericTypes) }
Expand All @@ -64,7 +67,7 @@ private protocol TypeSyntaxSupportingGenerics: TypeSyntaxProtocol {
}

private let typeSyntaxesSupportingGenerics: [TypeSyntaxSupportingGenerics.Type] = [
IdentifierTypeSyntax.self, // Start with IdentifierTypeSyntax for the sake of efficiency when looping through this array, as it's the most common TypeSyntax.
IdentifierTypeSyntax.self, // Start with IdentifierTypeSyntax for the sake of efficiency when looping through this array, as it's the most common TypeSyntax.
ArrayTypeSyntax.self,
GenericArgumentClauseSyntax.self,
TupleTypeSyntax.self,
Expand All @@ -82,7 +85,7 @@ extension IdentifierTypeSyntax: TypeSyntaxSupportingGenerics {
if let genericArgumentClause {
copy = copy.with(
\.genericArgumentClause,
genericArgumentClause.erasingGenericTypes(genericTypes)
genericArgumentClause.erasingGenericTypes(genericTypes)
)
}
return copy
Expand All @@ -105,14 +108,14 @@ extension GenericArgumentClauseSyntax: TypeSyntaxSupportingGenerics {
fileprivate func erasingGenericTypes(_ genericTypes: Set<String>) -> Self {
with(
\.arguments,
GenericArgumentListSyntax {
for argumentElement in arguments {
argumentElement.with(
GenericArgumentListSyntax {
for argumentElement in arguments {
argumentElement.with(
\.argument,
argumentElement.argument.erasingGenericTypes(genericTypes)
)
}
}
argumentElement.argument.erasingGenericTypes(genericTypes)
)
}
}
)
}
}
Expand All @@ -124,13 +127,13 @@ extension TupleTypeSyntax: TypeSyntaxSupportingGenerics {
fileprivate func erasingGenericTypes(_ genericTypes: Set<String>) -> Self {
with(
\.elements,
TupleTypeElementListSyntax {
for element in elements {
element.with(
TupleTypeElementListSyntax {
for element in elements {
element.with(
\.type,
element.type.erasingGenericTypes(genericTypes))
}
}
element.type.erasingGenericTypes(genericTypes))
}
}
)
}
}
10 changes: 7 additions & 3 deletions Sources/SpyableMacro/Factories/ClosureFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,24 @@ struct ClosureFactory {
/*
func f() -> String!
*/
if let implicitlyUnwrappedType = functionReturnClause.type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) {
if let implicitlyUnwrappedType = functionReturnClause.type.as(
ImplicitlyUnwrappedOptionalTypeSyntax.self)
{
var functionReturnClause = functionReturnClause
/*
`() -> String!` is not a valid code
so we have to convert it to `() -> String?
*/
functionReturnClause.type = TypeSyntax(OptionalTypeSyntax(wrappedType: implicitlyUnwrappedType.wrappedType))
functionReturnClause.type = TypeSyntax(
OptionalTypeSyntax(wrappedType: implicitlyUnwrappedType.wrappedType))
return functionReturnClause
/*
func f() -> Any
func f() -> Any?
*/
} else {
return functionReturnClause.with(\.type, functionReturnClause.type.erasingGenericTypes(genericTypes))
return functionReturnClause.with(
\.type, functionReturnClause.type.erasingGenericTypes(genericTypes))
}
/*
func f()
Expand Down
3 changes: 2 additions & 1 deletion Sources/SpyableMacro/Factories/SpyFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ struct SpyFactory {
for functionDeclaration in functionDeclarations {
let variablePrefix = variablePrefixFactory.text(for: functionDeclaration)
let genericTypes = functionDeclaration.genericTypes
let parameterList = parameterList(protocolFunctionDeclaration: functionDeclaration, genericTypes: genericTypes)
let parameterList = parameterList(
protocolFunctionDeclaration: functionDeclaration, genericTypes: genericTypes)

try callsCountFactory.variableDeclaration(variablePrefix: variablePrefix)
try calledFactory.variableDeclaration(variablePrefix: variablePrefix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ final class UT_TypeSyntax_ContainsGenericType: XCTestCase {
containsGenericType genericTypes: Set<String>
) -> Bool {
TypeSyntax(
TupleTypeSyntax(elements: TupleTypeElementListSyntax {
TupleTypeElementSyntax(type: IdentifierTypeSyntax(
name: .identifier(identifier)
))
})
TupleTypeSyntax(
elements: TupleTypeElementListSyntax {
TupleTypeElementSyntax(
type: IdentifierTypeSyntax(
name: .identifier(identifier)
))
})
)
.containsGenericType(from: genericTypes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ final class UT_TypeSyntax_ErasingGenericTypes: XCTestCase {
TupleTypeSyntax(
leadingTrivia: .space,
elements: TupleTypeElementListSyntax {
TupleTypeElementSyntax(type: IdentifierTypeSyntax(
name: .identifier(identifier)
))
TupleTypeElementSyntax(type: IdentifierTypeSyntax(
leadingTrivia: .space,
name: .identifier("Unerased")
))
TupleTypeElementSyntax(
type: IdentifierTypeSyntax(
name: .identifier(identifier)
))
TupleTypeElementSyntax(
type: IdentifierTypeSyntax(
leadingTrivia: .space,
name: .identifier("Unerased")
))
},
trailingTrivia: .space
)
Expand Down

0 comments on commit cb44ec5

Please sign in to comment.