Skip to content

Commit e7f75c0

Browse files
committed
Introduce ExpressibleAs protocols
1 parent 717dbba commit e7f75c0

File tree

6 files changed

+2701
-140
lines changed

6 files changed

+2701
-140
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@ public protocol ${kind}ListBuildable: SyntaxListBuildable {
3939
func build${kind}List(format: Format, leadingTrivia: Trivia?) -> [${build_kind}]
4040
}
4141

42+
% buildable_type = kind + 'Buildable'
43+
public protocol ExpressibleAs${buildable_type} {
44+
func create${buildable_type}() -> ${buildable_type}
45+
}
46+
4247
% if kind == 'Syntax':
43-
public protocol ${kind}Buildable: ${kind}ListBuildable {
48+
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, ${kind}ListBuildable {
49+
% elif kind == 'Decl':
50+
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, SyntaxBuildable, ${kind}ListBuildable, ExpressibleAsMemberDeclListItem, ExpressibleAsCodeBlockItem {
4451
% else:
45-
public protocol ${kind}Buildable: SyntaxBuildable, ${kind}ListBuildable {
52+
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, SyntaxBuildable, ${kind}ListBuildable {
4653
% end
4754
/// Builds a `${build_kind}`.
4855
/// - Parameter format: The `Format` to use.
@@ -51,7 +58,13 @@ public protocol ${kind}Buildable: SyntaxBuildable, ${kind}ListBuildable {
5158
func build${kind}(format: Format, leadingTrivia: Trivia?) -> ${build_kind}
5259
}
5360

54-
extension ${kind}Buildable {
61+
extension ${buildable_type} {
62+
public func create${buildable_type}() -> ${buildable_type} {
63+
self
64+
}
65+
}
66+
67+
extension ${buildable_type} {
5568
% if kind != 'Syntax':
5669
/// Builds a `${build_kind}`.
5770
/// - Returns: A `${build_kind}`.
@@ -82,6 +95,20 @@ extension ${kind}Buildable {
8295
[build${kind}(format: format, leadingTrivia: leadingTrivia)]
8396
}
8497
}
98+
% if kind == 'Decl':
99+
100+
extension DeclBuildable {
101+
public func createMemberDeclListItem() -> MemberDeclListItem {
102+
MemberDeclListItem(decl: self)
103+
}
104+
}
105+
106+
extension DeclBuildable {
107+
public func createCodeBlockItem() -> CodeBlockItem {
108+
CodeBlockItem(item: self)
109+
}
110+
}
111+
% end
85112

86113
% end
87114
% end
@@ -196,5 +223,26 @@ public struct ${node.syntax_kind}: SyntaxBuildable {
196223
}
197224
}
198225

226+
% end
227+
% if node.is_buildable() or node.is_syntax_collection():
228+
public protocol ExpressibleAs${node.syntax_kind} {
229+
func create${node.syntax_kind}() -> ${node.syntax_kind}
230+
}
231+
232+
extension ${node.syntax_kind}: ExpressibleAs${node.syntax_kind} {
233+
public func create${node.syntax_kind}() -> ${node.syntax_kind} {
234+
self
235+
}
236+
}
237+
199238
% end
200239
% end
240+
public protocol ExpressibleAsTokenSyntax {
241+
func createTokenSyntax() -> TokenSyntax
242+
}
243+
244+
extension TokenSyntax: ExpressibleAsTokenSyntax {
245+
public func createTokenSyntax() -> TokenSyntax {
246+
self
247+
}
248+
}

Sources/SwiftSyntaxBuilder/ResultBuilders.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public struct ${node.syntax_kind}Builder {
2929

3030
/// The type of individual statement expressions in the transformed function,
3131
/// which defaults to Component if buildExpression() is not provided.
32-
public typealias Expression = ${element_type}
32+
public typealias Expression = ExpressibleAs${element_type}
3333

3434
/// The type of a partial result, which will be carried through all of the
3535
/// build methods.
36-
public typealias Component = [${element_type}]
36+
public typealias Component = [ExpressibleAs${element_type}]
3737

3838
/// The type of the final returned result, which defaults to Component if
3939
/// buildFinalResult() is not provided.
@@ -84,7 +84,7 @@ public struct ${node.syntax_kind}Builder {
8484
/// If declared, this will be called on the partial result from the outermost
8585
/// block statement to produce the final returned result.
8686
public static func buildFinalResult(_ component: Component) -> FinalResult {
87-
.init(component)
87+
.init(component.map { $0.create${element_type}() })
8888
}
8989
}
9090

Sources/SwiftSyntaxBuilder/StringLiteralExprConvenienceInitializers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
import SwiftSyntax
1414

1515
extension StringLiteralExpr {
16-
public init(_ value: String) {
16+
public init(_ value: String, openQuote: TokenSyntax = .stringQuote, closeQuote: TokenSyntax = .stringQuote) {
1717
let content = SyntaxFactory.makeToken(TokenKind.stringSegment(value), presence: .present)
1818
let segment = StringSegment(content: content)
1919
let segments = StringLiteralSegments([segment])
2020

21-
self.init(openQuote: .stringQuote,
21+
self.init(openQuote: openQuote,
2222
segments: segments,
23-
closeQuote: .stringQuote)
23+
closeQuote: closeQuote)
2424
}
2525
}
2626

0 commit comments

Comments
 (0)