Skip to content

Commit

Permalink
Don't retain RawRepresentable CodingKey enum cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch committed Dec 31, 2023
1 parent 11ebbc5 commit 3dc7d24
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ final class EnumCaseReferenceBuilder: SourceGraphMutator {

func mutate() {
for enumDeclaration in graph.declarations(ofKind: .enum) {
if isRawRepresentable(enumDeclaration) {
let isCodingKey = graph.inheritedTypeReferences(of: enumDeclaration).contains {
$0.kind == .protocol && $0.name == "CodingKey"
}

if !isCodingKey, isRawRepresentable(enumDeclaration) {
let enumCases = enumDeclaration.declarations.filter { $0.kind == .enumelement }

for enumCase in enumCases {
Expand Down
10 changes: 10 additions & 0 deletions Tests/Fixtures/RetentionFixtures/testCodingKeyEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ public class FixtureClass220: Encodable {
case someUnusedVar
}
}

public struct FixtureStruct220: Encodable {
public var someUsedVar: String?
var someUnusedVar: String?

enum CodingKeys: String, CodingKey {
case someUsedVar
case someUnusedVar
}
}
9 changes: 9 additions & 0 deletions Tests/PeripheryTests/RetentionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,15 @@ final class RetentionTest: FixtureSourceGraphTestCase {
self.assertReferenced(.varInstance("someUsedVar"))
self.assertNotReferenced(.varInstance("someUnusedVar"))

self.assertReferenced(.enum("CodingKeys")) {
self.assertReferenced(.enumelement("someUsedVar"))
self.assertNotReferenced(.enumelement("someUnusedVar"))
}
}
assertReferenced(.struct("FixtureStruct220")) {
self.assertReferenced(.varInstance("someUsedVar"))
self.assertNotReferenced(.varInstance("someUnusedVar"))

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

0 comments on commit 3dc7d24

Please sign in to comment.