Skip to content

Commit

Permalink
Backtrack, retain all CodingKey enum cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch committed Dec 31, 2023
1 parent 3dc7d24 commit 2fe1263
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

- Add experimental unused import analysis.
- The option `--external-encodable-protocols` is deprecated, use `--external-codable-protocols` instead.
- CodingKey enum case are now identified as unused if the corresponding property is also unused in the Codable type.

##### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,21 @@ final class CodingKeyEnumReferenceBuilder: SourceGraphMutator {
return [.protocol, .typealias].contains($0.kind) && codableTypes.contains(name)
}

guard isCodingKey, isParentCodable else { continue }

// Build a reference from the Codable type to the CodingKey enum.
for usr in enumDeclaration.usrs {
let newReference = Reference(kind: .enum, usr: usr, location: enumDeclaration.location)
newReference.name = enumDeclaration.name
newReference.parent = parent
graph.add(newReference, from: parent)
guard isCodingKey else { continue }

// Retain each enum element.
for elem in enumDeclaration.declarations {
guard elem.kind == .enumelement else { continue }
graph.markRetained(elem)
}

// For each property in the Codable type, build a reference to its corresponding
// CodingKey enum element.
for decl in parent.declarations {
guard decl.kind == .varInstance,
let enumCase = enumDeclaration.declarations.first(where: { $0.kind == .enumelement && $0.name == decl.name })
else { continue }

for usr in enumCase.usrs {
let newReference = Reference(kind: .enumelement, usr: usr, location: decl.location)
newReference.name = enumCase.name
newReference.parent = decl
graph.add(newReference, from: decl)
if isParentCodable {
// Build a reference from the Codable type to the CodingKey enum.
for usr in enumDeclaration.usrs {
let newReference = Reference(kind: .enum, usr: usr, location: enumDeclaration.location)
newReference.name = enumDeclaration.name
newReference.parent = parent
graph.add(newReference, from: parent)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/PeripheryTests/RetentionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ final class RetentionTest: FixtureSourceGraphTestCase {

self.assertReferenced(.enum("CodingKeys")) {
self.assertReferenced(.enumelement("someUsedVar"))
self.assertNotReferenced(.enumelement("someUnusedVar"))
self.assertReferenced(.enumelement("someUnusedVar"))
}
}
assertReferenced(.struct("FixtureStruct220")) {
Expand All @@ -589,7 +589,7 @@ final class RetentionTest: FixtureSourceGraphTestCase {

self.assertReferenced(.enum("CodingKeys")) {
self.assertReferenced(.enumelement("someUsedVar"))
self.assertNotReferenced(.enumelement("someUnusedVar"))
self.assertReferenced(.enumelement("someUnusedVar"))
}
}
}
Expand Down

0 comments on commit 2fe1263

Please sign in to comment.