File tree 4 files changed +62
-3
lines changed
SwiftSyntaxMacroExpansion
Tests/SwiftSyntaxMacrosTest
4 files changed +62
-3
lines changed Original file line number Diff line number Diff line change @@ -46,8 +46,10 @@ public func expandFreestandingMacro(
46
46
var rewritten = try declMacroDef. expansion ( of: node, in: context)
47
47
// Copy attributes and modifiers to the generated decls.
48
48
if let expansionDecl = node. as ( MacroExpansionDeclSyntax . self) {
49
+ let attributes = declMacroDef. propagateFreestandingMacroAttributes ? expansionDecl. attributes : nil
50
+ let modifiers = declMacroDef. propagateFreestandingMacroModifiers ? expansionDecl. modifiers : nil
49
51
rewritten = rewritten. map {
50
- $0. applying ( attributes: expansionDecl . attributes, modifiers: expansionDecl . modifiers)
52
+ $0. applying ( attributes: attributes, modifiers: modifiers)
51
53
}
52
54
}
53
55
expandedSyntax = Syntax (
Original file line number Diff line number Diff line change @@ -20,4 +20,16 @@ public protocol DeclarationMacro: FreestandingMacro {
20
20
of node: Node ,
21
21
in context: Context
22
22
) throws -> [ DeclSyntax ]
23
+
24
+ /// Whether to copy attributes on the expansion syntax to expanded declarations,
25
+ /// 'true' by default.
26
+ static var propagateFreestandingMacroAttributes : Bool { get }
27
+ /// Whether to copy modifiers on the expansion syntax to expanded declarations,
28
+ /// 'true' by default.
29
+ static var propagateFreestandingMacroModifiers : Bool { get }
30
+ }
31
+
32
+ public extension DeclarationMacro {
33
+ static var propagateFreestandingMacroAttributes : Bool { true }
34
+ static var propagateFreestandingMacroModifiers : Bool { true }
23
35
}
Original file line number Diff line number Diff line change @@ -141,8 +141,10 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
141
141
in: context
142
142
)
143
143
if let declExpansion = expansion. as ( MacroExpansionDeclSyntax . self) {
144
+ let attributes = macro. propagateFreestandingMacroAttributes ? declExpansion. attributes : nil
145
+ let modifiers = macro. propagateFreestandingMacroModifiers ? declExpansion. modifiers : nil
144
146
expandedItemList = expandedItemList. map {
145
- $0. applying ( attributes: declExpansion . attributes, modifiers: declExpansion . modifiers)
147
+ $0. applying ( attributes: attributes, modifiers: modifiers)
146
148
}
147
149
}
148
150
newItems. append (
@@ -198,8 +200,10 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
198
200
of: declExpansion,
199
201
in: context
200
202
)
203
+ let attributes = freestandingMacro. propagateFreestandingMacroAttributes ? declExpansion. attributes : nil
204
+ let modifiers = freestandingMacro. propagateFreestandingMacroModifiers ? declExpansion. modifiers : nil
201
205
expandedList = expandedList. map {
202
- $0. applying ( attributes: declExpansion . attributes, modifiers: declExpansion . modifiers)
206
+ $0. applying ( attributes: attributes, modifiers: modifiers)
203
207
}
204
208
205
209
newItems. append (
Original file line number Diff line number Diff line change @@ -672,6 +672,30 @@ public struct DeclsFromStringsMacro: DeclarationMacro {
672
672
}
673
673
}
674
674
675
+ public struct DeclsFromStringsMacroNoAttrs : DeclarationMacro {
676
+ public static var propagateFreestandingMacroAttributes : Bool { false }
677
+ public static var propagateFreestandingMacroModifiers : Bool { false }
678
+
679
+ public static func expansion(
680
+ of node: some FreestandingMacroExpansionSyntax ,
681
+ in context: some MacroExpansionContext
682
+ ) throws -> [ DeclSyntax ] {
683
+ var strings : [ String ] = [ ]
684
+ for arg in node. argumentList {
685
+ guard
686
+ let value = arg. expression. as ( StringLiteralExprSyntax . self) ? . representedLiteralValue
687
+ else {
688
+ continue
689
+ }
690
+ strings. append ( value)
691
+ }
692
+
693
+ return strings. map {
694
+ " \( raw: $0) "
695
+ }
696
+ }
697
+ }
698
+
675
699
// MARK: Tests
676
700
677
701
/// The set of test macros we use here.
@@ -1114,5 +1138,22 @@ final class MacroSystemTests: XCTestCase {
1114
1138
macros: [ " decls " : DeclsFromStringsMacro . self] ,
1115
1139
indentationWidth: indentationWidth
1116
1140
)
1141
+
1142
+ assertMacroExpansion (
1143
+ #"""
1144
+ @attribute
1145
+ @otherAttribute(x: 1)
1146
+ public #decls("@moreAttibute var global = 42",
1147
+ "private func foo() {}")
1148
+ """# ,
1149
+ expandedSource: #"""
1150
+ @moreAttibute var global = 42
1151
+ private func foo() {
1152
+ }
1153
+ """# ,
1154
+ macros: [ " decls " : DeclsFromStringsMacroNoAttrs . self] ,
1155
+ indentationWidth: indentationWidth
1156
+ )
1157
+
1117
1158
}
1118
1159
}
You can’t perform that action at this time.
0 commit comments