-
Notifications
You must be signed in to change notification settings - Fork 451
Description
Description
I've been playing around with macros and I found myself in a situation where I got attached accessor macro with 2 parameters - url and method. Both are strings.
It looks like this
public struct MyMacroMacro: AccessorMacro {
public static func expansion<Context, Declaration>(
of node: AttributeSyntax,
providingAccessorsOf declaration: Declaration,
in context: Context
) throws -> [AccessorDeclSyntax] where Context : MacroExpansionContext, Declaration : DeclSyntaxProtocol {
guard let expressions = node.argument?.as(TupleExprElementListSyntax.self) else {
//TODO throw error
return []
}
let segments = expressions.compactMap { $0.expression.as(StringLiteralExprSyntax.self)?.segments }
let params = segments.compactMap { $0.trimmedDescription }
guard params.count == 2 else {
//TODO throw error
return []
}
return [
"""
.init(url: url + "\(raw: params[0])", method: "\(raw: params[1])")
"""
]
}
}
Now, when I call it like:
struct MyStruct { let url = "https://apple.com" @MyMacro(url: "/test", method: "some") var myVar: MyCustomType
it produces something like this:
struct MyStruct { let url = "https://apple.com" var myVar: MyCustomType { .init(url: url + " /test", method: " some") }
I have even tried hardcoding the return of AccessorMacro to produce .init(url: url + "/test", method: "get") but it still added those white spaces.
Steps to reproduce
Create accessor macro that'll return something - let it be dictionary or an initializer - with some string inside.
Expected behavior
I want to have my code generated without unwanted white spaces.
Environment
Swift 5.9
Xcode 15.0