Skip to content

Add ExpressibleAsExprBuildable: ExpressibleAsCodeBlockItem #524

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 1 commit into from
Jul 27, 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ func createMemberDeclListItem() -> MemberDeclListItem {
return MemberDeclListItem(decl: self)
}
}
public protocol ExpressibleAsExprBuildable: ExpressibleAsExprList {
public protocol ExpressibleAsExprBuildable: ExpressibleAsExprList, ExpressibleAsCodeBlockItem {
func createExprBuildable() -> ExprBuildable
}
public extension ExpressibleAsExprBuildable {
/// Conformance to `ExpressibleAsExprList`
func createExprList() -> ExprList {
return ExprList([self])
}
/// Conformance to ExpressibleAsCodeBlockItem
func createCodeBlockItem() -> CodeBlockItem {
return CodeBlockItem(item: self)
}
}
public protocol ExpressibleAsPatternBuildable {
func createPatternBuildable() -> PatternBuildable
Expand Down Expand Up @@ -222,7 +226,7 @@ public extension ExpressibleAsAssignmentExpr {
return createAssignmentExpr()
}
}
public protocol ExpressibleAsSequenceExpr: ExpressibleAsCodeBlockItem, ExpressibleAsTupleExprElement, ExpressibleAsExprBuildable {
public protocol ExpressibleAsSequenceExpr: ExpressibleAsTupleExprElement, ExpressibleAsExprBuildable {
func createSequenceExpr() -> SequenceExpr
}
public extension ExpressibleAsSequenceExpr {
Expand Down Expand Up @@ -528,14 +532,10 @@ func createMultipleTrailingClosureElementList() -> MultipleTrailingClosureElemen
public protocol ExpressibleAsMultipleTrailingClosureElementList {
func createMultipleTrailingClosureElementList() -> MultipleTrailingClosureElementList
}
public protocol ExpressibleAsFunctionCallExpr: ExpressibleAsCodeBlockItem, ExpressibleAsExprBuildable {
public protocol ExpressibleAsFunctionCallExpr: ExpressibleAsExprBuildable {
func createFunctionCallExpr() -> FunctionCallExpr
}
public extension ExpressibleAsFunctionCallExpr {
/// Conformance to ExpressibleAsCodeBlockItem
func createCodeBlockItem() -> CodeBlockItem {
return CodeBlockItem(item: self)
}
func createExprBuildable() -> ExprBuildable {
return createFunctionCallExpr()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'ExprList': [
'ConditionElement'
],
'FunctionCallExpr': [
'ExprBuildable': [
'CodeBlockItem'
],
'MemberDeclList': [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES: [String: [String]] = [
"ExprList": [
"ConditionElement",
],
"FunctionCallExpr": [
"ExprBuildable": [
"CodeBlockItem",
],
"MemberDeclList": [
Expand Down
19 changes: 18 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/ExpressibleBuildablesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ExpressibleBuildablesTests: XCTestCase {
""")
}

func testExpressibleAsCodeBlockItem() {
func testDeclExpressibleAsCodeBlockItem() {
let myCodeBlock = SourceFile(eofToken: .eof) {
StructDecl(identifier: "MyStruct1") {}

Expand All @@ -43,6 +43,23 @@ final class ExpressibleBuildablesTests: XCTestCase {
""")
}

func testExprExpressibleAsCodeBlockItem() {
let myCodeBlock = CodeBlock(leftBrace: .leftBrace.withLeadingTrivia([])) {
FunctionCallExpr("print") { TupleExprElement(expression: StringLiteralExpr("Hello world")) }
IntegerLiteralExpr(42)
"someIdentifier"
}

let syntax = myCodeBlock.buildSyntax(format: Format())
XCTAssertEqual(syntax.description, """
{
print("Hello world")
42
someIdentifier
}
""")
}

func testExpressibleAsSwitchStmt() {
let versions = [("version_1", "1.0.0"), ("version_2", "2.0.0"), ("version_3", "3.0.0"), ("version_3_1", "3.1.0")]
let expression = IdentifierExpr("version")
Expand Down