diff --git a/Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift b/Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift index 9ab60197..49fda196 100644 --- a/Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift +++ b/Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift @@ -22,10 +22,9 @@ public enum DependencyClientMacro: MemberAttributeMacro, MemberMacro { return [] } // NB: Ideally `@DependencyEndpoint` would handle this for us, but there are compiler crashes - if let initializer = binding.initializer, - try initializer.diagnose(node, context: context).earlyOut - { - return [] + if let initializer = binding.initializer { + guard try !initializer.diagnose(node, context: context).earlyOut + else { return [] } } else if functionType.effectSpecifiers?.throwsSpecifier == nil, !functionType.isVoid, !functionType.isOptional diff --git a/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift b/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift index 73427381..b880d73b 100644 --- a/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift +++ b/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift @@ -242,6 +242,33 @@ final class DependencyClientMacroTests: BaseTestCase { } } + func testDefaultValue() { + assertMacro { + """ + @DependencyClient + struct Client { + var endpoint: () -> Int = { 42 } + } + """ + } expansion: { + """ + struct Client { + @DependencyEndpoint + var endpoint: () -> Int = { 42 } + + init( + endpoint: @escaping () -> Int + ) { + self.endpoint = endpoint + } + + init() { + } + } + """ + } + } + func testPrivate_WithDefault() { assertMacro { """ @@ -513,6 +540,22 @@ final class DependencyClientMacroTests: BaseTestCase { var endpoint: @Sendable () -> Int = { <#Int#> } } """ + } expansion: { + """ + struct Client: Sendable { + @DependencyEndpoint + var endpoint: @Sendable () -> Int = { <#Int#> } + + init( + endpoint: @Sendable @escaping () -> Int + ) { + self.endpoint = endpoint + } + + init() { + } + } + """ } } @@ -761,6 +804,26 @@ final class DependencyClientMacroTests: BaseTestCase { public var bar: () -> String = { { fatalError("Goodbye") }() } } """ + } expansion: { + """ + struct Blah { + @DependencyEndpoint + public var foo: () -> String = { { fatalError() }() } + @DependencyEndpoint + public var bar: () -> String = { { fatalError("Goodbye") }() } + + public init( + foo: @escaping () -> String, + bar: @escaping () -> String + ) { + self.foo = foo + self.bar = bar + } + + public init() { + } + } + """ } } }