When trying to generate an enum with associated values, the generated enum is missing comma separating it's values.
This is the my expansion function (I'm using a PeermMacro)
public enum MyMacro: PeerMacro {
public static func expansion(
of _: AttributeSyntax,
providingPeersOf _: some DeclSyntaxProtocol,
in _: some MacroExpansionContext
) throws -> [DeclSyntax] {
[
DeclSyntax(
EnumDeclSyntax(name: "MyEnum") {
EnumCaseDeclSyntax {
EnumCaseElementSyntax(
name: "myCase",
parameterClause: EnumCaseParameterClauseSyntax(
parameters: EnumCaseParameterListSyntax(
[
EnumCaseParameterSyntax(type: TypeSyntax("Int")),
EnumCaseParameterSyntax(type: TypeSyntax("String"))
]
)
)
)
}
}
)
]
}
}
The code above generates the following code, which doesn't compile because there's a comma missing between the associated values:
enum MyEnum {
case myCase(Int String)
}
How can I update my expansion code so that the generated code contains comma between my associated values?
Matejkob
(Mateusz Bąk)
2
You just need to add a trailingComma argument to EnumCaseParameterSyntax initializer like so:
- EnumCaseParameterSyntax(type: TypeSyntax("Int"))
+ EnumCaseParameterSyntax(type: TypeSyntax("Int"), trailingComma: .commaToken())
ahoppen
(Alex Hoppen)
3
Alternatively you can import SwiftSyntaxBuilder and use the result builder initializer that automatically adds commas.
EnumCaseParameterListSyntax {
EnumCaseParameterSyntax(type: TypeSyntax("Int")),
EnumCaseParameterSyntax(type: TypeSyntax("String"))
}