Skip to content

Commit eeaf6e8

Browse files
committed
[MacroExamples] AddAsync: Handle implicit Void result
1 parent c4f53b3 commit eeaf6e8

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

Examples/Sources/MacroExamples/Implementation/Peer/AddAsyncMacro.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public struct AddAsyncMacro: PeerMacro {
4242
}
4343

4444
// This only makes sense void functions
45-
if funcDecl.signature.returnClause?.type.as(IdentifierTypeSyntax.self)?.name.text != "Void" {
45+
if let returnClause = funcDecl.signature.returnClause,
46+
returnClause.type.as(IdentifierTypeSyntax.self)?.name.text != "Void"
47+
{
4648
throw CustomError.message(
4749
"@addAsync requires an function that returns void"
4850
)

Examples/Tests/MacroExamples/Implementation/Peer/AddAsyncMacroTests.swift

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,32 @@ final class AddAsyncMacroTests: XCTestCase {
7979
)
8080
}
8181

82+
func testImplicitVoidResult() {
83+
assertMacroExpansion(
84+
"""
85+
@AddAsync
86+
func d(a: Int, completionBlock: @escaping (Bool) -> Void) {
87+
}
88+
""",
89+
expandedSource: """
90+
func d(a: Int, completionBlock: @escaping (Bool) -> Void) {
91+
}
92+
93+
func d(a: Int) async -> Bool {
94+
await withCheckedContinuation { continuation in
95+
d(a: a) { returnValue in
96+
97+
continuation.resume(returning: returnValue)
98+
}
99+
}
100+
101+
}
102+
""",
103+
macros: macros,
104+
indentationWidth: .spaces(2)
105+
)
106+
}
107+
82108
func testExpansionOnStoredPropertyEmitsError() {
83109
assertMacroExpansion(
84110
"""
@@ -105,18 +131,18 @@ final class AddAsyncMacroTests: XCTestCase {
105131
)
106132
}
107133

108-
func testExpansionOnAsyncFunctionEmitsError() {
134+
func testNonVoidResult() {
109135
assertMacroExpansion(
110136
"""
111137
struct Test {
112138
@AddAsync
113-
async func sayHello() {
139+
func sayHello() -> Int {
114140
}
115141
}
116142
""",
117143
expandedSource: """
118144
struct Test {
119-
async func sayHello() {
145+
func sayHello() -> Int {
120146
}
121147
}
122148
""",
@@ -132,4 +158,50 @@ final class AddAsyncMacroTests: XCTestCase {
132158
indentationWidth: .spaces(2)
133159
)
134160
}
161+
162+
func testAlreadyAsync() {
163+
assertMacroExpansion(
164+
"""
165+
@AddAsync
166+
func d(a: Int, completionBlock: @escaping (Bool) -> Void) async {
167+
}
168+
""",
169+
expandedSource: """
170+
func d(a: Int, completionBlock: @escaping (Bool) -> Void) async {
171+
}
172+
""",
173+
diagnostics: [
174+
DiagnosticSpec(
175+
message: "@addAsync requires an non async function",
176+
line: 1,
177+
column: 1,
178+
severity: .error
179+
)
180+
],
181+
macros: macros,
182+
indentationWidth: .spaces(2)
183+
)
184+
}
185+
186+
func testNoCompletionHandler() {
187+
assertMacroExpansion(
188+
"""
189+
@AddAsync
190+
func sayHello(x: Int) {}
191+
""",
192+
expandedSource: """
193+
func sayHello(x: Int) {}
194+
""",
195+
diagnostics: [
196+
DiagnosticSpec(
197+
message: "@addAsync requires an function that has a completion handler as last parameter",
198+
line: 1,
199+
column: 1,
200+
severity: .error
201+
)
202+
],
203+
macros: macros,
204+
indentationWidth: .spaces(2)
205+
)
206+
}
135207
}

0 commit comments

Comments
 (0)