Im trying to make macro which will expand to two expressions. One of them is OSLog log method, second is my debugging func.
It looks like this (Simplified):
public struct LogErrorMacro: ExpressionMacro {
public static func expansion(of node: some SwiftSyntax.FreestandingMacroExpansionSyntax, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> SwiftSyntax.ExprSyntax {
guard let message = node.argumentList.first?.expression.as(StringLiteralExprSyntax.self) else {
fatalError("Message is required!")
}
return try ExprSyntax(validating: ExprSyntax(
#"""
{
Logger().error(\#(raw: message));
AppDebug.error(\#(raw: message))
}()
"""#
))
}
}
Since ExpressionMacro has no option, to return array i had to use closure.
While above macro works correctly, NSLog called in closure is behaving poorly (Jumps to snippet of code inside closure, not file that is included in), and i cannot use xcode "Jump to source" function.
Any idea how i could improve this macro to restore this simple, but very usefull functionality?