Skip to content

Commit

Permalink
fix: added robust grouped variable declaration type detection (#3)
Browse files Browse the repository at this point in the history
* fix: added robust grouped variable declaration type detection

* wip: added scenario in test model
  • Loading branch information
soumyamahunt authored Jun 30, 2023
1 parent 9ac898f commit 0cc623f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
31 changes: 18 additions & 13 deletions Sources/CodableMacroPlugin/CodableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,30 @@ struct CodableMacro: ConformanceMacro, MemberMacro {
else { return }

// is a grouped property declaration
if decl.bindings.count > 1 {
var fields: [TokenSyntax] = []
var type: TypeSyntax!
if decl.initializableBindings.count > 1 {
var variables: [(TokenSyntax, TypeSyntax?)] = []
for binding in decl.initializableBindings
where binding.pattern.is(IdentifierPatternSyntax.self) {
fields.append(
binding.pattern
.as(IdentifierPatternSyntax.self)!.identifier
)
variables.append((
binding.pattern.as(IdentifierPatternSyntax.self)!
.identifier,
binding.typeAnnotation?.type
))
}

var datas: [Registrar.Node.Data] = []
datas.reserveCapacity(variables.count)

guard let fType = binding.typeAnnotation?.type
else { continue }
type = fType
var latestType: TypeSyntax!
for (field, type) in variables.reversed() {
if let type { latestType = type }
datas.append(.init(field: field, type: latestType))
}

for field in fields where type != nil {
for data in datas.reversed() {
registrar.add(
data: .init(field: field, type: type),
keyPath: [field.asKey],
data: data,
keyPath: [data.field.asKey],
context: context
)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/MetaCodableTests/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import MetaCodable

@Codable
public struct CodableData {
let groupedOne, groupedTwo, groupedThree: String
let groupedOne, groupedTwo: String, groupedThree: Int

let optional: String?
let genericOptional: Optional<String>
Expand Down Expand Up @@ -50,7 +50,7 @@ public struct CodableData {
let simulateWithoutArgumentWarning2: String?

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

@CodablePath("customKey")
Expand Down

0 comments on commit 0cc623f

Please sign in to comment.