Skip to content

Commit 9ac898f

Browse files
authored
fix: ignore default immutable initialized and computed properties in decoding/encoding (#2)
* fix: ignore default initialized and computed properties in decoding/encoding * wip: allow mutable initialized declaration
1 parent 7086c41 commit 9ac898f

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

Sources/CodableMacroPlugin/CodableMacro.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
229251
extension TokenSyntax {
230252
/// Convert field token syntax

Tests/MetaCodableTests/Model.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public struct CodableData {
4848
let simulateWithoutArgumentWarning1: String?
4949
@CodablePath()
5050
let simulateWithoutArgumentWarning2: String?
51+
52+
var mutable: String = "some"
53+
var mutableOne = "any", mutableTwo, mutableThree: String
54+
var mutableOptional: String? = "some"
55+
56+
@CodablePath("customKey")
57+
var customMutableKeyValue: String
58+
59+
var computedInt: Int { 9 }
5160
}
5261

5362
struct PrimitiveCoder: ExternalHelperCoder {

0 commit comments

Comments
 (0)