Skip to content

Commit 21d78c8

Browse files
committed
Remove ExpressibleAsTokenSyntax protocol
1 parent 8d74eda commit 21d78c8

18 files changed

+1006
-1006
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,20 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
128128
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
129129
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
130130
% default_value = syntax_buildable_default_init_value(child, child_token)
131-
% init_parameters.append("%s: ExpressibleAs%s%s" % (child.swift_name, param_type, default_value))
131+
% param_type = param_type if child.is_token() else "ExpressibleAs" + param_type
132+
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
132133
% end
133134
${',\n '.join(init_parameters)}
134135
) {
135136
% for child in node.children:
136137
% create_method_dot = '?.' if child.is_optional else '.'
137138
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
138139
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token())
140+
% if param_type is 'TokenSyntax':
141+
self.${child.swift_name} = ${child.swift_name}
142+
% else:
139143
self.${child.swift_name} = ${child.swift_name}${create_method_dot}create${param_type}()
144+
% end
140145
% end
141146
}
142147

@@ -192,8 +197,13 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
192197
public struct ${node.syntax_kind}: SyntaxBuildable {
193198
let elements: [${element_type}]
194199

195-
public init(_ elements: [ExpressibleAs${element_type}]) {
200+
% param_type = element_type if node.is_token() else 'ExpressibleAs' + element_type
201+
public init(_ elements: [${param_type}]) {
202+
% if node.is_token():
203+
self.elements = elements
204+
% else:
196205
self.elements = elements.map { $0.create${element_type}() }
206+
% end
197207
}
198208

199209
public func build${node.syntax_kind}(format: Format) -> ${node.syntax_kind}Syntax {
@@ -243,21 +253,13 @@ extension ${node.syntax_kind}: ${expressible_as_type} {
243253

244254
% end
245255
% end
246-
public protocol ExpressibleAsTokenSyntax {
247-
func createTokenSyntax() -> TokenSyntax
248-
}
249-
250-
extension TokenSyntax: ExpressibleAsTokenSyntax {
251-
public func createTokenSyntax() -> TokenSyntax {
252-
self
253-
}
254-
}
255256

256257
// MARK: - Syntax buildable expressible as conformances
257258

258259
% for protocol, conformances in SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.items():
259260
% for conformance in conformances:
260-
extension ExpressibleAs${protocol} {
261+
% extension_protocol = protocol if protocol is 'TokenSyntax' else 'ExpressibleAs' + protocol
262+
extension ${extension_protocol} {
261263
public func create${conformance}() -> ${conformance} {
262264
${conformance}([self])
263265
}
@@ -272,7 +274,8 @@ extension TokenSyntax: ExpressibleAsTokenSyntax {
272274
% non_defaulted_params = filter(lambda child : syntax_buildable_default_init_value(child, SYNTAX_TOKEN_MAP.get(child.syntax_kind)) == "", node.children)
273275
% assert len(non_defaulted_params) == 1, "ExpressibleAs conformances expects the conforming type to have an initializer with a single non-optional child"
274276
% param = non_defaulted_params[0].swift_name
275-
extension ExpressibleAs${protocol} {
277+
% extension_protocol = protocol if protocol is 'TokenSyntax' else 'ExpressibleAs' + protocol
278+
extension ${extension_protocol} {
276279
public func create${conformance}() -> ${conformance} {
277280
${conformance}(${param}: self)
278281
}

Sources/SwiftSyntaxBuilder/BuildablesConvenienceInitializers.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ extension ${node.syntax_kind} {
5252
% else:
5353
% # When type is not handled above, use default value
5454
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
55+
% param_type = param_type if child.is_token() else "ExpressibleAs" + param_type
5556
% default_value = syntax_buildable_default_init_value(child, child_token)
56-
% init_parameters.append("%s: ExpressibleAs%s%s" % (child.swift_name, param_type, default_value))
57+
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
5758
% end
5859
% end
5960
${',\n '.join(init_parameters + init_result_builder_parameters)}

Sources/SwiftSyntaxBuilder/FunctionCallExprConvenienceInitializers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
//===----------------------------------------------------------------------===//
1212

1313

14-
import Foundation
14+
import SwiftSyntax
1515

1616
extension FunctionCallExpr {
1717
public init(
1818
_ calledExpression: ExpressibleAsIdentifierExpr,
19-
leftParen: ExpressibleAsTokenSyntax? = nil,
20-
rightParen: ExpressibleAsTokenSyntax? = nil,
19+
leftParen: TokenSyntax? = nil,
20+
rightParen: TokenSyntax? = nil,
2121
trailingClosure: ExpressibleAsClosureExpr? = nil,
2222
@TupleExprElementListBuilder argumentListBuilder: () -> TupleExprElementList = { .empty },
2323
@MultipleTrailingClosureElementListBuilder additionalTrailingClosuresBuilder: () -> MultipleTrailingClosureElementList? = { nil }

Sources/SwiftSyntaxBuilder/ResultBuilders.swift.gyb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ import SwiftSyntax
2626
@resultBuilder
2727
public struct ${node.syntax_kind}Builder {
2828
% element_type = syntax_buildable_child_type(node.collection_element_type, node.collection_element, node.is_token())
29-
29+
% component_type = element_type if node.is_token() else 'ExpressibleAs' + element_type
30+
3031
/// The type of individual statement expressions in the transformed function,
3132
/// which defaults to Component if buildExpression() is not provided.
32-
public typealias Expression = ExpressibleAs${element_type}
33+
public typealias Expression = ${component_type}
3334

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

3839
/// The type of the final returned result, which defaults to Component if
3940
/// buildFinalResult() is not provided.
@@ -84,7 +85,11 @@ public struct ${node.syntax_kind}Builder {
8485
/// If declared, this will be called on the partial result from the outermost
8586
/// block statement to produce the final returned result.
8687
public static func buildFinalResult(_ component: Component) -> FinalResult {
88+
% if node.is_token():
89+
.init(component)
90+
% else:
8791
.init(component.map { $0.create${element_type}() })
92+
% end
8893
}
8994
}
9095

Sources/SwiftSyntaxBuilder/Tokens.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import SwiftSyntax
2222

2323
/// Namespace for commonly used tokens with default trivia.
24-
public extension ExpressibleAsTokenSyntax where Self == TokenSyntax {
24+
public extension TokenSyntax {
2525
% for token in SYNTAX_TOKENS:
2626
% if token.is_keyword:
2727
/// The `${token.text.encode('utf-8').decode('unicode_escape')}` keyword

0 commit comments

Comments
 (0)