Skip to content

Commit 4f545b0

Browse files
authored
Merge pull request #10 from p-x9/feature/support-old-value
support using `oldValue` in `didSet`
2 parents a4e2bce + 7b6054e commit 4f545b0

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift

+17-14
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,6 @@ extension AssociatedObjectMacro: AccessorMacro {
9494
return []
9595
}
9696

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-
10497
return [
10598
AccessorDeclSyntax(
10699
accessorKind: .keyword(.get),
@@ -118,14 +111,21 @@ extension AssociatedObjectMacro: AccessorMacro {
118111
AccessorDeclSyntax(
119112
accessorKind: .keyword(.set),
120113
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")
122117
"""
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)
125120
}
126121
willSet(newValue)
127122
"""
128123
}
124+
125+
if binding.didSet != nil {
126+
"let oldValue = \(identifier)"
127+
}
128+
129129
"""
130130
objc_setAssociatedObject(
131131
self,
@@ -134,12 +134,15 @@ extension AssociatedObjectMacro: AccessorMacro {
134134
\(policy)
135135
)
136136
"""
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")
138141
"""
139-
let didSet = {
140-
\(didSet.statements.trimmed)
142+
let didSet: (\(type.trimmed)) -> Void = { [self] \(oldValue) in
143+
\(body.statements.trimmed)
141144
}
142-
didSet()
145+
didSet(oldValue)
143146
"""
144147
}
145148
})

Sources/AssociatedObjectPlugin/AssociatedObjectMacroDiagnostic.swift

-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public enum AssociatedObjectMacroDiagnostic {
1313
case requiresVariableDeclaration
1414
case multipleVariableDeclarationIsNotSupported
1515
case getterAndSetterShouldBeNil
16-
case accessorParameterNameMustBeNewValue
1716
case requiresInitialValue
1817
case specifyTypeExplicitly
1918
}
@@ -33,8 +32,6 @@ extension AssociatedObjectMacroDiagnostic: DiagnosticMessage {
3332
"""
3433
case .getterAndSetterShouldBeNil:
3534
return "getter and setter must not be implemented when using `@AssociatedObject`."
36-
case .accessorParameterNameMustBeNewValue:
37-
return "accessor parameter name must be `newValue` when using `@AssociatedObject`."
3835
case .requiresInitialValue:
3936
return "Initial values must be specified when using `@AssociatedObject`."
4037
case .specifyTypeExplicitly:

Sources/AssociatedObjectPlugin/Extension/PatternBindingSyntax+.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ extension PatternBindingSyntax {
116116
return nil
117117
}
118118
set {
119-
// NOTE: Be careful that willSet cannot be implemented without a setter.
119+
// NOTE: Be careful that didSet cannot be implemented without a setter.
120120
setNewAccessor(kind: .keyword(.willSet), newValue: newValue)
121121
}
122122
}

0 commit comments

Comments
 (0)