Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Sources/MacroToolkit/AssociatedType.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 10 additions & 0 deletions Sources/MacroToolkit/Decl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 associatedtype.
public var asAssociatedType: AssociatedType? {
_syntax.as(AssociatedTypeDeclSyntax.self).map(AssociatedType.init)
}
}
4 changes: 4 additions & 0 deletions Sources/MacroToolkit/DeclGroup/DeclGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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
}
}

Expand All @@ -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))'")
}
Expand Down
19 changes: 19 additions & 0 deletions Sources/MacroToolkit/DeclGroup/Protocol.swift
Original file line number Diff line number Diff line change
@@ -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
}
}