|
14 | 14 | import SwiftSyntax
|
15 | 15 |
|
16 | 16 | extension FunctionCallExpr {
|
| 17 | + /// A convenience initializer that allows passing in arguments using a result builder |
| 18 | + /// instead of having to wrap them in a `TupleExprElementList`. |
| 19 | + /// The presence of the parenthesis will be inferred based on the presence of arguments and the trailing closure. |
17 | 20 | public init(
|
18 | 21 | _ calledExpression: ExpressibleAsExprBuildable,
|
19 |
| - leftParen: TokenSyntax? = .leftParen, |
20 |
| - rightParen: TokenSyntax? = .rightParen, |
21 | 22 | trailingClosure: ExpressibleAsClosureExpr? = nil,
|
22 | 23 | @TupleExprElementListBuilder argumentListBuilder: () -> ExpressibleAsTupleExprElementList = { TupleExprElementList([]) },
|
23 | 24 | @MultipleTrailingClosureElementListBuilder additionalTrailingClosuresBuilder: () -> MultipleTrailingClosureElementList? = { nil }
|
24 | 25 | ) {
|
| 26 | + let argumentList = argumentListBuilder().createTupleExprElementList() |
| 27 | + let shouldOmitParens = argumentList.elements.isEmpty && trailingClosure != nil |
25 | 28 | self.init(
|
26 | 29 | calledExpression: calledExpression.createExprBuildable(),
|
27 |
| - leftParen: leftParen, |
28 |
| - argumentList: argumentListBuilder(), |
29 |
| - rightParen: rightParen, |
| 30 | + leftParen: shouldOmitParens ? nil : .leftParen, |
| 31 | + argumentList: argumentList, |
| 32 | + rightParen: shouldOmitParens ? nil : .rightParen, |
30 | 33 | trailingClosure: trailingClosure,
|
31 | 34 | additionalTrailingClosures: additionalTrailingClosuresBuilder()
|
32 | 35 | )
|
|
0 commit comments