@@ -97,7 +97,7 @@ struct CodableMacro: ConformanceMacro, MemberMacro {
9797 if decl. bindings. count > 1 {
9898 var fields : [ TokenSyntax ] = [ ]
9999 var type : TypeSyntax !
100- for binding in decl. bindings
100+ for binding in decl. initializableBindings
101101 where binding. pattern. is ( IdentifierPatternSyntax . self) {
102102 fields. append (
103103 binding. pattern
@@ -121,9 +121,9 @@ struct CodableMacro: ConformanceMacro, MemberMacro {
121121
122122 // is a single property declaration
123123 guard
124- let p = decl. bindings . first,
125- let type = p . typeAnnotation? . type,
126- let field = p . pattern
124+ let binding = decl. initializableBindings . first,
125+ let type = binding . typeAnnotation? . type,
126+ let field = binding . pattern
127127 . as ( IdentifierPatternSyntax . self) ? . identifier
128128 else { return }
129129
@@ -222,9 +222,31 @@ fileprivate extension VariableDeclSyntax {
222222
223223 return ( attr: macro, default: def, helper: hlpr)
224224 }
225+
226+ /// Filters variables in variable declaration that can be initialized
227+ /// first in parent type's Initializer.
228+ ///
229+ /// Filters variables that are not computed properties,
230+ /// and if immutable not initialized already.
231+ var initializableBindings : [ PatternBindingSyntax ] {
232+ return self . bindings. filter { binding in
233+ switch binding. accessor {
234+ case . none:
235+ self . bindingKeyword. tokenKind == . keyword( . var)
236+ || binding. initializer == nil
237+ // TODO: Reevaluate when init accessor is introduced
238+ // https://github.com/apple/swift-evolution/blob/main/proposals/0400-init-accessors.md
239+ // case .accessors(let block) where block.accessors
240+ // .contains { $0.accessorKind == .keyword(Keyword.`init`)}:
241+ // return true
242+ default :
243+ false
244+ }
245+ }
246+ }
225247}
226248
227- /// An extension that coverts field token syntax
249+ /// An extension that converts field token syntax
228250/// to equivalent key token.
229251extension TokenSyntax {
230252 /// Convert field token syntax
0 commit comments