@@ -18,15 +18,28 @@ import _SwiftSyntaxMacros
18
18
import _SwiftSyntaxTestSupport
19
19
import XCTest
20
20
21
+ enum CustomError : Error , CustomStringConvertible {
22
+ case message( String )
23
+
24
+ var description : String {
25
+ switch self {
26
+ case . message( let text) :
27
+ return text
28
+ }
29
+ }
30
+ }
31
+
21
32
// MARK: Example macros
22
33
public struct StringifyMacro : ExpressionMacro {
23
- public static func expansion(
24
- of macro: MacroExpansionExprSyntax ,
25
- in context: any MacroExpansionContext
26
- ) -> ExprSyntax {
34
+ public static func expansion<
35
+ Node: FreestandingMacroExpansionSyntax ,
36
+ Context: MacroExpansionContext
37
+ > (
38
+ of macro: Node ,
39
+ in context: Context
40
+ ) throws -> ExprSyntax {
27
41
guard let argument = macro. argumentList. first? . expression else {
28
- // FIXME: Create a diagnostic for the missing argument?
29
- return ExprSyntax ( macro)
42
+ throw CustomError . message ( " missing argument " )
30
43
}
31
44
32
45
return " ( \( argument) , \( StringLiteralExprSyntax ( content: argument. description) ) ) "
@@ -50,9 +63,12 @@ private func replaceFirstLabel(
50
63
}
51
64
52
65
public struct ColorLiteralMacro : ExpressionMacro {
53
- public static func expansion(
54
- of macro: MacroExpansionExprSyntax ,
55
- in context: any MacroExpansionContext
66
+ public static func expansion<
67
+ Node: FreestandingMacroExpansionSyntax ,
68
+ Context: MacroExpansionContext
69
+ > (
70
+ of macro: Node ,
71
+ in context: Context
56
72
) -> ExprSyntax {
57
73
let argList = replaceFirstLabel (
58
74
of: macro. argumentList,
@@ -67,9 +83,12 @@ public struct ColorLiteralMacro: ExpressionMacro {
67
83
}
68
84
69
85
public struct FileLiteralMacro : ExpressionMacro {
70
- public static func expansion(
71
- of macro: MacroExpansionExprSyntax ,
72
- in context: any MacroExpansionContext
86
+ public static func expansion<
87
+ Node: FreestandingMacroExpansionSyntax ,
88
+ Context: MacroExpansionContext
89
+ > (
90
+ of macro: Node ,
91
+ in context: Context
73
92
) -> ExprSyntax {
74
93
let argList = replaceFirstLabel (
75
94
of: macro. argumentList,
@@ -84,9 +103,12 @@ public struct FileLiteralMacro: ExpressionMacro {
84
103
}
85
104
86
105
public struct ImageLiteralMacro : ExpressionMacro {
87
- public static func expansion(
88
- of macro: MacroExpansionExprSyntax ,
89
- in context: any MacroExpansionContext
106
+ public static func expansion<
107
+ Node: FreestandingMacroExpansionSyntax ,
108
+ Context: MacroExpansionContext
109
+ > (
110
+ of macro: Node ,
111
+ in context: Context
90
112
) -> ExprSyntax {
91
113
let argList = replaceFirstLabel (
92
114
of: macro. argumentList,
@@ -101,9 +123,12 @@ public struct ImageLiteralMacro: ExpressionMacro {
101
123
}
102
124
103
125
public struct ColumnMacro : ExpressionMacro {
104
- public static func expansion(
105
- of macro: MacroExpansionExprSyntax ,
106
- in context: any MacroExpansionContext
126
+ public static func expansion<
127
+ Node: FreestandingMacroExpansionSyntax ,
128
+ Context: MacroExpansionContext
129
+ > (
130
+ of macro: Node ,
131
+ in context: Context
107
132
) throws -> ExprSyntax {
108
133
guard let sourceLoc = context. location ( of: macro) ,
109
134
let column = sourceLoc. column else {
@@ -120,9 +145,12 @@ public struct ColumnMacro: ExpressionMacro {
120
145
}
121
146
122
147
public struct FileIDMacro : ExpressionMacro {
123
- public static func expansion(
124
- of macro: MacroExpansionExprSyntax ,
125
- in context: any MacroExpansionContext
148
+ public static func expansion<
149
+ Node: FreestandingMacroExpansionSyntax ,
150
+ Context: MacroExpansionContext
151
+ > (
152
+ of macro: Node ,
153
+ in context: Context
126
154
) throws -> ExprSyntax {
127
155
guard let sourceLoc = context. location ( of: macro) ,
128
156
let fileID = sourceLoc. file else {
@@ -141,9 +169,12 @@ public struct FileIDMacro: ExpressionMacro {
141
169
/// Macro whose only purpose is to ensure that we cannot see "out" of the
142
170
/// macro expansion syntax node we were given.
143
171
struct CheckContextIndependenceMacro : ExpressionMacro {
144
- static func expansion(
145
- of macro: MacroExpansionExprSyntax ,
146
- in context: any MacroExpansionContext
172
+ static func expansion<
173
+ Node: FreestandingMacroExpansionSyntax ,
174
+ Context: MacroExpansionContext
175
+ > (
176
+ of macro: Node ,
177
+ in context: Context
147
178
) -> ExprSyntax {
148
179
149
180
// Should not have a parent.
@@ -152,18 +183,7 @@ struct CheckContextIndependenceMacro: ExpressionMacro {
152
183
// Absolute starting position should be zero.
153
184
XCTAssertEqual ( macro. position. utf8Offset, 0 )
154
185
155
- return ExprSyntax ( macro)
156
- }
157
- }
158
-
159
- enum CustomError : Error , CustomStringConvertible {
160
- case message( String )
161
-
162
- var description : String {
163
- switch self {
164
- case . message( let text) :
165
- return text
166
- }
186
+ return " () "
167
187
}
168
188
}
169
189
@@ -701,7 +721,7 @@ final class MacroSystemTests: XCTestCase {
701
721
let b = #checkContext
702
722
""" ,
703
723
"""
704
- let b = #checkContext
724
+ let b = ()
705
725
"""
706
726
)
707
727
}
0 commit comments