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