Skip to content

Commit

Permalink
Ignored property would not check type status anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellXY committed May 28, 2024
1 parent d43bfe5 commit ef7a543
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
20 changes: 15 additions & 5 deletions Sources/CodableKitMacros/CodeGenCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 53 additions & 2 deletions Tests/CodableKitTests/CodableMacroTests+Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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: """
Expand All @@ -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 {
Expand Down

0 comments on commit ef7a543

Please sign in to comment.