I'm fiddling around with the new Swift 5.9 Macros.
I have the following macro:
Given the following enum
@associatedValues
enum Fruit {
case banana(Banana)
case grape(Grape)
}
I want
@associatedValues
enum Fruit {
case banana(Banana)
case grape(Grape)
var bananaValue: Banana? { if ... return ... } }
var grapeValue: Grape? { if ... return ... } }
}
My implementation is like so:
public static func expansion<Declaration, Context>(
of node: SwiftSyntax.AttributeSyntax,
providingMembersOf declaration: Declaration,
in context: Context
) throws -> [SwiftSyntax.DeclSyntax]
where Declaration: SwiftSyntax.DeclGroupSyntax,
Context: SwiftSyntaxMacros.MacroExpansionContext
{
guard let enumDeclaration = declaration.as(EnumDeclSyntax.self) else {
throw AssociatedValuesMacroError.onlyApplicableToEnum
}
return enumDeclaration.memberBlock.members
.compactMap { $0.decl.as(EnumCaseDeclSyntax.self) }
.map { (_case: EnumCaseDeclSyntax) in
let _element: EnumCaseElementListSyntax.Element = _case.elements.first!
let caseName = _element.identifier
let typeName = _element.associatedValue!.parameterList.first!.description
return
"""
var \(raw: caseName)Value: \(raw: typeName)? {
if case let .\(raw: caseName)(value) = self {
return value
} else {
return nil
}
}
"""
}
}
Now, the autocompletion detects the new properties, but the compiler tells me:
Declaration name 'bananaValue' is not covered by macro 'associatedValues'
Declaration name 'grapeValue' is not covered by macro 'associatedValues'
The tests run and validate the output but not when compiling.
What am I missing? Thank you
We need to add names: arbitrary
to the macro role.
@attached(member, names: arbitrary)
public macro AssociatedValues() = #externalMacro(module: "MyMacrosModule", type: "AssociatedValuesMacro")
Note: In my case I needed arbitrary because the names are dynamic. But if you do know what attributes you're adding, you should use names: myCustomName
instead.
Also, though not an answer to the specific question, but make sure you add your macro type to the list of providing Macros in CompilerPlugin
.
#if canImport(SwiftCompilerPlugin)
import SwiftCompilerPlugin
import SwiftSyntaxMacros
@main
struct MacrosPlugin: CompilerPlugin {
let providingMacros: [Macro.Type] = [
AssociatedValueMacro.self
]
}