Skip to content

Commit 18babc1

Browse files
committed
Refine class hook diagnostics and super coding advisory
1 parent 42183f0 commit 18babc1

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

Sources/CodableKitMacros/CodeGenCore.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ extension CodeGenCore {
263263

264264
// MARK: - Code Generation Preparation
265265
extension CodeGenCore {
266+
fileprivate static func shouldWarnAboutSuperCoding(for declaration: some DeclGroupSyntax) -> Bool {
267+
let inherited = directInheritedTypeNames(in: declaration)
268+
return inherited.contains("NSObject")
269+
}
270+
266271
/// Validate that the macro is being applied to a struct declaration
267272
fileprivate func validateDeclaration(
268273
for declaration: some DeclGroupSyntax,
@@ -335,6 +340,7 @@ extension CodeGenCore {
335340
if emitAdvisories,
336341
case .classType(let hasSuperclass) = structureTypes[id],
337342
hasSuperclass,
343+
Self.shouldWarnAboutSuperCoding(for: declaration),
338344
!options.contains(.skipSuperCoding)
339345
{
340346
let message = "If the inherited type is not Codable, add '.skipSuperCoding' to avoid generating super encode/decode calls"

Tests/CodableKitTests/CodableMacroTests+class_hooks.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,24 @@ import Testing
160160
diagnostics: [
161161
.init(
162162
message: "Hook method 'willEncode' will not be invoked unless annotated with @CodableHook(.willEncode)",
163-
line: 1,
164-
column: 1,
165-
severity: .error
163+
line: 6,
164+
column: 3,
165+
severity: .error,
166+
fixIts: [.init(message: "Insert @CodableHook(.willEncode)")]
166167
),
167168
.init(
168169
message: "Hook method 'didEncode' will not be invoked unless annotated with @CodableHook(.didEncode)",
169-
line: 1,
170-
column: 1,
171-
severity: .error
170+
line: 7,
171+
column: 3,
172+
severity: .error,
173+
fixIts: [.init(message: "Insert @CodableHook(.didEncode)")]
172174
),
173175
.init(
174176
message: "Hook method 'didDecode' will not be invoked unless annotated with @CodableHook(.didDecode)",
175-
line: 1,
176-
column: 1,
177-
severity: .error
177+
line: 8,
178+
column: 3,
179+
severity: .error,
180+
fixIts: [.init(message: "Insert @CodableHook(.didDecode)")]
178181
),
179182
]
180183
)

0 commit comments

Comments
 (0)