Skip to content

Commit

Permalink
fix: ignore default immutable initialized and computed properties in …
Browse files Browse the repository at this point in the history
…decoding/encoding (#2)

* fix: ignore default initialized and computed properties in decoding/encoding

* wip: allow mutable initialized declaration
  • Loading branch information
soumyamahunt authored Jun 30, 2023
1 parent 7086c41 commit 9ac898f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
32 changes: 27 additions & 5 deletions Sources/CodableMacroPlugin/CodableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct CodableMacro: ConformanceMacro, MemberMacro {
if decl.bindings.count > 1 {
var fields: [TokenSyntax] = []
var type: TypeSyntax!
for binding in decl.bindings
for binding in decl.initializableBindings
where binding.pattern.is(IdentifierPatternSyntax.self) {
fields.append(
binding.pattern
Expand All @@ -121,9 +121,9 @@ struct CodableMacro: ConformanceMacro, MemberMacro {

// is a single property declaration
guard
let p = decl.bindings.first,
let type = p.typeAnnotation?.type,
let field = p.pattern
let binding = decl.initializableBindings.first,
let type = binding.typeAnnotation?.type,
let field = binding.pattern
.as(IdentifierPatternSyntax.self)?.identifier
else { return }

Expand Down Expand Up @@ -222,9 +222,31 @@ fileprivate extension VariableDeclSyntax {

return (attr: macro, default: def, helper: hlpr)
}

/// Filters variables in variable declaration that can be initialized
/// first in parent type's Initializer.
///
/// Filters variables that are not computed properties,
/// and if immutable not initialized already.
var initializableBindings: [PatternBindingSyntax] {
return self.bindings.filter { binding in
switch binding.accessor {
case .none:
self.bindingKeyword.tokenKind == .keyword(.var)
|| binding.initializer == nil
// TODO: Reevaluate when init accessor is introduced
// https://github.com/apple/swift-evolution/blob/main/proposals/0400-init-accessors.md
// case .accessors(let block) where block.accessors
// .contains { $0.accessorKind == .keyword(Keyword.`init`)}:
// return true
default:
false
}
}
}
}

/// An extension that coverts field token syntax
/// An extension that converts field token syntax
/// to equivalent key token.
extension TokenSyntax {
/// Convert field token syntax
Expand Down
9 changes: 9 additions & 0 deletions Tests/MetaCodableTests/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public struct CodableData {
let simulateWithoutArgumentWarning1: String?
@CodablePath()
let simulateWithoutArgumentWarning2: String?

var mutable: String = "some"
var mutableOne = "any", mutableTwo, mutableThree: String
var mutableOptional: String? = "some"

@CodablePath("customKey")
var customMutableKeyValue: String

var computedInt: Int { 9 }
}

struct PrimitiveCoder: ExternalHelperCoder {
Expand Down

0 comments on commit 9ac898f

Please sign in to comment.