Skip to content

Add a convenience initializer for IfStmt (optionally taking a second trailing builder closure) #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2022
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
34 changes: 34 additions & 0 deletions Sources/SwiftSyntaxBuilder/IfStmtConvenienceInitializers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

public extension IfStmt {
/// A convenience initializer that uses builder closures to express an
/// if body, potentially with a second trailing builder closure for an else
/// body.
init(
leadingTrivia: Trivia = [],
conditions: ExpressibleAsConditionElementList,
@CodeBlockItemListBuilder body: () -> ExpressibleAsCodeBlockItemList,
@CodeBlockItemListBuilder elseBody: () -> ExpressibleAsCodeBlockItemList? = { nil }
) {
let generatedElseBody = elseBody()
self.init(
leadingTrivia: leadingTrivia,
conditions: conditions,
body: body(),
elseKeyword: generatedElseBody == nil ? nil : SyntaxFactory.makeElseKeyword(leadingTrivia: .space, trailingTrivia: []),
elseBody: generatedElseBody.map { CodeBlock(statements: $0) }
)
}
}
39 changes: 39 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/IfStmtTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import XCTest
import SwiftSyntax
import SwiftSyntaxBuilder

final class IfStmtTests: XCTestCase {
func testEmptyIfStmt() {
// Use the convenience initializer from IfStmtConvenienceInitializers. This is
// disambiguated by the absence of a labelName parameter and the use of a
// trailing closure.
let buildable = IfStmt(conditions: ExprList([BooleanLiteralExpr(false)])) {}
let syntax = buildable.buildSyntax(format: Format())
XCTAssertEqual(syntax.description, """
if false {
}
""")
}

func testIfElseStmt() {
// Use the convenience initializer from IfStmtConvenienceInitializers
// with an else branch expressed by a second trailing closure.
let buildable = IfStmt(conditions: ExprList([BooleanLiteralExpr(true)])) {
FunctionCallExpr("print") {
TupleExprElement(expression: StringLiteralExpr("Hello from the if-branch!"))
}
} elseBody: {
FunctionCallExpr("print") {
TupleExprElement(expression: StringLiteralExpr("Hello from the else-branch!"))
}
}
let syntax = buildable.buildSyntax(format: Format())
XCTAssertEqual(syntax.description, """
if true {
print("Hello from the if-branch!")
} else {
print("Hello from the else-branch!")
}
""")
}
}