Skip to content

Commit d6d4f12

Browse files
committed
Improvement suggestsions to Kim’s “Add result builder to decl blocks”
1 parent 717dbba commit d6d4f12

File tree

5 files changed

+2670
-137
lines changed

5 files changed

+2670
-137
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+
return 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+
228+
% if node.is_buildable() or node.is_syntax_collection():
229+
public protocol ExpressibleAs${node.syntax_kind} {
230+
func create${node.syntax_kind}() -> ${node.syntax_kind}
231+
}
232+
233+
extension ${node.syntax_kind}: ExpressibleAs${node.syntax_kind} {
234+
public func create${node.syntax_kind}() -> ${node.syntax_kind} {
235+
return self
236+
}
237+
}
199238
% end
200239
% end
240+
241+
public protocol ExpressibleAsTokenSyntax {
242+
func createTokenSyntax() -> TokenSyntax
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

0 commit comments

Comments
 (0)