@@ -94,13 +94,6 @@ extension AssociatedObjectMacro: AccessorMacro {
94
94
return [ ]
95
95
}
96
96
97
- if let willSet = binding. willSet,
98
- let parameter = willSet. parameter,
99
- parameter. name. trimmed. text != " newValue " {
100
- context. diagnose ( AssociatedObjectMacroDiagnostic . accessorParameterNameMustBeNewValue. diagnose ( at: parameter) )
101
- return [ ]
102
- }
103
-
104
97
return [
105
98
AccessorDeclSyntax (
106
99
accessorKind: . keyword( . get) ,
@@ -118,14 +111,21 @@ extension AssociatedObjectMacro: AccessorMacro {
118
111
AccessorDeclSyntax (
119
112
accessorKind: . keyword( . set) ,
120
113
body: CodeBlockSyntax {
121
- if let willSet = binding. willSet? . body {
114
+ if let willSet = binding. willSet,
115
+ let body = willSet. body {
116
+ let newValue = willSet. parameter? . name. trimmed ?? . identifier( " newValue " )
122
117
"""
123
- let willSet: ( \( type. trimmed) ) -> Void = { newValue in
124
- \( willSet . statements. trimmed)
118
+ let willSet: ( \( type. trimmed) ) -> Void = { [self] \( newValue) in
119
+ \( body . statements. trimmed)
125
120
}
126
121
willSet(newValue)
127
122
"""
128
123
}
124
+
125
+ if binding. didSet != nil {
126
+ " let oldValue = \( identifier) "
127
+ }
128
+
129
129
"""
130
130
objc_setAssociatedObject(
131
131
self,
@@ -134,12 +134,15 @@ extension AssociatedObjectMacro: AccessorMacro {
134
134
\( policy)
135
135
)
136
136
"""
137
- if let didSet = binding. didSet? . body {
137
+
138
+ if let didSet = binding. didSet,
139
+ let body = didSet. body {
140
+ let oldValue = didSet. parameter? . name. trimmed ?? . identifier( " oldValue " )
138
141
"""
139
- let didSet = {
140
- \( didSet . statements. trimmed)
142
+ let didSet: ( \( type . trimmed ) ) -> Void = { [self] \( oldValue ) in
143
+ \( body . statements. trimmed)
141
144
}
142
- didSet()
145
+ didSet(oldValue )
143
146
"""
144
147
}
145
148
} )
0 commit comments