-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAddAsyncMacro.swift
More file actions
106 lines (91 loc) · 3.79 KB
/
AddAsyncMacro.swift
File metadata and controls
106 lines (91 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import MacroToolkit
import SwiftSyntax
import SwiftSyntaxMacros
// Modified from: https://github.com/DougGregor/swift-macro-examples/blob/f61ac7cdca8dc3557e53f86e7e03df1353908d3e/MacroExamplesPlugin/AddAsyncMacro.swift
public struct AddAsyncMacro: PeerMacro {
public static func expansion<
Context: MacroExpansionContext,
Declaration: DeclSyntaxProtocol
>(
of node: AttributeSyntax,
providingPeersOf declaration: Declaration,
in context: Context
) throws -> [DeclSyntax] {
// Only on functions at the moment.
guard let function = Function(declaration) else {
throw MacroError("@AddAsync only works on functions")
}
// This only makes sense for non async functions.
guard !function.isAsync else {
throw MacroError("@AddAsync requires a non async function")
}
// This only makes sense void functions
guard function.returnsVoid else {
throw MacroError("@AddAsync requires a function that returns void")
}
// Requires a completion handler block as last parameter
guard
let completionHandlerType = function.parameters.last?.type.asFunctionType
else {
throw MacroError(
"@AddAsync requires a function that has a completion handler as last parameter")
}
// Completion handler needs to return Void
guard completionHandlerType.returnType.isVoid else {
throw MacroError(
"@AddAsync requires a function that has a completion handler that returns Void")
}
guard let returnType = completionHandlerType.parameters.first else {
throw MacroError(
"@AddAsync requires a function that has a completion handler that has one parameter"
)
}
// Destructure return type
let successReturnType: Type
let isResultReturn: Bool
if case let .simple("Result", (successType, _)) = destructure(returnType) {
isResultReturn = true
successReturnType = successType
} else {
isResultReturn = false
successReturnType = returnType
}
// Remove completionHandler and comma from the previous parameter
let newParameters = function.parameters.dropLast()
// Drop the @AddAsync attribute from the new declaration.
let filteredAttributes = function.attributes.removing(node)
let callArguments = newParameters.asPassthroughArguments
let switchBody: ExprSyntax =
"""
switch returnValue {
case .success(let value):
continuation.resume(returning: value)
case .failure(let error):
continuation.resume(throwing: error)
}
"""
let continuationExpr =
isResultReturn
? "try await withCheckedThrowingContinuation { continuation in"
: "await withCheckedContinuation { continuation in"
let newBody: ExprSyntax =
"""
\(raw: continuationExpr)
\(raw: function.identifier)(\(raw: callArguments.joined(separator: ", "))) { returnValue in
\(isResultReturn ? switchBody : "continuation.resume(returning: returnValue)")
}
}
"""
// TODO: Make better codeblock init
let newFunc =
function._syntax
.withParameters(newParameters)
.withReturnType(successReturnType)
.withAsyncModifier()
.withThrowsModifier(isResultReturn)
.withBody(CodeBlockSyntax([newBody]))
.withAttributes(filteredAttributes)
.withLeadingBlankLine()
return [DeclSyntax(newFunc)]
}
}