Skip to content

Commit 2e9dc11

Browse files
committed
Factor out TypeInheritanceClause utility function
1 parent b91cfef commit 2e9dc11

File tree

4 files changed

+12
-20
lines changed

4 files changed

+12
-20
lines changed

Sources/SwiftSyntaxBuilderGeneration/SyntaxUtilities.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ func createTokenSyntaxPatternBinding(_ pattern: ExpressibleAsPatternBuildable, a
5353
initializer: nil,
5454
accessor: accessor)
5555
}
56+
57+
func createTypeInheritanceClause(conformances: [String]) -> TypeInheritanceClause? {
58+
conformances.isEmpty ? nil : TypeInheritanceClause {
59+
for conformance in conformances {
60+
InheritedType(typeName: conformance)
61+
}
62+
}
63+
}

Sources/SwiftSyntaxBuilderGeneration/Templates/BuildableBaseProtocolsFile.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ let buildableBaseProtocolsFile = SourceFile {
3636
ProtocolDecl(
3737
modifiers: [TokenSyntax.public],
3838
identifier: type.listBuildable,
39-
inheritanceClause: listConformances.isEmpty ? nil : TypeInheritanceClause {
40-
for conformance in listConformances {
41-
InheritedType(typeName: conformance)
42-
}
43-
}
39+
inheritanceClause: createTypeInheritanceClause(conformances: listConformances)
4440
) {
4541
FunctionDecl(
4642
leadingTrivia: [
@@ -60,11 +56,7 @@ let buildableBaseProtocolsFile = SourceFile {
6056
ProtocolDecl(
6157
modifiers: [TokenSyntax.public],
6258
identifier: type.buildable,
63-
inheritanceClause: buildableConformances.isEmpty ? nil : TypeInheritanceClause {
64-
for conformance in buildableConformances {
65-
InheritedType(typeName: conformance)
66-
}
67-
}
59+
inheritanceClause: createTypeInheritanceClause(conformances: buildableConformances)
6860
) {
6961
FunctionDecl(
7062
leadingTrivia: [

Sources/SwiftSyntaxBuilderGeneration/Templates/ExpressibleAsProtocolsFile.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ let expressibleAsProtocolsFile = SourceFile {
4040
ProtocolDecl(
4141
modifiers: [TokenSyntax.public],
4242
identifier: type.expressibleAs,
43-
inheritanceClause: declaredConformances.isEmpty ? nil : TypeInheritanceClause {
44-
for conformance in declaredConformances {
45-
InheritedType(typeName: conformance.expressibleAs)
46-
}
47-
}
43+
inheritanceClause: createTypeInheritanceClause(conformances: declaredConformances.map(\.expressibleAs))
4844
) {
4945
FunctionDecl(
5046
identifier: .identifier("create\(type.buildableBaseName)"),

Sources/SwiftSyntaxBuilderGeneration/Templates/TokenSyntaxFile.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ let tokenSyntaxFile = SourceFile {
2525

2626
ExtensionDecl(
2727
extendedType: "TokenSyntax",
28-
inheritanceClause: TypeInheritanceClause {
29-
for conformance in conformances {
30-
InheritedType(typeName: conformance.expressibleAs)
31-
}
32-
}
28+
inheritanceClause: createTypeInheritanceClause(conformances: conformances.map(\.expressibleAs))
3329
) {
3430
for conformance in tokenType.elementInCollections {
3531
FunctionDecl(

0 commit comments

Comments
 (0)