File tree 2 files changed +27
-3
lines changed
Sources/AssociatedObjectPlugin
2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -82,8 +82,9 @@ extension AssociatedObjectMacro: AccessorMacro {
82
82
return [ ]
83
83
}
84
84
85
- // Initial value required
86
- guard let defaultValue = binding. initializer? . value else {
85
+ let defaultValue = binding. initializer? . value
86
+ // Initial value required if type is optional
87
+ if defaultValue == nil && !type. isOptional {
87
88
context. diagnose ( AssociatedObjectMacroDiagnostic . requiresInitialValue. diagnose ( at: declaration) )
88
89
return [ ]
89
90
}
@@ -103,7 +104,7 @@ extension AssociatedObjectMacro: AccessorMacro {
103
104
self,
104
105
&Self.__associated_ \( identifier) Key
105
106
) as? \( type)
106
- ?? \( defaultValue)
107
+ ?? \( defaultValue ?? " nil " )
107
108
"""
108
109
}
109
110
) ,
Original file line number Diff line number Diff line change
1
+ //
2
+ // TypeSyntax+.swift
3
+ //
4
+ //
5
+ // Created by p-x9 on 2023/06/27.
6
+ //
7
+ //
8
+
9
+ import SwiftSyntax
10
+ import SwiftSyntaxBuilder
11
+
12
+ extension TypeSyntax {
13
+ var isOptional : Bool {
14
+ if let optionalType = self . as ( OptionalTypeSyntax . self) {
15
+ return true
16
+ }
17
+ if let simpleType = self . as ( SimpleTypeIdentifierSyntax . self) ,
18
+ simpleType. name. trimmed. text == " Optional " {
19
+ return true
20
+ }
21
+ return false
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments