Skip to content

Commit bac1a1d

Browse files
committed
Fix the problematic builders
1 parent 4776876 commit bac1a1d

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
@_spi(RawSyntax) import SwiftParser
1414
@_spi(RawSyntax) import SwiftSyntax
1515

16-
// MARK: - ArrayElementList
16+
// MARK: - ArrayElementListSyntax
1717

1818
extension ArrayElementListSyntax {
1919
public init(expressions: [ExprSyntax]) {
@@ -30,15 +30,15 @@ extension ArrayElementListSyntax {
3030
}
3131
}
3232

33-
// MARK: - ArrayExpr
33+
// MARK: - ArrayExprSyntax
3434

3535
extension ArrayExprSyntax {
3636
public init(expressions: [ExprSyntax]) {
3737
self.init(elements: ArrayElementListSyntax(expressions: expressions))
3838
}
3939
}
4040

41-
// MARK: - CustomAttribute
41+
// MARK: - AttributeSyntax
4242

4343
extension AttributeSyntax {
4444
/// A convenience initializer that allows passing in arguments using a result builder
@@ -58,15 +58,15 @@ extension AttributeSyntax {
5858
}
5959
}
6060

61-
// MARK: - BinaryOperatorExpr
61+
// MARK: - BinaryOperatorExprSyntax
6262

6363
extension BinaryOperatorExprSyntax {
6464
public init(text: String) {
6565
self.init(operator: .binaryOperator(text))
6666
}
6767
}
6868

69-
// MARK: - BooleanLiteralExpr
69+
// MARK: - BooleanLiteralExprSyntax
7070

7171
extension BooleanLiteralExprSyntax: ExpressibleByBooleanLiteral {
7272
public init(_ value: Bool) {
@@ -78,7 +78,7 @@ extension BooleanLiteralExprSyntax: ExpressibleByBooleanLiteral {
7878
}
7979
}
8080

81-
// MARK: - CatchClause
81+
// MARK: - CatchClauseSyntax
8282

8383
extension CatchClauseSyntax {
8484
/// A convenience initializer that calculates spacing around the `catch` keyword.
@@ -96,7 +96,7 @@ extension CatchClauseSyntax {
9696
}
9797
}
9898

99-
// MARK: - DictionaryExpr
99+
// MARK: - DictionaryExprSyntax
100100

101101
extension DictionaryExprSyntax {
102102
/// A convenience initializer that allows passing in members using a result builder
@@ -115,7 +115,15 @@ extension DictionaryExprSyntax {
115115
}
116116
}
117117

118-
// MARK: - Expr
118+
// MARK: - ExprListSyntax
119+
120+
extension ExprListSyntax {
121+
public init(_ elements: [ExprSyntaxProtocol]) {
122+
self.init(elements.map { ExprSyntax(fromProtocol: $0) } as [ExprSyntax])
123+
}
124+
}
125+
126+
// MARK: - ExprSyntax
119127

120128
extension ExprSyntax {
121129
/// Returns a syntax tree for an expression that represents the value of the
@@ -163,7 +171,7 @@ extension FloatLiteralExprSyntax: ExpressibleByFloatLiteral {
163171
}
164172
}
165173

166-
// MARK: - FunctionCallExpr
174+
// MARK: - FunctionCallExprSyntax
167175

168176
extension FunctionCallExprSyntax {
169177
/// A convenience initializer that allows passing in arguments using a result builder
@@ -188,7 +196,7 @@ extension FunctionCallExprSyntax {
188196
}
189197
}
190198

191-
// MARK: - IntegerLiteralExpr
199+
// MARK: - IntegerLiteralExprSyntax
192200

193201
extension IntegerLiteralExprSyntax: ExpressibleByIntegerLiteral {
194202
public init(_ value: Int) {
@@ -200,7 +208,21 @@ extension IntegerLiteralExprSyntax: ExpressibleByIntegerLiteral {
200208
}
201209
}
202210

203-
// MARK: - StringLiteralExpr
211+
// MARK: - LabeledExprSyntax
212+
213+
extension LabeledExprSyntax {
214+
/// A convenience initializer that allows passing in label as an optional string.
215+
/// The presence of the colon will be inferred based on the presence of the label.
216+
public init(label: String? = nil, expression: some ExprSyntaxProtocol) {
217+
self.init(
218+
label: label.map { .identifier($0) },
219+
colon: label == nil ? nil : .colonToken(trailingTrivia: .space),
220+
expression: expression
221+
)
222+
}
223+
}
224+
225+
// MARK: - StringLiteralExprSyntax
204226

205227
extension String {
206228
/// Replace literal newlines with "\r", "\n", "\u{2028}", and ASCII control characters with "\0", "\u{7}"
@@ -338,21 +360,15 @@ extension StringLiteralExprSyntax {
338360
}
339361
}
340362

341-
// MARK: - TupleExprElement
363+
// MARK: - UnexpectedNodesSyntax
342364

343-
extension LabeledExprSyntax {
344-
/// A convenience initializer that allows passing in label as an optional string.
345-
/// The presence of the colon will be inferred based on the presence of the label.
346-
public init(label: String? = nil, expression: some ExprSyntaxProtocol) {
347-
self.init(
348-
label: label.map { .identifier($0) },
349-
colon: label == nil ? nil : .colonToken(trailingTrivia: .space),
350-
expression: expression
351-
)
365+
extension UnexpectedNodesSyntax {
366+
public init(_ elements: [SyntaxProtocol]) {
367+
self.init(elements.map { Syntax(fromProtocol: $0) } as [Syntax])
352368
}
353369
}
354370

355-
// MARK: - VariableDecl
371+
// MARK: - VariableDeclSyntax
356372

357373
extension VariableDeclSyntax {
358374
/// Creates an optionally initialized property.

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)