diff --git a/Sources/CodableKitMacros/CodeGenCore.swift b/Sources/CodableKitMacros/CodeGenCore.swift index c0590d8..dccd54d 100644 --- a/Sources/CodableKitMacros/CodeGenCore.swift +++ b/Sources/CodableKitMacros/CodeGenCore.swift @@ -84,11 +84,21 @@ extension CodeGenCore { guard !modifiers.contains("static") else { return [] } guard let defaultType = variable.bindings.last?.typeAnnotation?.type else { - throw SimpleDiagnosticMessage( - message: "Properties must have a type annotation", - diagnosticID: messageID, - severity: .error - ) + // If no binding is found, return empty array. + guard let lastBinding = variable.bindings.last else { return [] } + // To check if a property is ignored, create a temporary property. If the property is ignored, return an empty + // array. Otherwise, throw an error. + let tmpProperty = Property(attributes: attributes, binding: lastBinding, defaultType: "Any") + + if tmpProperty.ignored { + return [] + } else { + throw SimpleDiagnosticMessage( + message: "Properties must have a type annotation", + diagnosticID: messageID, + severity: .error + ) + } } return variable.bindings.map { binding in diff --git a/Tests/CodableKitTests/CodableMacroTests+Diagnostics.swift b/Tests/CodableKitTests/CodableMacroTests+Diagnostics.swift index 397c39e..7746855 100644 --- a/Tests/CodableKitTests/CodableMacroTests+Diagnostics.swift +++ b/Tests/CodableKitTests/CodableMacroTests+Diagnostics.swift @@ -43,6 +43,57 @@ final class CodableKitDiagnosticsTests: XCTestCase { #endif } + func testMacroWithIgnoredPropertyTypeAnnotation() throws { + #if canImport(CodableKitMacros) + assertMacroExpansion( + """ + @Codable + public struct User { + let id: UUID + let name: String + let age: Int + @CodableKey(options: .ignored) + var ignored: String = "Hello World" + } + """, + expandedSource: """ + public struct User { + let id: UUID + let name: String + let age: Int + var ignored: String = "Hello World" + } + + extension User: Codable { + enum CodingKeys: String, CodingKey { + case id + case name + case age + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + id = try container.decode(UUID.self, forKey: .id) + name = try container.decode(String.self, forKey: .name) + age = try container.decode(Int.self, forKey: .age) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(age, forKey: .age) + } + } + """, + macros: macros, + indentationWidth: .spaces(2) + ) + #else + throw XCTSkip("macros are only supported when running tests for the host platform") + #endif + } + func testMacroWithStaticTypeAnnotation() throws { #if canImport(CodableKitMacros) assertMacroExpansion( @@ -53,7 +104,7 @@ final class CodableKitDiagnosticsTests: XCTestCase { let name: String let age: Int - static let staticProperty: String = "Hello World" + static let staticProperty = "Hello World" } """, expandedSource: """ @@ -62,7 +113,7 @@ final class CodableKitDiagnosticsTests: XCTestCase { let name: String let age: Int - static let staticProperty: String = "Hello World" + static let staticProperty = "Hello World" } extension User: Codable {