Skip to content

Commit f31addb

Browse files
authored
Add Protocol and AssociatedType wrappers (#28)
* protocol * associatedtype * typo
1 parent ec8c5cf commit f31addb

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import SwiftSyntax
2+
3+
/// Wraps a AssociatedType declaration.
4+
public struct AssociatedType {
5+
public var _syntax: AssociatedTypeDeclSyntax
6+
7+
public init?(_ syntax: DeclSyntaxProtocol) {
8+
guard let syntax = syntax.as(AssociatedTypeDeclSyntax.self) else {
9+
return nil
10+
}
11+
_syntax = syntax
12+
}
13+
14+
public init(_ syntax: AssociatedTypeDeclSyntax) {
15+
_syntax = syntax
16+
}
17+
18+
public var identifier: String {
19+
_syntax.name.text
20+
}
21+
22+
public var inheritanceClause: String? {
23+
_syntax.inheritanceClause?.trimmedDescription
24+
}
25+
26+
public var inheritedTypes: [Type] {
27+
_syntax.inheritanceClause?.inheritedTypes.map(\.type).map(Type.init) ?? []
28+
}
29+
30+
public var genericWhereClauseRequirements: String? {
31+
_syntax.genericWhereClause?.requirements.trimmedDescription
32+
}
33+
}

Sources/MacroToolkit/Decl.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,14 @@ public struct Decl {
2525
public var asVariable: Variable? {
2626
_syntax.as(VariableDeclSyntax.self).map(Variable.init)
2727
}
28+
29+
/// Attempts to get the declaration as a function.
30+
public var asFunction: Function? {
31+
_syntax.as(FunctionDeclSyntax.self).map(Function.init)
32+
}
33+
34+
/// Attempts to get the declaration as a associatedtype.
35+
public var asAssociatedType: AssociatedType? {
36+
_syntax.as(AssociatedTypeDeclSyntax.self).map(AssociatedType.init)
37+
}
2838
}

Sources/MacroToolkit/DeclGroup/DeclGroup.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public enum DeclGroup: DeclGroupProtocol {
99
case `class`(Class)
1010
case `actor`(Actor)
1111
case `extension`(Extension)
12+
case `protocol`(Protocol)
1213

1314
/// A private computed property that returns the wrapped `DeclGroupProtocol` instance.
1415
///
@@ -20,6 +21,7 @@ public enum DeclGroup: DeclGroupProtocol {
2021
case .class(let wrapped): return wrapped
2122
case .actor(let wrapped): return wrapped
2223
case .extension(let wrapped): return wrapped
24+
case .protocol(let wrapped): return wrapped
2325
}
2426
}
2527

@@ -38,6 +40,8 @@ public enum DeclGroup: DeclGroupProtocol {
3840
self = .extension(Extension(syntax))
3941
} else if let syntax = syntax.as(StructDeclSyntax.self) {
4042
self = .struct(Struct(syntax))
43+
} else if let syntax = syntax.as(ProtocolDeclSyntax.self) {
44+
self = .protocol(Protocol(syntax))
4145
} else {
4246
fatalError("Unhandled decl group type '\(type(of: syntax))'")
4347
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import SwiftSyntax
2+
3+
/// Wraps a `protocol` declaration.
4+
public struct Protocol: DeclGroupProtocol, RepresentableBySyntax {
5+
/// The underlying syntax node for the `protocol` declaration.
6+
public var _syntax: ProtocolDeclSyntax
7+
8+
/// The identifier (name) of the `protocol`.
9+
public var identifier: String {
10+
_syntax.name.withoutTrivia().text
11+
}
12+
13+
/// Initializes a `Protocol` instance with the given syntax node.
14+
///
15+
/// - Parameter syntax: The syntax node representing the `protocol` declaration.
16+
public init(_ syntax: ProtocolDeclSyntax) {
17+
_syntax = syntax
18+
}
19+
}

0 commit comments

Comments
 (0)