Skip to content

Commit 2228008

Browse files
committed
Introduce ExpressibleAs protocols
1 parent b6d7e69 commit 2228008

File tree

6 files changed

+2705
-140
lines changed

6 files changed

+2705
-140
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,19 @@ 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 {
51+
% elif kind == 'Stmt':
52+
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, SyntaxBuildable, ${kind}ListBuildable, ExpressibleAsCodeBlockItem {
4453
% else:
45-
public protocol ${kind}Buildable: SyntaxBuildable, ${kind}ListBuildable {
54+
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, SyntaxBuildable, ${kind}ListBuildable {
4655
% end
4756
/// Builds a `${build_kind}`.
4857
/// - Parameter format: The `Format` to use.
@@ -51,7 +60,13 @@ public protocol ${kind}Buildable: SyntaxBuildable, ${kind}ListBuildable {
5160
func build${kind}(format: Format, leadingTrivia: Trivia?) -> ${build_kind}
5261
}
5362

54-
extension ${kind}Buildable {
63+
extension ${buildable_type} {
64+
public func create${buildable_type}() -> ${buildable_type} {
65+
self
66+
}
67+
}
68+
69+
extension ${buildable_type} {
5570
% if kind != 'Syntax':
5671
/// Builds a `${build_kind}`.
5772
/// - Returns: A `${build_kind}`.
@@ -82,6 +97,22 @@ extension ${kind}Buildable {
8297
[build${kind}(format: format, leadingTrivia: leadingTrivia)]
8398
}
8499
}
100+
% if kind == 'Decl':
101+
102+
extension DeclBuildable {
103+
public func createMemberDeclListItem() -> MemberDeclListItem {
104+
MemberDeclListItem(decl: self)
105+
}
106+
}
107+
% end
108+
% if kind in ['Decl', 'Stmt']:
109+
110+
extension ${kind}Buildable {
111+
public func createCodeBlockItem() -> CodeBlockItem {
112+
CodeBlockItem(item: self)
113+
}
114+
}
115+
% end
85116

86117
% end
87118
% end
@@ -196,5 +227,26 @@ public struct ${node.syntax_kind}: SyntaxBuildable {
196227
}
197228
}
198229

230+
% end
231+
% if node.is_buildable() or node.is_syntax_collection():
232+
public protocol ExpressibleAs${node.syntax_kind} {
233+
func create${node.syntax_kind}() -> ${node.syntax_kind}
234+
}
235+
236+
extension ${node.syntax_kind}: ExpressibleAs${node.syntax_kind} {
237+
public func create${node.syntax_kind}() -> ${node.syntax_kind} {
238+
self
239+
}
240+
}
241+
199242
% end
200243
% end
244+
public protocol ExpressibleAsTokenSyntax {
245+
func createTokenSyntax() -> TokenSyntax
246+
}
247+
248+
extension TokenSyntax: ExpressibleAsTokenSyntax {
249+
public func createTokenSyntax() -> TokenSyntax {
250+
self
251+
}
252+
}

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)