Skip to content

Commit 27d45ea

Browse files
author
StephaneMagne
committed
Add async/await support to Meta.
1 parent ac1103c commit 27d45ea

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

Sources/Meta/Function.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ public struct Function: Hashable, Node {
199199
public var body = FunctionBody()
200200

201201
public var resultType: TypeIdentifier?
202-
202+
203+
public var `async` = false
204+
203205
public var `throws` = false
204206

205207
public var `static` = false
@@ -295,7 +297,13 @@ public struct Function: Hashable, Node {
295297
_self.resultType = resultType
296298
return _self
297299
}
298-
300+
301+
public func with(async: Bool) -> Function {
302+
var _self = self
303+
_self.async = `async`
304+
return _self
305+
}
306+
299307
public func with(throws: Bool) -> Function {
300308
var _self = self
301309
_self.throws = `throws`
@@ -492,7 +500,9 @@ extension Function {
492500
.map { $0.swiftString }
493501
.joined(separator: ", ")
494502
.wrapped("<", ">")
495-
503+
504+
let `async` = self.async ? " async" : .empty
505+
496506
let `throws` = self.throws ? " throws" : .empty
497507

498508
let resultType = self.resultType?.swiftString.prefixed(" -> ") ?? .empty
@@ -510,7 +520,7 @@ extension Function {
510520
.joined(separator: ", ")
511521

512522
let build = {
513-
return "\(beforeParameters)\(parameters))\(`throws`)\(resultType)\(constraints) \(self.body.swiftString)"
523+
return "\(beforeParameters)\(parameters))\(`async`)\(`throws`)\(resultType)\(constraints) \(self.body.swiftString)"
514524
}
515525

516526
if self.parameters.count > 4 || build().count > 80 {

Sources/Meta/Reference.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public enum Reference: Hashable, MetaSwiftConvertible, Node {
3535
case name(ReferenceName)
3636
case tuple(Tuple)
3737
case unwrap
38+
case `await`
3839
case `try`
3940
case optionalTry
4041
case `throw`
@@ -62,6 +63,7 @@ public enum Reference: Hashable, MetaSwiftConvertible, Node {
6263
.name,
6364
.tuple,
6465
.unwrap,
66+
.await,
6567
.try,
6668
.optionalTry,
6769
.throw,
@@ -153,6 +155,8 @@ extension Reference {
153155
return tuple.swiftString
154156
case .unwrap:
155157
return "?"
158+
case .await:
159+
return "await "
156160
case .try:
157161
return "try "
158162
case .optionalTry:

Tests/MetaTests/FileTests.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,21 @@ final class FileTests: XCTestCase {
9090
.adding(member: Return(value: Value.bool(true))
9191
)
9292
)
93-
93+
.adding(member: EmptyLine())
94+
.adding(member:
95+
Function(kind: .named("printError"))
96+
.with(async: true)
97+
.with(throws: true)
98+
.with(resultType: TypeIdentifier.bool)
99+
.adding(member: .await | .type(TypeIdentifier(name: "Logger")) + .named("error") | .call(Tuple()
100+
.adding(parameter: TupleParameter(value: +.named("error")))
101+
.adding(parameter: TupleParameter(value: Reference.named("lowercasedMessage")))
102+
.adding(parameter: TupleParameter(name: "assert", value: Value.bool(true))))
103+
)
104+
.adding(member: Return(value: Value.bool(true))
105+
)
106+
)
107+
94108
let barID = TypeIdentifier(name: "Bar")
95109
let bar = Type(identifier: barID)
96110
.adding(inheritedType: .string)
@@ -171,6 +185,11 @@ final class FileTests: XCTestCase {
171185
Logger.error(.error, lowercasedMessage, assert: true)
172186
return true
173187
}
188+
189+
func printError() async throws -> Bool {
190+
await Logger.error(.error, lowercasedMessage, assert: true)
191+
return true
192+
}
174193
}
175194
176195
public enum Bar: String {

0 commit comments

Comments
 (0)