-
Notifications
You must be signed in to change notification settings - Fork 2
fix: Fixed compiler error when using @PolymorphicEnumCodable macro on enums defined inside nested types #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f5e1313
29ad1b2
e6bf128
7ed5343
c97f340
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,38 +10,47 @@ import SwiftDiagnostics | |
| import SwiftSyntax | ||
| import SwiftSyntaxMacros | ||
|
|
||
| public struct PolymorphicEnumEncodableMacro: ExtensionMacro { | ||
| public struct PolymorphicEnumEncodableMacro: MemberMacro { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| attachedTo declaration: some DeclGroupSyntax, | ||
| providingExtensionsOf type: some TypeSyntaxProtocol, | ||
| conformingTo protocols: [TypeSyntax], | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [ExtensionDeclSyntax] { | ||
| // Ensure the declaration is an enum and extract case information | ||
| providingMembersOf declaration: some DeclGroupSyntax, | ||
| in _: some MacroExpansionContext, | ||
| ) throws -> [DeclSyntax] { | ||
| guard let enumDecl = declaration.as(EnumDeclSyntax.self) else { | ||
| throw CodableKitError.message("`@PolymorphicEnumEncodable` can only be attached to enums") | ||
| } | ||
|
|
||
| // Validate identifierCodingKey | ||
| try PolymorphicEnumCodableFactory.validateIdentifierCodingKey(in: node) | ||
|
|
||
| // Extract case information from the enum | ||
| let caseInfos = try PolymorphicEnumCodableFactory.extractCaseInfos(from: enumDecl) | ||
|
|
||
| let accessLevel = AccessLevelModifier.stringValue(from: declaration) | ||
|
|
||
| let encodeToEncoderSyntax = PolymorphicEnumCodableFactory.makeEncodeToEncoder( | ||
| with: caseInfos, | ||
| accessLevel: accessLevel | ||
| accessLevel: accessLevel, | ||
| ) | ||
|
|
||
| return [ | ||
| try ExtensionDeclSyntax("extension \(raw: enumDecl.name.text): Encodable") { | ||
| """ | ||
| \(raw: encodeToEncoderSyntax) | ||
| """ | ||
| }, | ||
| "\(raw: encodeToEncoderSyntax)" | ||
| ] | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| extension PolymorphicEnumEncodableMacro: ExtensionMacro { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| attachedTo declaration: some DeclGroupSyntax, | ||
| providingExtensionsOf type: some TypeSyntaxProtocol, | ||
| conformingTo _: [TypeSyntax], | ||
| in _: some MacroExpansionContext, | ||
| ) throws -> [ExtensionDeclSyntax] { | ||
| guard let enumDecl = declaration.as(EnumDeclSyntax.self) else { | ||
| throw CodableKitError.message("`@PolymorphicEnumEncodable` can only be attached to enums") | ||
| } | ||
|
|
||
| try PolymorphicEnumCodableFactory.validateIdentifierCodingKey(in: node) | ||
| _ = try PolymorphicEnumCodableFactory.extractCaseInfos(from: enumDecl) | ||
|
|
||
| return try [ExtensionDeclSyntax("extension \(type.trimmed): Encodable {}")] | ||
| } | ||
|
Comment on lines
+39
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial [MEDIUM] Both macro roles duplicate validation, causing duplicate diagnostics. The This is consistent with commit message noting "added validation to the ExtensionMacro role to prevent emitting orphaned protocol conformances when MemberMacro validation fails." However, consider extracting validation into a shared helper that caches results, or accept the duplicate diagnostics as an intentional design trade-off for preventing orphaned extensions. The tests explicitly expect duplicate 🤖 Prompt for AI Agents |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.