mishutto
(Misha)
1
I'm delving into the macros topic to enhance the developer experience with my macros.
I envision a future where the Xcode Fix It button empowers developers to make source code changes effortlessly. For instance, it could intelligently switch an enum to a struct if my attached macro is designed exclusively for structs.
Now, my challenge lies in navigating the syntax tree to identify the appropriate struct declaration syntax for the FixIt.change property:
fixIt: FixIt(
message: CustomError.invalidDeclarationType,
changes: [
.replace(
oldNode: Syntax(declaration._syntaxNode),
newNode: Syntax(???)
)
]
)
How can I locate this syntax?
Additionally, I attempted to debug the macro using tests to explore the tree and find what I'm looking for, but encountered an issue — my tests failed without triggering any breakpoints:
let testMacros: [String: Macro.Type] = [
"CustomMacro": CustomMacro.self
]
final class StructTests: XCTestCase {
func testEnumToStruct() {
assertMacroExpansion(
"""
@CustomInit
enum Caller {
}
""",
expandedSource:
"""
@CustomInit
struct Caller {
}
""",
macros: testMacros
)
}
}
What could be causing this discrepancy, and how can I resolve it?