Skip to content

Commit 05baf4c

Browse files
committed
Add ExpressibleAsExprBuildable: ExpressibleAsCodeBlockItem
This generalizes the current conformance of ExpressibleAsFunctionCallExpr to ExpressibleAsCodeBlockItem to arbitrary expressions.
1 parent d9af173 commit 05baf4c

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

Sources/SwiftSyntaxBuilder/generated/ExpressibleAsProtocols.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ func createMemberDeclListItem() -> MemberDeclListItem {
2727
return MemberDeclListItem(decl: self)
2828
}
2929
}
30-
public protocol ExpressibleAsExprBuildable: ExpressibleAsExprList {
30+
public protocol ExpressibleAsExprBuildable: ExpressibleAsExprList, ExpressibleAsCodeBlockItem {
3131
func createExprBuildable() -> ExprBuildable
3232
}
3333
public extension ExpressibleAsExprBuildable {
3434
/// Conformance to `ExpressibleAsExprList`
3535
func createExprList() -> ExprList {
3636
return ExprList([self])
3737
}
38+
/// Conformance to ExpressibleAsCodeBlockItem
39+
func createCodeBlockItem() -> CodeBlockItem {
40+
return CodeBlockItem(item: self)
41+
}
3842
}
3943
public protocol ExpressibleAsPatternBuildable {
4044
func createPatternBuildable() -> PatternBuildable
@@ -222,7 +226,7 @@ public extension ExpressibleAsAssignmentExpr {
222226
return createAssignmentExpr()
223227
}
224228
}
225-
public protocol ExpressibleAsSequenceExpr: ExpressibleAsCodeBlockItem, ExpressibleAsTupleExprElement, ExpressibleAsExprBuildable {
229+
public protocol ExpressibleAsSequenceExpr: ExpressibleAsTupleExprElement, ExpressibleAsExprBuildable {
226230
func createSequenceExpr() -> SequenceExpr
227231
}
228232
public extension ExpressibleAsSequenceExpr {
@@ -528,14 +532,10 @@ func createMultipleTrailingClosureElementList() -> MultipleTrailingClosureElemen
528532
public protocol ExpressibleAsMultipleTrailingClosureElementList {
529533
func createMultipleTrailingClosureElementList() -> MultipleTrailingClosureElementList
530534
}
531-
public protocol ExpressibleAsFunctionCallExpr: ExpressibleAsCodeBlockItem, ExpressibleAsExprBuildable {
535+
public protocol ExpressibleAsFunctionCallExpr: ExpressibleAsExprBuildable {
532536
func createFunctionCallExpr() -> FunctionCallExpr
533537
}
534538
public extension ExpressibleAsFunctionCallExpr {
535-
/// Conformance to ExpressibleAsCodeBlockItem
536-
func createCodeBlockItem() -> CodeBlockItem {
537-
return CodeBlockItem(item: self)
538-
}
539539
func createExprBuildable() -> ExprBuildable {
540540
return createFunctionCallExpr()
541541
}

Sources/SwiftSyntaxBuilder/gyb_helpers/ExpressibleAsConformances.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'ExprList': [
1919
'ConditionElement'
2020
],
21-
'FunctionCallExpr': [
21+
'ExprBuildable': [
2222
'CodeBlockItem'
2323
],
2424
'MemberDeclList': [

Sources/SwiftSyntaxBuilderGeneration/gyb_generated/ExpressibleAsConformances.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ let SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES: [String: [String]] = [
3232
"ExprList": [
3333
"ConditionElement",
3434
],
35-
"FunctionCallExpr": [
35+
"ExprBuildable": [
3636
"CodeBlockItem",
3737
],
3838
"MemberDeclList": [

Tests/SwiftSyntaxBuilderTest/ExpressibleBuildablesTests.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class ExpressibleBuildablesTests: XCTestCase {
2626
""")
2727
}
2828

29-
func testExpressibleAsCodeBlockItem() {
29+
func testDeclExpressibleAsCodeBlockItem() {
3030
let myCodeBlock = SourceFile(eofToken: .eof) {
3131
StructDecl(identifier: "MyStruct1") {}
3232

@@ -43,6 +43,23 @@ final class ExpressibleBuildablesTests: XCTestCase {
4343
""")
4444
}
4545

46+
func testExprExpressibleAsCodeBlockItem() {
47+
let myCodeBlock = CodeBlock(leftBrace: .leftBrace.withLeadingTrivia([])) {
48+
FunctionCallExpr("print") { TupleExprElement(expression: StringLiteralExpr("Hello world")) }
49+
IntegerLiteralExpr(42)
50+
"someIdentifier"
51+
}
52+
53+
let syntax = myCodeBlock.buildSyntax(format: Format())
54+
XCTAssertEqual(syntax.description, """
55+
{
56+
print("Hello world")
57+
42
58+
someIdentifier
59+
}
60+
""")
61+
}
62+
4663
func testExpressibleAsSwitchStmt() {
4764
let versions = [("version_1", "1.0.0"), ("version_2", "2.0.0"), ("version_3", "3.0.0"), ("version_3_1", "3.1.0")]
4865
let expression = IdentifierExpr("version")

0 commit comments

Comments
 (0)