Skip to content

Commit 9f5d39b

Browse files
committed
Fix the problematic builders
1 parent cca29e0 commit 9f5d39b

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,22 @@ extension VariableDeclSyntax {
380380
}
381381
}
382382

383+
// MARK: Initializing collections from protocols
384+
// These initializers allow the creation of syntax collections that have a base
385+
// node as their element from the corresponding protocol type.
386+
387+
extension ExprListSyntax {
388+
public init(_ elements: [ExprSyntaxProtocol]) {
389+
self.init(elements.map { ExprSyntax(fromProtocol: $0) } as [ExprSyntax])
390+
}
391+
}
392+
393+
extension UnexpectedNodesSyntax {
394+
public init(_ elements: [SyntaxProtocol]) {
395+
self.init(elements.map { Syntax(fromProtocol: $0) } as [Syntax])
396+
}
397+
}
398+
383399
//==========================================================================//
384400
// IMPORTANT: If you are tempted to add an extension here, please insert //
385401
// it in alphabetical order above //

Sources/SwiftSyntaxBuilder/ResultBuilderExtensions.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,14 @@ extension MemberBlockItemListBuilder {
5050
}
5151
}
5252

53-
// MARK: Initializing collections from protocols
54-
// These initializers allow the creation of syntax collections that have a base
55-
// node as their element from the corresponding protocol type.
56-
// These are used by the result builders.
57-
// Since we only have two of these, it doesn’t make sense to generate them.
58-
59-
extension ExprListSyntax {
60-
init(_ elements: [ExprSyntaxProtocol]) {
61-
self = ExprListSyntax(elements.map { ExprSyntax(fromProtocol: $0) } as [ExprSyntax])
53+
extension ExprListBuilder {
54+
public static func buildExpression(_ expression: some ExprSyntaxProtocol) -> Component {
55+
return buildExpression(ExprSyntax(fromProtocol: expression))
6256
}
6357
}
6458

65-
extension UnexpectedNodesSyntax {
66-
public init(_ elements: [SyntaxProtocol]) {
67-
self = UnexpectedNodesSyntax(elements.map { Syntax(fromProtocol: $0) } as [Syntax])
59+
extension UnexpectedNodesBuilder {
60+
public static func buildExpression(_ expression: some SyntaxProtocol) -> Component {
61+
return buildExpression(Syntax(fromProtocol: expression))
6862
}
6963
}

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,8 @@ final class DeclarationTests: ParserTestCase {
12461246
])
12471247
),
12481248
UnexpectedNodesSyntax([
1249-
TokenSyntax.identifier("bogus"), TokenSyntax.keyword(.rethrows),
1249+
TokenSyntax.identifier("bogus"),
1250+
TokenSyntax.keyword(.rethrows),
12501251
TokenSyntax.identifier("set"),
12511252
])
12521253
),
@@ -1352,7 +1353,10 @@ final class DeclarationTests: ParserTestCase {
13521353
substructure: FunctionParameterSyntax(
13531354
firstName: .identifier("first"),
13541355
secondName: .identifier("second"),
1355-
UnexpectedNodesSyntax([TokenSyntax.identifier("third"), TokenSyntax.identifier("fourth")]),
1356+
UnexpectedNodesSyntax([
1357+
TokenSyntax.identifier("third"),
1358+
TokenSyntax.identifier("fourth"),
1359+
]),
13561360
colon: .colonToken(),
13571361
type: IdentifierTypeSyntax(name: .identifier("Int"))
13581362
),
@@ -2879,9 +2883,7 @@ final class DeclarationTests: ParserTestCase {
28792883
substructure: FunctionDeclSyntax(
28802884
funcKeyword: .keyword(.func),
28812885
name: .identifier("test"),
2882-
UnexpectedNodesSyntax([
2883-
TokenSyntax.identifier("<#name#>")
2884-
]),
2886+
UnexpectedNodesSyntax([TokenSyntax.identifier("<#name#>")]),
28852887
signature: FunctionSignatureSyntax(
28862888
parameterClause: FunctionParameterClauseSyntax(
28872889
parameters: FunctionParameterListSyntax([])

0 commit comments

Comments
 (0)