|
22 | 22 | import SwiftSyntax |
23 | 23 | import SwiftSyntaxMacros |
24 | 24 |
|
| 25 | +private enum IgnoreCodingMode: Equatable { |
| 26 | + case both |
| 27 | + case encoding |
| 28 | + case decoding |
| 29 | + |
| 30 | + var macroName: String { |
| 31 | + switch self { |
| 32 | + case .both: |
| 33 | + return "CodingIgnored" |
| 34 | + case .encoding: |
| 35 | + return "EncodingIgnored" |
| 36 | + case .decoding: |
| 37 | + return "DecodingIgnored" |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + var diagnosticPrefix: String { |
| 42 | + "@\(macroName) macro" |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +private func validateIgnoredProperty( |
| 47 | + declaration: some DeclSyntaxProtocol, |
| 48 | + mode: IgnoreCodingMode |
| 49 | +) throws { |
| 50 | + guard |
| 51 | + let variable = declaration.as(VariableDeclSyntax.self), |
| 52 | + let name = variable.name |
| 53 | + else { |
| 54 | + throw MacroError(text: "\(mode.diagnosticPrefix) is only for property.") |
| 55 | + } |
| 56 | + |
| 57 | + let ignoreMacros = ["CodingIgnored", "EncodingIgnored", "DecodingIgnored"] |
| 58 | + let usedIgnoreMacros = ignoreMacros.filter { variable.attributes.containsAttribute(named: $0) } |
| 59 | + if usedIgnoreMacros.count > 1 { |
| 60 | + throw MacroError( |
| 61 | + text: "\(mode.diagnosticPrefix) cannot be used together with @\(usedIgnoreMacros.filter { $0 != mode.macroName }.joined(separator: ", @"))." |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + if mode == .encoding, variable.attributes.containsAttribute(named: "EncodingKey") { |
| 66 | + throw MacroError(text: "@EncodingIgnored macro cannot be used together with @EncodingKey.") |
| 67 | + } |
| 68 | + |
| 69 | + if mode != .encoding { |
| 70 | + if variable.isOptional { |
| 71 | + return |
| 72 | + } |
| 73 | + if variable.initExpr != nil { |
| 74 | + return |
| 75 | + } |
| 76 | + if let type = variable.type, |
| 77 | + canGenerateDefaultValue(for: type) { |
| 78 | + return |
| 79 | + } |
| 80 | + throw MacroError(text: "The ignored property `\(name)` should have a default value, or be set as an optional type.") |
| 81 | + } |
| 82 | +} |
| 83 | + |
25 | 84 | public struct CodingIgnored: PeerMacro { |
26 | 85 | public static func expansion( |
27 | 86 | of node: AttributeSyntax, |
28 | 87 | providingPeersOf declaration: some DeclSyntaxProtocol, |
29 | 88 | in context: some MacroExpansionContext |
30 | 89 | ) throws -> [DeclSyntax] { |
31 | | - guard |
32 | | - let variable = declaration.as(VariableDeclSyntax.self), |
33 | | - let name = variable.name |
34 | | - else { |
35 | | - throw MacroError(text: "@CodingIgnored macro is only for property.") |
36 | | - } |
37 | | - |
38 | | - if variable.attributes.firstAttribute(named: "CodingIgnored") != nil { |
39 | | - if variable.isOptional { |
40 | | - return [] |
41 | | - } |
42 | | - if variable.initExpr != nil { |
43 | | - return [] |
44 | | - } |
45 | | - if let type = variable.type, |
46 | | - canGenerateDefaultValue(for: type) { |
47 | | - return [] |
48 | | - } |
49 | | - throw MacroError(text: "The ignored property `\(name)` should have a default value, or be set as an optional type.") |
50 | | - } |
| 90 | + try validateIgnoredProperty(declaration: declaration, mode: .both) |
51 | 91 | return [] |
52 | 92 | } |
53 | | - |
54 | | - static func canGenerateDefaultValue(for type: String) -> Bool { |
55 | | - let trimmed = type.trimmingCharacters(in: .whitespaces) |
56 | | - let basicType = [ |
57 | | - "Int", "Int8", "Int16", "Int32", "Int64", "Int128", |
58 | | - "UInt", "UInt8", "UInt16", "UInt32", "UInt64", "UInt128", |
59 | | - "Bool", "String", "Float", "Double" |
60 | | - ].contains(trimmed) |
61 | | - if basicType |
62 | | - || (trimmed.hasPrefix("[") && trimmed.hasSuffix("]")) |
63 | | - || trimmed.hasPrefix("Set<") { |
64 | | - return true |
65 | | - } |
66 | | - return false |
| 93 | +} |
| 94 | + |
| 95 | +public struct EncodingIgnored: PeerMacro { |
| 96 | + public static func expansion( |
| 97 | + of node: AttributeSyntax, |
| 98 | + providingPeersOf declaration: some DeclSyntaxProtocol, |
| 99 | + in context: some MacroExpansionContext |
| 100 | + ) throws -> [DeclSyntax] { |
| 101 | + try validateIgnoredProperty(declaration: declaration, mode: .encoding) |
| 102 | + return [] |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +public struct DecodingIgnored: PeerMacro { |
| 107 | + public static func expansion( |
| 108 | + of node: AttributeSyntax, |
| 109 | + providingPeersOf declaration: some DeclSyntaxProtocol, |
| 110 | + in context: some MacroExpansionContext |
| 111 | + ) throws -> [DeclSyntax] { |
| 112 | + try validateIgnoredProperty(declaration: declaration, mode: .decoding) |
| 113 | + return [] |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func canGenerateDefaultValue(for type: String) -> Bool { |
| 118 | + let trimmed = type.trimmingCharacters(in: .whitespaces) |
| 119 | + let basicType = [ |
| 120 | + "Int", "Int8", "Int16", "Int32", "Int64", "Int128", |
| 121 | + "UInt", "UInt8", "UInt16", "UInt32", "UInt64", "UInt128", |
| 122 | + "Bool", "String", "Float", "Double" |
| 123 | + ].contains(trimmed) |
| 124 | + if basicType |
| 125 | + || (trimmed.hasPrefix("[") && trimmed.hasSuffix("]")) |
| 126 | + || trimmed.hasPrefix("Set<") { |
| 127 | + return true |
67 | 128 | } |
| 129 | + return false |
68 | 130 | } |
0 commit comments