Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions Sources/Meta/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ public struct Function: Hashable, Node {
public var body = FunctionBody()

public var resultType: TypeIdentifier?


public var `async` = false

public var `throws` = false

public var `static` = false
Expand Down Expand Up @@ -295,7 +297,13 @@ public struct Function: Hashable, Node {
_self.resultType = resultType
return _self
}


public func with(async: Bool) -> Function {
var _self = self
_self.async = `async`
return _self
}

public func with(throws: Bool) -> Function {
var _self = self
_self.throws = `throws`
Expand Down Expand Up @@ -492,7 +500,9 @@ extension Function {
.map { $0.swiftString }
.joined(separator: ", ")
.wrapped("<", ">")


let `async` = self.async ? " async" : .empty

let `throws` = self.throws ? " throws" : .empty

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

let build = {
return "\(beforeParameters)\(parameters))\(`throws`)\(resultType)\(constraints) \(self.body.swiftString)"
return "\(beforeParameters)\(parameters))\(`async`)\(`throws`)\(resultType)\(constraints) \(self.body.swiftString)"
}

if self.parameters.count > 4 || build().count > 80 {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Meta/Reference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public enum Reference: Hashable, MetaSwiftConvertible, Node {
case name(ReferenceName)
case tuple(Tuple)
case unwrap
case `await`
case `try`
case optionalTry
case `throw`
Expand Down Expand Up @@ -62,6 +63,7 @@ public enum Reference: Hashable, MetaSwiftConvertible, Node {
.name,
.tuple,
.unwrap,
.await,
.try,
.optionalTry,
.throw,
Expand Down Expand Up @@ -153,6 +155,8 @@ extension Reference {
return tuple.swiftString
case .unwrap:
return "?"
case .await:
return "await "
case .try:
return "try "
case .optionalTry:
Expand Down
21 changes: 20 additions & 1 deletion Tests/MetaTests/FileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,21 @@ final class FileTests: XCTestCase {
.adding(member: Return(value: Value.bool(true))
)
)

.adding(member: EmptyLine())
.adding(member:
Function(kind: .named("printError"))
.with(async: true)
.with(throws: true)
.with(resultType: TypeIdentifier.bool)
.adding(member: .await | .type(TypeIdentifier(name: "Logger")) + .named("error") | .call(Tuple()
.adding(parameter: TupleParameter(value: +.named("error")))
.adding(parameter: TupleParameter(value: Reference.named("lowercasedMessage")))
.adding(parameter: TupleParameter(name: "assert", value: Value.bool(true))))
)
.adding(member: Return(value: Value.bool(true))
)
)

let barID = TypeIdentifier(name: "Bar")
let bar = Type(identifier: barID)
.adding(inheritedType: .string)
Expand Down Expand Up @@ -171,6 +185,11 @@ final class FileTests: XCTestCase {
Logger.error(.error, lowercasedMessage, assert: true)
return true
}

func printError() async throws -> Bool {
await Logger.error(.error, lowercasedMessage, assert: true)
return true
}
}

public enum Bar: String {
Expand Down