Hello everyone, I'm developing a macro which needs to identify if a protocol is already implemented on a type. The macro looks like this:
public static func expansion(
of _: AttributeSyntax,
attachedTo _: some DeclGroupSyntax,
providingExtensionsOf type: some TypeSyntaxProtocol,
conformingTo protocols: [TypeSyntax],
in _: some MacroExpansionContext
) throws -> [ExtensionDeclSyntax] {
var newFunctions: [ExtensionDeclSyntax] = []
let identifiers = protocols.compactMap { $0.as(IdentifierTypeSyntax.self) }
if identifiers.contains(where: { $0.description == "Hashable" }) {
try newFunctions.append(
ExtensionDeclSyntax("""
extension \(type.trimmed): Hashable {
}
"""
)
)
}
return newFunctions
}
my question is whether my approach of mapping to IdentifierTypeSyntax
and then using description
is a good one for identifying whether Hashable
is in the protocols list? Or is there a simpler way to do this?