Skip to content

Make ExprList initializable by result builder #530

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 29, 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
30 changes: 30 additions & 0 deletions Sources/SwiftSyntaxBuilder/gyb_generated/BuildableNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,18 @@ public struct SequenceExpr: ExprBuildable, ExpressibleAsSequenceExpr {
self.elements = elements.createExprList()
}

/// A convenience initializer that allows:
/// - Initializing syntax collections using result builders
/// - Initializing tokens without default text using strings
public init(
leadingTrivia: Trivia = [],
@ExprListBuilder elementsBuilder: () -> ExpressibleAsExprList = { ExprList([]) }
) {
self.init(
leadingTrivia: leadingTrivia,
elements: elementsBuilder()
)
}

/// Builds a `SequenceExprSyntax`.
/// - Parameter format: The `Format` to use.
Expand Down Expand Up @@ -10987,6 +10999,24 @@ public struct YieldList: SyntaxBuildable, ExpressibleAsYieldList {
assert(rightParen.text == ")")
}

/// A convenience initializer that allows:
/// - Initializing syntax collections using result builders
/// - Initializing tokens without default text using strings
public init(
leadingTrivia: Trivia = [],
leftParen: TokenSyntax = TokenSyntax.`leftParen`,
trailingComma: TokenSyntax? = nil,
rightParen: TokenSyntax = TokenSyntax.`rightParen`,
@ExprListBuilder elementListBuilder: () -> ExpressibleAsExprList = { ExprList([]) }
) {
self.init(
leadingTrivia: leadingTrivia,
leftParen: leftParen,
elementList: elementListBuilder(),
trailingComma: trailingComma,
rightParen: rightParen
)
}

/// Builds a `YieldListSyntax`.
/// - Parameter format: The `Format` to use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
'CaseItemList': None,
'GenericArgumentList': None,
'TuplePatternElementList': None,
'ExprList': None,
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ let BUILDER_INITIALIZABLE_TYPES: [String: String?] = [
"CaseItemList": nil,
"GenericArgumentList": nil,
"TuplePatternElementList": nil,
"ExprList": nil,
]
12 changes: 6 additions & 6 deletions Tests/SwiftSyntaxBuilderTest/FunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ final class FunctionTests: XCTestCase {
},
body: ifCodeBlock)

ReturnStmt(expression: SequenceExpr(elements: ExprList {
ReturnStmt(expression: SequenceExpr {
FunctionCallExpr("fibonacci") {
SequenceExpr(elements: ExprList {
SequenceExpr {
IntegerLiteralExpr(digits: "n")
BinaryOperatorExpr("-")
IntegerLiteralExpr(1)
})
}
}

BinaryOperatorExpr("+")

FunctionCallExpr(MemberAccessExpr(base: "self", name: "fibonacci")) {
SequenceExpr(elements: ExprList {
SequenceExpr {
IntegerLiteralExpr(digits: "n")
BinaryOperatorExpr("-")
IntegerLiteralExpr(2)
})
}
}
}))
})
}
let syntax = buildable.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)

Expand Down