Skip to content

Commit

Permalink
Work correctly with functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jrosen081 committed Jan 12, 2024
1 parent c4ba07a commit 9de9db1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/WhoopDIKitMacros/InjectableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ extension VariableDeclSyntax {
}

var typeName: TypeSyntax? {
self.bindings.first?.typeAnnotation?.type.trimmed
guard let annotationType = self.bindings.first?.typeAnnotation?.type.trimmed else { return nil }
if (annotationType.is(FunctionTypeSyntax.self)) {
return "@escaping \(annotationType)"
} else {
return annotationType
}
}
}
26 changes: 26 additions & 0 deletions Tests/WhoopDIKitTests/InjectableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,30 @@ final class InjectableTests: XCTestCase {
""",
macros: ["Injectable": InjectableMacro.self])
}

func testInjectWithClosures() {
assertMacroExpansion(
"""
@Injectable struct ClosureHolder {
let closure: () -> String
}
""",
expandedSource: """
struct ClosureHolder {
let closure: () -> String
internal static func inject() -> Self {
Self.init(closure: WhoopDI.inject(nil))
}
internal init(closure: @escaping () -> String) {
self.closure = closure
}
}
extension ClosureHolder : Injectable {
}
""",
macros: ["Injectable": InjectableMacro.self])
}
}

0 comments on commit 9de9db1

Please sign in to comment.