Skip to content

Instantly share code, notes, and snippets.

@WhiteHyun
Created August 10, 2024 23:07
Show Gist options
  • Select an option

  • Save WhiteHyun/6f0ba2c8340b288750674a706f702abc to your computer and use it in GitHub Desktop.

Select an option

Save WhiteHyun/6f0ba2c8340b288750674a706f702abc to your computer and use it in GitHub Desktop.
매크로를 두 번 감싸는 걸 방지하는 코드
// MARK: - PropertyMacro
public struct PropertyMacro: AccessorMacro {
public static func expansion(
of node: AttributeSyntax,
providingAccessorsOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [AccessorDeclSyntax] {
guard let varDecl = declaration.as(VariableDeclSyntax.self) else {
return []
}
// Check if there's more than one @Property attribute
let propertyAttributes = varDecl.attributes.filter {
$0.as(AttributeSyntax.self)?.attributeName.description == "Property"
}
if propertyAttributes.count > 1 {
context.diagnose(
.init(
node: Syntax(node),
message: MacroExpansionDiagnostic.multiplePropertyApplication
)
)
}
return []
}
}
enum MacroExpansionDiagnostic: String, DiagnosticMessage {
case multiplePropertyApplication
var severity: DiagnosticSeverity {
switch self {
case .multiplePropertyApplication:
return .error
}
}
var message: String {
switch self {
case .multiplePropertyApplication:
return "@Property can only be applied once per property"
}
}
var diagnosticID: MessageID {
switch self {
case .multiplePropertyApplication:
return MessageID(domain: "DomainMacro", id: rawValue)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment