From d762b41c43eb0c77b8d86d0f3bd466532066c415 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Tue, 12 Dec 2023 08:31:31 -0600 Subject: [PATCH 1/3] Fix for @DependencyClient and default values. --- .../DependencyClientMacroTests.swift | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift b/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift index 73427381..c9e6e020 100644 --- a/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift +++ b/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift @@ -242,6 +242,34 @@ final class DependencyClientMacroTests: BaseTestCase { } } + func testDefaultValue() { + assertMacro(record: true) { + """ + @DependencyClient + struct Client { + var endpoint: () -> Int = { 42 } + } + """ + } diagnostics: { + """ + @DependencyClient + struct Client { + var endpoint: () -> Int = { 42 } + ┬─────────────────────────── + ╰─ 🛑 Default value required for non-throwing closure 'endpoint' + ✏️ Insert '= { <#Int#> }' + } + """ + } fixes: { + """ + @DependencyClient + struct Client { + var endpoint: () -> Int = { <#Int#> } + } + """ + } + } + func testPrivate_WithDefault() { assertMacro { """ From 03fe3fff0870eeb23b6a1d322c27b32610c2d4fa Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Tue, 12 Dec 2023 08:55:43 -0800 Subject: [PATCH 2/3] fix regression --- .../DependencyClientMacro.swift | 7 +++--- .../DependencyClientMacroTests.swift | 25 +++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) 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 c9e6e020..2920c0bf 100644 --- a/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift +++ b/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift @@ -243,28 +243,27 @@ final class DependencyClientMacroTests: BaseTestCase { } func testDefaultValue() { - assertMacro(record: true) { + assertMacro { """ @DependencyClient struct Client { var endpoint: () -> Int = { 42 } } """ - } diagnostics: { + } expansion: { """ - @DependencyClient struct Client { + @DependencyEndpoint var endpoint: () -> Int = { 42 } - ┬─────────────────────────── - ╰─ 🛑 Default value required for non-throwing closure 'endpoint' - ✏️ Insert '= { <#Int#> }' - } - """ - } fixes: { - """ - @DependencyClient - struct Client { - var endpoint: () -> Int = { <#Int#> } + + init( + endpoint: @escaping () -> Int + ) { + self.endpoint = endpoint + } + + init() { + } } """ } From c9cdc8db65e8ece0663874c6cc979adca8ae4cbc Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Tue, 12 Dec 2023 09:15:27 -0800 Subject: [PATCH 3/3] fix --- .../DependencyClientMacroTests.swift | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift b/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift index 2920c0bf..b880d73b 100644 --- a/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift +++ b/Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift @@ -540,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() { + } + } + """ } } @@ -788,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() { + } + } + """ } } }