Skip to content

Commit 3704b33

Browse files
authored
Merge pull request #530 from fwcd/expr-list-builder
Make `ExprList` initializable by result builder
2 parents f4b17d2 + 7ef2cbb commit 3704b33

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

Sources/SwiftSyntaxBuilder/gyb_generated/BuildableNodes.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,18 @@ public struct SequenceExpr: ExprBuildable, ExpressibleAsSequenceExpr {
842842
self.elements = elements.createExprList()
843843
}
844844

845+
/// A convenience initializer that allows:
846+
/// - Initializing syntax collections using result builders
847+
/// - Initializing tokens without default text using strings
848+
public init(
849+
leadingTrivia: Trivia = [],
850+
@ExprListBuilder elementsBuilder: () -> ExpressibleAsExprList = { ExprList([]) }
851+
) {
852+
self.init(
853+
leadingTrivia: leadingTrivia,
854+
elements: elementsBuilder()
855+
)
856+
}
845857

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

11002+
/// A convenience initializer that allows:
11003+
/// - Initializing syntax collections using result builders
11004+
/// - Initializing tokens without default text using strings
11005+
public init(
11006+
leadingTrivia: Trivia = [],
11007+
leftParen: TokenSyntax = TokenSyntax.`leftParen`,
11008+
trailingComma: TokenSyntax? = nil,
11009+
rightParen: TokenSyntax = TokenSyntax.`rightParen`,
11010+
@ExprListBuilder elementListBuilder: () -> ExpressibleAsExprList = { ExprList([]) }
11011+
) {
11012+
self.init(
11013+
leadingTrivia: leadingTrivia,
11014+
leftParen: leftParen,
11015+
elementList: elementListBuilder(),
11016+
trailingComma: trailingComma,
11017+
rightParen: rightParen
11018+
)
11019+
}
1099011020

1099111021
/// Builds a `YieldListSyntax`.
1099211022
/// - Parameter format: The `Format` to use.

Sources/SwiftSyntaxBuilder/gyb_helpers/BuilderInitializableTypes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
'CaseItemList': None,
1717
'GenericArgumentList': None,
1818
'TuplePatternElementList': None,
19+
'ExprList': None,
1920
}

Sources/SwiftSyntaxBuilderGeneration/gyb_generated/BuilderInitializableTypes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ let BUILDER_INITIALIZABLE_TYPES: [String: String?] = [
3030
"CaseItemList": nil,
3131
"GenericArgumentList": nil,
3232
"TuplePatternElementList": nil,
33+
"ExprList": nil,
3334
]

Tests/SwiftSyntaxBuilderTest/FunctionTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@ final class FunctionTests: XCTestCase {
2323
},
2424
body: ifCodeBlock)
2525

26-
ReturnStmt(expression: SequenceExpr(elements: ExprList {
26+
ReturnStmt(expression: SequenceExpr {
2727
FunctionCallExpr("fibonacci") {
28-
SequenceExpr(elements: ExprList {
28+
SequenceExpr {
2929
IntegerLiteralExpr(digits: "n")
3030
BinaryOperatorExpr("-")
3131
IntegerLiteralExpr(1)
32-
})
32+
}
3333
}
3434

3535
BinaryOperatorExpr("+")
3636

3737
FunctionCallExpr(MemberAccessExpr(base: "self", name: "fibonacci")) {
38-
SequenceExpr(elements: ExprList {
38+
SequenceExpr {
3939
IntegerLiteralExpr(digits: "n")
4040
BinaryOperatorExpr("-")
4141
IntegerLiteralExpr(2)
42-
})
42+
}
4343
}
44-
}))
44+
})
4545
}
4646
let syntax = buildable.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
4747

0 commit comments

Comments
 (0)