Skip to content

Commit 57b91b1

Browse files
committed
Unify functions for generating ExpressibleAs conformances
1 parent 2caa68e commit 57b91b1

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

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
@@ -332,33 +332,6 @@ private func createFormatAdditionalLeadingTriviaParams() -> ParameterClause {
332332
}
333333
}
334334

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

0 commit comments

Comments
 (0)