Skip to content
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
12 changes: 8 additions & 4 deletions Sources/SpyableMacro/Extensions/TypeSyntax+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,17 @@ extension GenericArgumentClauseSyntax: TypeSyntaxSupportingGenerics {
newArgument = type.erasingGenericTypes(genericTypes)
default: continue
}
let newArgumentElement = GenericArgumentSyntax(
argument: .type(newArgument),
trailingComma: argumentElement.trailingComma
)
#else
let newArgument: TypeSyntax = argumentElement.argument.erasingGenericTypes(genericTypes)
let newArgumentElement = GenericArgumentSyntax(
argument: newArgument,
trailingComma: argumentElement.trailingComma
)
#endif
let newArgumentElement = GenericArgumentSyntax(
argument: newArgument,
trailingComma: argumentElement.trailingComma
)
newArgumentElements.append(newArgumentElement)
}

Expand Down
36 changes: 24 additions & 12 deletions Sources/SpyableMacro/Factories/ClosureFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ struct ClosureFactory {
let genericTypes = protocolFunctionDeclaration.genericTypes
let returnClause = returnClause(protocolFunctionDeclaration: protocolFunctionDeclaration)

#if canImport(SwiftSyntax600)
let effectSpecifiers = TypeEffectSpecifiersSyntax(
asyncSpecifier: functionSignature.effectSpecifiers?.asyncSpecifier,
throwsClause: functionSignature.effectSpecifiers?.throwsClause
)
#else
let effectSpecifiers = TypeEffectSpecifiersSyntax(
asyncSpecifier: functionSignature.effectSpecifiers?.asyncSpecifier,
throwsSpecifier: functionSignature.effectSpecifiers?.throwsSpecifier
)
#endif

let elements = TupleTypeElementListSyntax {
TupleTypeElementSyntax(
type: FunctionTypeSyntax(
Expand All @@ -46,10 +58,7 @@ struct ClosureFactory {
)
}
},
effectSpecifiers: TypeEffectSpecifiersSyntax(
asyncSpecifier: functionSignature.effectSpecifiers?.asyncSpecifier,
throwsSpecifier: functionSignature.effectSpecifiers?.throwsSpecifier
),
effectSpecifiers: effectSpecifiers,
returnClause: returnClause
)
)
Expand Down Expand Up @@ -166,7 +175,13 @@ struct ClosureFactory {
expression = AwaitExprSyntax(expression: expression)
}

if functionSignature.effectSpecifiers?.throwsSpecifier != nil {
#if canImport(SwiftSyntax600)
let throwsSpecifier = functionSignature.effectSpecifiers?.throwsClause?.throwsSpecifier
#else
let throwsSpecifier = functionSignature.effectSpecifiers?.throwsSpecifier
#endif

if throwsSpecifier != nil {
expression = TryExprSyntax(expression: expression)
}

Expand All @@ -188,12 +203,9 @@ struct ClosureFactory {

extension FunctionParameterListSyntax.Element {
fileprivate var isInoutParameter: Bool {
if let attributedType = self.type.as(AttributedTypeSyntax.self),
attributedType.specifier?.text == TokenSyntax.keyword(.inout).text
{
return true
} else {
return false
}
// Check if the type contains 'inout' anywhere in its description
// This works regardless of SwiftSyntax version and handles cases like "isolated inout"
let typeDescription = self.type.description.trimmingCharacters(in: .whitespacesAndNewlines)
return typeDescription.contains(TokenSyntax.keyword(.inout).text)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ struct FunctionImplementationFactory {
)
}

if protocolFunctionDeclaration.signature.effectSpecifiers?.throwsSpecifier != nil {
#if canImport(SwiftSyntax600)
let throwsSpecifier = protocolFunctionDeclaration.signature.effectSpecifiers?.throwsClause?.throwsSpecifier
#else
let throwsSpecifier = protocolFunctionDeclaration.signature.effectSpecifiers?.throwsSpecifier
#endif

if throwsSpecifier != nil {
throwableErrorFactory.throwErrorExpression(variablePrefix: variablePrefix)
}

Expand Down
8 changes: 7 additions & 1 deletion Sources/SpyableMacro/Factories/SpyFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,14 @@ struct SpyFactory {
parameterList: parameterList
)
}

#if canImport(SwiftSyntax600)
let throwsSpecifier = functionDeclaration.signature.effectSpecifiers?.throwsClause?.throwsSpecifier
#else
let throwsSpecifier = functionDeclaration.signature.effectSpecifiers?.throwsSpecifier
#endif

if functionDeclaration.signature.effectSpecifiers?.throwsSpecifier != nil {
if throwsSpecifier != nil {
try throwableErrorFactory.variableDeclaration(variablePrefix: variablePrefix)
}

Expand Down
16 changes: 16 additions & 0 deletions Tests/SpyableMacroTests/Factories/UT_ClosureFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ final class UT_ClosureFactory: XCTestCase {
)
}

func testVariableDeclarationWithIsolatedInoutAttribute() throws {
try assertProtocolFunction(
withFunctionDeclaration: "func _ignore_(value: isolated inout TestActor)",
prefixForVariable: "_prefix_",
expectingVariableDeclaration: "var _prefix_Closure: ((isolated inout TestActor) -> Void)?"
)
}

func testVariableDeclarationWithGenericParameter() throws {
try assertProtocolFunction(
withFunctionDeclaration: "func _ignore_<T>(value: T)",
Expand Down Expand Up @@ -133,6 +141,14 @@ final class UT_ClosureFactory: XCTestCase {
)
}

func testCallExpressionWithIsolatedInoutAttribute() throws {
try assertProtocolFunction(
withFunctionDeclaration: "func _ignore_(value: isolated inout TestActor)",
prefixForVariable: "_prefix_",
expectingCallExpression: "_prefix_Closure?(&value)"
)
}

func testCallExpressionWithGenericParameter() throws {
try assertProtocolFunction(
withFunctionDeclaration: "func _ignore_<T>(value: T)",
Expand Down