Skip to content

Commit

Permalink
Avoid erroneous missing default diagnosis (#161)
Browse files Browse the repository at this point in the history
* Fix for @DependencyClient and default values.

* fix regression

* fix

---------

Co-authored-by: Brandon Williams <[email protected]>
  • Loading branch information
stephencelis and mbrandonw authored Dec 12, 2023
1 parent 5901e46 commit 101ba87
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
"""
Expand Down Expand Up @@ -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() {
}
}
"""
}
}

Expand Down Expand Up @@ -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() {
}
}
"""
}
}
}

0 comments on commit 101ba87

Please sign in to comment.