Skip to content

Commit be398f6

Browse files
committed
Simplify .some(…) generation
1 parent e69832f commit be398f6

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ extension FloatLiteralExprSyntax: ExpressibleByFloatLiteral {
141141
// MARK: - FunctionCallExpr
142142

143143
extension FunctionCallExpr {
144+
// Need an overload that's explicitly `ExprSyntax` for code literals to work.
144145
/// A convenience initializer that allows passing in arguments using a result builder
145146
/// instead of having to wrap them in a `TupleExprElementList`.
146147
/// The presence of the parenthesis will be inferred based on the presence of arguments and the trailing closure.
@@ -161,6 +162,23 @@ extension FunctionCallExpr {
161162
additionalTrailingClosures: additionalTrailingClosures
162163
)
163164
}
165+
166+
/// A convenience initializer that allows passing in arguments using a result builder
167+
/// instead of having to wrap them in a `TupleExprElementList`.
168+
/// The presence of the parenthesis will be inferred based on the presence of arguments and the trailing closure.
169+
public init(
170+
callee: ExprSyntaxProtocol,
171+
trailingClosure: ClosureExprSyntax? = nil,
172+
additionalTrailingClosures: MultipleTrailingClosureElementList? = nil,
173+
@TupleExprElementListBuilder argumentList: () -> TupleExprElementList = { [] }
174+
) {
175+
self.init(
176+
callee: ExprSyntax(fromProtocol: callee),
177+
trailingClosure: trailingClosure,
178+
additionalTrailingClosures: additionalTrailingClosures,
179+
argumentList: argumentList
180+
)
181+
}
164182
}
165183

166184
// MARK: - FunctionParameter

Sources/SwiftSyntaxBuilder/Syntax+StringInterpolation.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,9 @@ extension Optional: ExpressibleByLiteralSyntax where Wrapped: ExpressibleByLiter
420420
// e.g. `.some(nil)`, add a layer of `.some(_:)` around it to preserve the
421421
// depth of the data structure.
422422
if containsNil(wrappedExpr) {
423-
// TODO: Can't we have something nicer than this? `MemberAccessExpr(name: "some").makeCall(wrapped)`?
424-
return FunctionCallExpr(
425-
calledExpression: MemberAccessExpr(name: "some"),
426-
leftParen: .leftParen,
427-
argumentList: TupleExprElementList {
428-
TupleExprElement(expression: wrappedExpr)
429-
},
430-
rightParen: .rightParen
431-
)
423+
return FunctionCallExpr(callee: MemberAccessExpr(name: "some")) {
424+
TupleExprElement(expression: wrappedExpr)
425+
}
432426
}
433427

434428
return wrappedExpr

0 commit comments

Comments
 (0)