From 20796af8aeced0d3c4f096a2f2937b0a4606d3c5 Mon Sep 17 00:00:00 2001 From: Casper Zandbergen Date: Tue, 17 Jun 2025 13:58:40 +0200 Subject: [PATCH 1/3] protocol --- .../MacroToolkit/DeclGroup/DeclGroup.swift | 4 ++++ Sources/MacroToolkit/DeclGroup/Protocol.swift | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 Sources/MacroToolkit/DeclGroup/Protocol.swift diff --git a/Sources/MacroToolkit/DeclGroup/DeclGroup.swift b/Sources/MacroToolkit/DeclGroup/DeclGroup.swift index 837d244..aa78e2b 100644 --- a/Sources/MacroToolkit/DeclGroup/DeclGroup.swift +++ b/Sources/MacroToolkit/DeclGroup/DeclGroup.swift @@ -9,6 +9,7 @@ public enum DeclGroup: DeclGroupProtocol { case `class`(Class) case `actor`(Actor) case `extension`(Extension) + case `protocol`(Protocol) /// A private computed property that returns the wrapped `DeclGroupProtocol` instance. /// @@ -20,6 +21,7 @@ public enum DeclGroup: DeclGroupProtocol { case .class(let wrapped): return wrapped case .actor(let wrapped): return wrapped case .extension(let wrapped): return wrapped + case .protocol(let wrapped): return wrapped } } @@ -38,6 +40,8 @@ public enum DeclGroup: DeclGroupProtocol { self = .extension(Extension(syntax)) } else if let syntax = syntax.as(StructDeclSyntax.self) { self = .struct(Struct(syntax)) + } else if let syntax = syntax.as(ProtocolDeclSyntax.self) { + self = .protocol(Protocol(syntax)) } else { fatalError("Unhandled decl group type '\(type(of: syntax))'") } diff --git a/Sources/MacroToolkit/DeclGroup/Protocol.swift b/Sources/MacroToolkit/DeclGroup/Protocol.swift new file mode 100644 index 0000000..a825972 --- /dev/null +++ b/Sources/MacroToolkit/DeclGroup/Protocol.swift @@ -0,0 +1,19 @@ +import SwiftSyntax + +/// Wraps a `protocol` declaration. +public struct Protocol: DeclGroupProtocol, RepresentableBySyntax { + /// The underlying syntax node for the `protocol` declaration. + public var _syntax: ProtocolDeclSyntax + + /// The identifier (name) of the `protocol`. + public var identifier: String { + _syntax.name.withoutTrivia().text + } + + /// Initializes a `Protocol` instance with the given syntax node. + /// + /// - Parameter syntax: The syntax node representing the `protocol` declaration. + public init(_ syntax: ProtocolDeclSyntax) { + _syntax = syntax + } +} From a79efcf9e9e077d9dcfa4e2ae897e8925a5eb9b7 Mon Sep 17 00:00:00 2001 From: Casper Zandbergen Date: Tue, 17 Jun 2025 13:59:19 +0200 Subject: [PATCH 2/3] associatedtype --- Sources/MacroToolkit/AssociatedType.swift | 33 +++++++++++++++++++++++ Sources/MacroToolkit/Decl.swift | 10 +++++++ 2 files changed, 43 insertions(+) create mode 100644 Sources/MacroToolkit/AssociatedType.swift diff --git a/Sources/MacroToolkit/AssociatedType.swift b/Sources/MacroToolkit/AssociatedType.swift new file mode 100644 index 0000000..30a20ad --- /dev/null +++ b/Sources/MacroToolkit/AssociatedType.swift @@ -0,0 +1,33 @@ +import SwiftSyntax + +/// Wraps a AssociatedType declaration. +public struct AssociatedType { + public var _syntax: AssociatedTypeDeclSyntax + + public init?(_ syntax: DeclSyntaxProtocol) { + guard let syntax = syntax.as(AssociatedTypeDeclSyntax.self) else { + return nil + } + _syntax = syntax + } + + public init(_ syntax: AssociatedTypeDeclSyntax) { + _syntax = syntax + } + + public var identifier: String { + _syntax.name.text + } + + public var inheritanceClause: String? { + _syntax.inheritanceClause?.trimmedDescription + } + + public var inheritedTypes: [Type] { + _syntax.inheritanceClause?.inheritedTypes.map(\.type).map(Type.init) ?? [] + } + + public var genericWhereClauseRequirements: String? { + _syntax.genericWhereClause?.requirements.trimmedDescription + } +} diff --git a/Sources/MacroToolkit/Decl.swift b/Sources/MacroToolkit/Decl.swift index 6db7ec4..00c758f 100644 --- a/Sources/MacroToolkit/Decl.swift +++ b/Sources/MacroToolkit/Decl.swift @@ -25,4 +25,14 @@ public struct Decl { public var asVariable: Variable? { _syntax.as(VariableDeclSyntax.self).map(Variable.init) } + + /// Attempts to get the declaration as a function. + public var asFunction: Function? { + _syntax.as(FunctionDeclSyntax.self).map(Function.init) + } + + /// Attempts to get the declaration as a function. + public var asAssociatedType: AssociatedType? { + _syntax.as(AssociatedTypeDeclSyntax.self).map(AssociatedType.init) + } } From ee6bce958be536ca75c2856851cda814b9326b81 Mon Sep 17 00:00:00 2001 From: Casper Zandbergen Date: Tue, 17 Jun 2025 14:00:41 +0200 Subject: [PATCH 3/3] typo --- Sources/MacroToolkit/Decl.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/MacroToolkit/Decl.swift b/Sources/MacroToolkit/Decl.swift index 00c758f..0f45ef0 100644 --- a/Sources/MacroToolkit/Decl.swift +++ b/Sources/MacroToolkit/Decl.swift @@ -31,7 +31,7 @@ public struct Decl { _syntax.as(FunctionDeclSyntax.self).map(Function.init) } - /// Attempts to get the declaration as a function. + /// Attempts to get the declaration as a associatedtype. public var asAssociatedType: AssociatedType? { _syntax.as(AssociatedTypeDeclSyntax.self).map(AssociatedType.init) }