Skip to content

Commit 3504588

Browse files
committed
Unify functions for generating ExpressibleAs conformances
1 parent 3983a8c commit 3504588

File tree

3 files changed

+29
-65
lines changed

3 files changed

+29
-65
lines changed

Sources/SwiftSyntaxBuilderGeneration/SyntaxUtilities.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,30 @@ func createFormatLeadingTriviaParameters(withDefaultTrivia: Bool = false) -> Par
7979
)
8080
}
8181
}
82+
83+
/// Generate the `create...` function for an `ExpressibleAs...` conformance.
84+
func createExpressibleAsCreateFunction(type: SyntaxBuildableType, additionalDocComments: [String] = []) -> FunctionDecl {
85+
FunctionDecl(
86+
leadingTrivia: ([
87+
"/// Conformance to `\(type.expressibleAsBaseName)`.",
88+
] + additionalDocComments).map { .docLineComment($0) + .newline }.reduce([], +),
89+
modifiers: [TokenSyntax.public],
90+
identifier: .identifier("create\(type.buildableBaseName)"),
91+
signature: FunctionSignature(
92+
input: ParameterClause(),
93+
output: type.buildable
94+
)
95+
) {
96+
ReturnStmt(expression: "self")
97+
}
98+
}
99+
100+
/// Generate the `create...` function for an `ExpressibleAs...` conformance
101+
/// that includes an explanation as to how the function disambiguates a conformance.
102+
func createDisambiguatingExpressibleAsCreateFunction(type: SyntaxBuildableType, baseType: SyntaxBuildableType) -> FunctionDecl {
103+
createExpressibleAsCreateFunction(type: baseType, additionalDocComments: [
104+
"/// `\(type.buildableBaseName)` may conform to `\(baseType.expressibleAsBaseName)` via different `ExpressibleAs*` paths.",
105+
"/// Thus, there are multiple default implementations of `create\(baseType.buildableBaseName)`, some of which perform conversions",
106+
"/// through `ExpressibleAs*` protocols. To resolve the ambiguity, provie a fixed implementation that doesn't perform any conversions.",
107+
])
108+
}

Sources/SwiftSyntaxBuilderGeneration/Templates/BuildableCollectionNodesFile.swift

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ let buildableCollectionNodesFile = SourceFile {
4444
// Generate function declarations
4545
createBuildFunction(node: node)
4646
createBuildSyntaxFunction(node: node)
47-
createExpressibleAsCreateFunction(node: node)
48-
createSyntaxBuildableCreateFunction(node: node)
47+
createExpressibleAsCreateFunction(type: type)
48+
createDisambiguatingExpressibleAsCreateFunction(type: type, baseType: .init(syntaxKind: "Syntax"))
4949
}
5050

5151
// For nodes without expressible-as conformances, conform Array to the corresponding expressible-as
@@ -258,39 +258,3 @@ private func createBuildSyntaxFunction(node: Node) -> FunctionDecl {
258258
})
259259
}
260260
}
261-
262-
/// Generate the `create...` function for the `ExpressibleAs...` conformance.
263-
private func createExpressibleAsCreateFunction(node: Node) -> FunctionDecl {
264-
let type = node.type
265-
return FunctionDecl(
266-
leadingTrivia: .docLineComment("/// Conformance to `\(type.expressibleAsBaseName)`") + .newline,
267-
modifiers: [TokenSyntax.public],
268-
identifier: .identifier("create\(type.buildableBaseName)"),
269-
signature: FunctionSignature(
270-
input: ParameterClause(),
271-
output: type.buildable
272-
)
273-
) {
274-
ReturnStmt(expression: "self")
275-
}
276-
}
277-
278-
/// Generate the `createSyntaxBuildable` function for the `SyntaxBuildable` conformance.
279-
private func createSyntaxBuildableCreateFunction(node: Node) -> FunctionDecl {
280-
let type = node.type
281-
return FunctionDecl(
282-
leadingTrivia: [
283-
"/// `\(type.buildableBaseName)` might conform to `SyntaxBuildable` via different `ExpressibleAs*` paths.",
284-
"/// Thus, there are multiple default implementations for `createSyntaxBuildable`, some of which perform conversions through `ExpressibleAs*` protocols.",
285-
"/// To resolve the ambiguity, provide a fixed implementation that doesn't perform any conversions.",
286-
].map { .docLineComment($0) + .newline }.reduce([], +),
287-
modifiers: [TokenSyntax.public],
288-
identifier: .identifier("createSyntaxBuildable"),
289-
signature: FunctionSignature(
290-
input: ParameterClause(),
291-
output: "SyntaxBuildable"
292-
)
293-
) {
294-
ReturnStmt(expression: "self")
295-
}
296-
}

Sources/SwiftSyntaxBuilderGeneration/Templates/BuildableNodesFile.swift

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -331,33 +331,6 @@ private func createFormatAdditionalLeadingTriviaParams() -> ParameterClause {
331331
}
332332
}
333333

334-
/// Generate the `create...` function for an `ExpressibleAs...` conformance.
335-
private func createExpressibleAsCreateFunction(type: SyntaxBuildableType, additionalDocComments: [String] = []) -> FunctionDecl {
336-
FunctionDecl(
337-
leadingTrivia: ([
338-
"/// Conformance to `\(type.expressibleAsBaseName)`.",
339-
] + additionalDocComments).map { .docLineComment($0) + .newline }.reduce([], +),
340-
modifiers: [TokenSyntax.public],
341-
identifier: .identifier("create\(type.buildableBaseName)"),
342-
signature: FunctionSignature(
343-
input: ParameterClause(),
344-
output: type.buildable
345-
)
346-
) {
347-
ReturnStmt(expression: "self")
348-
}
349-
}
350-
351-
/// Generate the `create...` function for an `ExpressibleAs...` conformance
352-
/// that includes an explanation as to how the function disambiguates a conformance.
353-
private func createDisambiguatingExpressibleAsCreateFunction(type: SyntaxBuildableType, baseType: SyntaxBuildableType) -> FunctionDecl {
354-
createExpressibleAsCreateFunction(type: baseType, additionalDocComments: [
355-
"/// `\(type.buildableBaseName)` may conform to `\(baseType.expressibleAsBaseName)` via different `ExpressibleAs*` paths.",
356-
"/// Thus, there are multiple default implementations of `create\(baseType.buildableBaseName)`, some of which perform conversions",
357-
"/// through `ExpressibleAs*` protocols. To resolve the ambiguity, provie a fixed implementation that doesn't perform any conversions.",
358-
])
359-
}
360-
361334
/// Generate the `withTrailingComma` function.
362335
private func createWithTrailingCommaFunction(node: Node) -> FunctionDecl {
363336
let children = node.children

0 commit comments

Comments
 (0)