@@ -27,10 +27,10 @@ public struct MockedMacro: PeerMacro {
27
27
)
28
28
return [ ]
29
29
}
30
-
30
+
31
31
// Check for access level argument (e.g., @Mocked(.public))
32
32
var accessLevel : String = " internal "
33
-
33
+
34
34
if case let . argumentList( argumentList) = node. arguments {
35
35
// Process each argument in the list
36
36
for argument in argumentList {
@@ -70,59 +70,56 @@ public struct MockedMacro: PeerMacro {
70
70
}
71
71
72
72
let mockClassName = " Mocked \( protocolDecl. name. text) "
73
-
73
+
74
74
let members = protocolDecl. memberBlock. members
75
-
75
+
76
76
// Variables
77
-
77
+
78
78
let variables : [ Variable ] = variableBuilder ( members: members)
79
-
79
+
80
80
let variablesDefinitions : String = variableDefinitions ( variables: variables, accessLevel: accessLevel)
81
81
let variablesInitDefinitions : String = variablesInitDefinitions ( variables: variables)
82
82
let variablesInitAssignments : String = variablesInitAssignments ( variables: variables)
83
-
83
+
84
84
// Functions
85
-
86
- let functions : [ Function ] = functionBuilder (
87
- protocolDecl: protocolDecl,
88
- members: members
89
- )
90
-
85
+
86
+ let functions : [ Function ] = functionBuilder ( protocolDecl: protocolDecl, members: members)
87
+
91
88
let functionVariableDefinitions : String = functionVariableDefinitions ( functions: functions)
92
89
let functionVariableInitDefinitions : String = functionVariableInitDefinitions ( functions: functions)
93
90
let functionVariableInitAssignments : String = functionVariableInitAssignments ( functions: functions)
94
91
let functionImplementations : String = functionImplementations ( functions: functions, accessLevel: accessLevel)
95
-
92
+
96
93
// Check if the protocol conforms to AnyObject
97
- let requiresClassConformance = protocolDecl. inheritanceClause? . inheritedTypes. contains ( where : {
98
- $0. type. description. trimmingCharacters ( in: . whitespacesAndNewlines) == " AnyObject "
99
- } ) ?? false
100
-
94
+ let requiresClassConformance = protocolDecl. inheritanceClause? . inheritedTypes. contains (
95
+ where : { $0. type. description. trimmingCharacters ( in: . whitespacesAndNewlines) == " AnyObject " }
96
+ ) ?? false
97
+
101
98
let objectType : String = requiresClassConformance ? " class " : " struct "
102
-
99
+
103
100
// Check for associated types in the protocol
104
101
var associatedTypes : [ String ] = [ ]
105
-
102
+
106
103
for member in protocolDecl. memberBlock. members {
107
104
if let associatedTypeDecl = member. decl. as ( AssociatedTypeDeclSyntax . self) {
108
105
let name = associatedTypeDecl. name. text
109
106
let constraint = associatedTypeDecl. inheritanceClause? . description. trimmingCharacters ( in: . whitespacesAndNewlines)
110
-
107
+
111
108
if let constraint {
112
109
associatedTypes. append ( " \( name) \( constraint) " )
113
110
} else {
114
111
associatedTypes. append ( name)
115
112
}
116
113
}
117
114
}
118
-
115
+
119
116
// Construct generic type parameters if there are associated types
120
117
let genericValues = if associatedTypes. isEmpty {
121
118
" "
122
119
} else {
123
120
" < " + associatedTypes. joined ( separator: " , " ) + " > "
124
121
}
125
-
122
+
126
123
return [
127
124
"""
128
125
/// Mocked version of \( raw: protocolDecl. name. text)
@@ -144,24 +141,23 @@ public struct MockedMacro: PeerMacro {
144
141
\( raw: variablesInitAssignments)
145
142
\( raw: functionVariableInitAssignments)
146
143
}
147
-
148
-
144
+
149
145
// MARK: - \( raw: mockClassName) Functions
150
146
151
147
\( raw: functionImplementations)
152
148
}
153
149
"""
154
150
]
155
151
}
156
-
152
+
157
153
// MARK: - Variable helpers
158
-
154
+
159
155
private static func variableBuilder( members: MemberBlockItemListSyntax ) -> [ Variable ] {
160
156
members. compactMap { member in
161
157
guard
162
158
let variable = member. decl. as ( VariableDeclSyntax . self)
163
159
else { return nil }
164
-
160
+
165
161
guard let binding = variable. bindings. first else {
166
162
return nil
167
163
}
@@ -170,18 +166,18 @@ public struct MockedMacro: PeerMacro {
170
166
else {
171
167
fatalError ( " \( String ( describing: binding. initializer? . syntaxNodeType) ) " )
172
168
}
173
-
169
+
174
170
let name = binding. pattern
175
171
let type = typeAnnotation. description. trimmingCharacters ( in: . whitespacesAndNewlines)
176
-
172
+
177
173
return Variable (
178
174
firstName: " \( name) " ,
179
175
secondName: nil ,
180
176
type: type
181
177
)
182
178
}
183
179
}
184
-
180
+
185
181
private static func variableDefinitions(
186
182
variables: [ Variable ] ,
187
183
accessLevel: String
@@ -198,50 +194,50 @@ public struct MockedMacro: PeerMacro {
198
194
}
199
195
. joined ( separator: " \n " )
200
196
}
201
-
197
+
202
198
private static func variablesInitDefinitions(
203
199
variables: [ Variable ]
204
200
) -> String {
205
201
variables
206
202
. map { " \( $0. declaration) , " }
207
203
. joined ( separator: " \n " )
208
204
}
209
-
205
+
210
206
private static func variablesInitAssignments(
211
207
variables: [ Variable ]
212
208
) -> String {
213
209
variables
214
210
. map { " self. \( $0. name) = \( $0. name) " }
215
211
. joined ( separator: " \n " )
216
212
}
217
-
213
+
218
214
// MARK: - Function helpers
219
-
215
+
220
216
private static func functionBuilder(
221
217
protocolDecl: ProtocolDeclSyntax ,
222
218
members: MemberBlockItemListSyntax
223
219
) -> [ Function ] {
224
220
let inheritsSendable = protocolDecl. inheritanceClause? . inheritedTypes. contains { inheritedType in
225
221
inheritedType. type. description. trimmingCharacters ( in: . whitespacesAndNewlines) == " Sendable "
226
222
} ?? false
227
-
223
+
228
224
return members. compactMap { member in
229
225
guard
230
226
let function = member. decl. as ( FunctionDeclSyntax . self)
231
227
else { return nil }
232
-
228
+
233
229
let name = function. name. text
234
230
var parameters : [ Variable ] = [ ]
235
231
let returnType = function. signature. returnClause? . type ?? " Void "
236
-
232
+
237
233
let isAsync = function. signature. effectSpecifiers? . asyncSpecifier != nil
238
234
let canThrow = function. signature. effectSpecifiers? . throwsClause? . throwsSpecifier != nil
239
-
235
+
240
236
for parameter in function. signature. parameterClause. parameters {
241
237
let parameterFirstName = parameter. firstName. text
242
238
let parameterSecondName = parameter. secondName? . text
243
239
let parameterType = parameter. type. description. trimmingCharacters ( in: . whitespacesAndNewlines)
244
-
240
+
245
241
parameters. append (
246
242
Variable (
247
243
firstName: parameterFirstName,
@@ -250,7 +246,7 @@ public struct MockedMacro: PeerMacro {
250
246
)
251
247
)
252
248
}
253
-
249
+
254
250
return Function (
255
251
name: " \( name) " ,
256
252
parameters: parameters,
@@ -261,31 +257,31 @@ public struct MockedMacro: PeerMacro {
261
257
)
262
258
}
263
259
}
264
-
260
+
265
261
private static func functionVariableDefinitions(
266
262
functions: [ Function ]
267
263
) -> String {
268
264
functions
269
265
. map { " private let \( $0. overrideClosure) " }
270
266
. joined ( separator: " \n " )
271
267
}
272
-
268
+
273
269
private static func functionVariableInitDefinitions(
274
270
functions: [ Function ]
275
271
) -> String {
276
272
functions
277
273
. map { " \( $0. closure) = nil " }
278
274
. joined ( separator: " , \n " )
279
275
}
280
-
276
+
281
277
private static func functionVariableInitAssignments(
282
278
functions: [ Function ]
283
279
) -> String {
284
280
functions
285
281
. map { " self. \( $0. overrideName) = \( $0. uniqueName) " }
286
282
. joined ( separator: " \n " )
287
283
}
288
-
284
+
289
285
private static func functionImplementations(
290
286
functions: [ Function ] ,
291
287
accessLevel: String
@@ -299,8 +295,7 @@ public struct MockedMacro: PeerMacro {
299
295
let parameterUsage : String = function. parameters
300
296
. map ( \. usageName)
301
297
. joined ( separator: " , " )
302
-
303
-
298
+
304
299
let effectSignature : String = if function. canThrow && function. isAsync {
305
300
" async throws "
306
301
} else if function. canThrow {
@@ -310,7 +305,7 @@ public struct MockedMacro: PeerMacro {
310
305
} else {
311
306
" "
312
307
}
313
-
308
+
314
309
let callSignature : String = if function. canThrow && function. isAsync {
315
310
" try await "
316
311
} else if function. canThrow {
@@ -320,15 +315,15 @@ public struct MockedMacro: PeerMacro {
320
315
} else {
321
316
" "
322
317
}
323
-
318
+
324
319
let accessLevel : String = if accessLevel. contains ( " public " ) {
325
320
" public "
326
321
} else if accessLevel. contains ( " package " ) {
327
322
" package "
328
323
} else {
329
324
" internal "
330
325
}
331
-
326
+
332
327
if parameters. isEmpty {
333
328
return """
334
329
\( accessLevel) func \( function. name) () \( effectSignature) -> \( function. returnType ?? " Void " ) {
0 commit comments