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?
In the same way that you passed a result builder closure to
EnumDeclSyntaxandEnumCaseDeclSyntax, you can do that toEnumCaseParameterListSyntaxtoo.Many of the AST nodes conform to
ExpressibleByStringInterpolation, so you can further simplify this toIn fact, given a list of enum case parameters, you can generate the whole enum declaration using an interpolated string literal: