@@ -33,8 +33,7 @@ public struct WriteRouteActionToSectionMacro: DeclarationMacro {
3333 ) throws -> [ DeclSyntax ] {
3434 let argumentList = node. arguments
3535 var key : String = " "
36- var functionBody : String = " "
37- var signature : String ?
36+ var actionClosure : ClosureExprSyntax ?
3837
3938 for argument in argumentList {
4039 switch argument. label? . text {
@@ -49,41 +48,38 @@ public struct WriteRouteActionToSectionMacro: DeclarationMacro {
4948 key = hostValue
5049 }
5150 case " action " :
52- if let closureExpr = argument. expression. as ( ClosureExprSyntax . self) {
53- functionBody = closureExpr. statements. trimmedDescription
54- if let sig = closureExpr. signature {
55- signature = sig. trimmedDescription
56- }
57- }
51+ actionClosure = argument. expression. as ( ClosureExprSyntax . self)
5852 default :
5953 break
6054 }
6155 }
6256
6357 // `#route(key: "haha") { params in ... }` puts the closure in trailingClosure.
64- if functionBody. isEmpty, let trailing = node. trailingClosure {
65- functionBody = trailing. statements. trimmedDescription
66- if let sig = trailing. signature {
67- signature = sig. trimmedDescription
68- }
58+ actionClosure = actionClosure ?? node. trailingClosure
59+
60+ // Interpolate as a syntax node (not `raw:`) so SwiftSyntax's Indenter
61+ // applies the insertion-site indent to every line of the closure.
62+ let closure : ExprSyntax
63+ if let actionClosure {
64+ closure = ExprSyntax ( actionClosure. trimmed)
65+ } else {
66+ closure = " { param in } "
6967 }
7068
7169 let isGlobal = context. lexicalContext. isEmpty
7270 let staticString = isGlobal ? " " : " static "
7371 let infoName = " \( context. makeUniqueName ( " rhea " ) ) "
7472 let hashLiteral = fnv1aHashLiteral ( key)
7573
76- let declarationString = """
77- @used
74+ let declaration : DeclSyntax = """
75+ @used
7876 @section( " __DATA,__rerouter_ac " )
79- \( staticString) let \( infoName) : RouteActionInfo = (
80- \( hashLiteral) ,
81- { \( signature ?? " param in " )
82- \( functionBody)
83- }
77+ \( raw: staticString) let \( raw: infoName) : RouteActionInfo = (
78+ \( raw: hashLiteral) ,
79+ \( closure)
8480 )
8581 """
86- return [ DeclSyntax ( stringLiteral : declarationString ) ]
82+ return [ declaration ]
8783 }
8884}
8985
0 commit comments