@@ -208,7 +208,9 @@ struct DefineBitwidthNumberedStructsMacro: FreestandingDeclarationMacro {
208
208
}
209
209
}
210
210
211
- public struct PropertyWrapper : AccessorDeclarationMacro {
211
+ public struct PropertyWrapper { }
212
+
213
+ extension PropertyWrapper : AccessorDeclarationMacro {
212
214
public static func expansion(
213
215
of node: CustomAttributeSyntax ,
214
216
attachedTo declaration: DeclSyntax ,
@@ -217,18 +219,10 @@ public struct PropertyWrapper: AccessorDeclarationMacro {
217
219
guard let varDecl = declaration. as ( VariableDeclSyntax . self) ,
218
220
let binding = varDecl. bindings. first,
219
221
let identifier = binding. pattern. as ( IdentifierPatternSyntax . self) ? . identifier,
220
- let type = binding. typeAnnotation? . type,
221
222
binding. accessor == nil else {
222
223
return [ ]
223
224
}
224
225
225
- guard let wrapperTypeNameExpr = node. argumentList? . first? . expression,
226
- let stringLiteral = wrapperTypeNameExpr. as ( StringLiteralExprSyntax . self) ,
227
- stringLiteral. segments. count == 1 ,
228
- case let . stringSegment( wrapperTypeNameSegment) ? = stringLiteral. segments. first else {
229
- return [ ]
230
- }
231
-
232
226
return [
233
227
"""
234
228
@@ -246,6 +240,40 @@ public struct PropertyWrapper: AccessorDeclarationMacro {
246
240
}
247
241
}
248
242
243
+ extension PropertyWrapper : PeerDeclarationMacro {
244
+ public static func expansion(
245
+ of node: CustomAttributeSyntax ,
246
+ attachedTo declaration: DeclSyntax ,
247
+ in context: inout MacroExpansionContext
248
+ ) throws -> [ SwiftSyntax . DeclSyntax ] {
249
+ guard let varDecl = declaration. as ( VariableDeclSyntax . self) ,
250
+ let binding = varDecl. bindings. first,
251
+ let identifier = binding. pattern. as ( IdentifierPatternSyntax . self) ? . identifier,
252
+ let type = binding. typeAnnotation? . type,
253
+ binding. accessor == nil else {
254
+ return [ ]
255
+ }
256
+
257
+ guard let wrapperTypeNameExpr = node. argumentList? . first? . expression,
258
+ let stringLiteral = wrapperTypeNameExpr. as ( StringLiteralExprSyntax . self) ,
259
+ stringLiteral. segments. count == 1 ,
260
+ case let . stringSegment( wrapperTypeNameSegment) ? = stringLiteral. segments. first else {
261
+ return [ ]
262
+ }
263
+
264
+ let storageType : TypeSyntax = " \( wrapperTypeNameSegment. content) < \( type) > "
265
+ let storageName = " _ \( identifier) "
266
+
267
+ return [
268
+ """
269
+
270
+ private var \( raw: storageName) : \( storageType)
271
+
272
+ """
273
+ ]
274
+ }
275
+ }
276
+
249
277
public struct AddCompletionHandler : PeerDeclarationMacro {
250
278
public static func expansion(
251
279
of node: CustomAttributeSyntax ,
@@ -571,6 +599,7 @@ final class MacroSystemTests: XCTestCase {
571
599
_x.wrappedValue = newValue
572
600
}
573
601
}
602
+ private var _x: MyWrapperType<Int>
574
603
""" )
575
604
}
576
605
0 commit comments