Skip to content

Commit 18bceb6

Browse files
committed
Document all type syntax wrappers and the associated TypeProtocol protocol
1 parent cdca1e4 commit 18bceb6

File tree

7 files changed

+66
-168
lines changed

7 files changed

+66
-168
lines changed

AddCompletionHandler.swift

Lines changed: 0 additions & 158 deletions
This file was deleted.

Sources/MacroToolkit/ConstrainedSugarType.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import SwiftSyntax
22

3-
/// Wraps a class restriction type (i.e. `any Protocol` or `some Protocol`).
3+
/// Wraps a constrained sugar type (i.e. `any Protocol` or `some Protocol`).
44
public struct ConstrainedSugarType: TypeProtocol {
55
public var _baseSyntax: ConstrainedSugarTypeSyntax
66
public var _attributedSyntax: AttributedTypeSyntax?
77

8-
public init(_ syntax: ConstrainedSugarTypeSyntax, attributedSyntax: AttributedTypeSyntax? = nil) {
8+
public init(
9+
_ syntax: ConstrainedSugarTypeSyntax,
10+
attributedSyntax: AttributedTypeSyntax? = nil
11+
) {
912
_baseSyntax = syntax
1013
_attributedSyntax = attributedSyntax
1114
}

Sources/MacroToolkit/FunctionType.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ public struct FunctionType: TypeProtocol {
1313
_attributedSyntax = attributedSyntax
1414
}
1515

16+
/// The return type that the function type describes.
1617
public var returnType: Type {
1718
Type(_baseSyntax.output.returnType)
1819
}
1920

21+
/// The types of the parameters the function type describes.
2022
public var parameters: [Type] {
2123
_baseSyntax.arguments.map(\.type).map(Type.init)
2224
}

Sources/MacroToolkit/MemberTypeIdentifierSyntax.swift renamed to Sources/MacroToolkit/MemberType.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import SwiftSyntax
22

3-
/// Wraps a member type (e.g. `Foundation.URL`).
3+
/// Wraps a member type (e.g. `Array<Int>.Element`).
44
public struct MemberType: TypeProtocol {
55
public var _baseSyntax: MemberTypeIdentifierSyntax
66
public var _attributedSyntax: AttributedTypeSyntax?
77

8-
public init(_ syntax: MemberTypeIdentifierSyntax, attributedSyntax: AttributedTypeSyntax? = nil) {
8+
public init(
9+
_ syntax: MemberTypeIdentifierSyntax,
10+
attributedSyntax: AttributedTypeSyntax? = nil
11+
) {
912
_baseSyntax = syntax
1013
_attributedSyntax = attributedSyntax
1114
}

Sources/MacroToolkit/SimpleType.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ public struct SimpleType: TypeProtocol {
55
public var _baseSyntax: SimpleTypeIdentifierSyntax
66
public var _attributedSyntax: AttributedTypeSyntax?
77

8-
public init(_ syntax: SimpleTypeIdentifierSyntax, attributedSyntax: AttributedTypeSyntax? = nil) {
8+
public init(
9+
_ syntax: SimpleTypeIdentifierSyntax,
10+
attributedSyntax: AttributedTypeSyntax? = nil
11+
) {
912
_baseSyntax = syntax
1013
_attributedSyntax = attributedSyntax
1114
}
1215

16+
/// The base type's name (e.g. for `Array<Int>` it would be `"Array"`).
1317
public var name: String {
1418
_baseSyntax.name.description
1519
}
1620

21+
/// The type's generic arguments if any were supplied (e.g. for
22+
/// `Dictionary<Int, String>` it would be `["Int", "String"]`).
1723
public var genericArguments: [Type]? {
1824
_baseSyntax.genericArgumentClause.map { clause in
1925
clause.arguments.map(\.argumentType).map(Type.init)

Sources/MacroToolkit/Type.swift

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
import SwiftSyntax
22
import SwiftSyntaxBuilder
33

4-
// TODO: Always normalize typed and pretend sugar doesn't exist (e.g. Int? looks like Optional<Int> to devs)
4+
// TODO: Implement type normalisation and pretend sugar doesn't exist (e.g. Int? looks like Optional<Int> to devs)
55
/// Wraps type syntax (e.g. `Result<Success, Failure>`).
66
public enum Type: TypeProtocol, SyntaxExpressibleByStringInterpolation {
7+
/// An array type (e.g. `[Int]`).
78
case array(ArrayType)
9+
/// A `class` token in a conformance list. Equivalent to `AnyObject`.
810
case classRestriction(ClassRestrictionType)
11+
/// A composition of two types (e.g. `Encodable & Decodable`). Used to
12+
/// combine protocol requirements.
913
case composition(CompositionType)
14+
/// A sugared protocl type (e.g. `any T` or `some T`).
1015
case constrainedSugar(ConstrainedSugarType)
16+
/// A dictionary type (e.g. `[Int: String]`).
1117
case dictionary(DictionaryType)
18+
/// A function type (e.g. `() -> ()`).
1219
case function(FunctionType)
20+
/// An implicitly unwrapped optional type (e.g. `Int!`).
1321
case implicitlyUnwrappedOptional(ImplicitlyUnwrappedOptionalType)
22+
/// A member type (e.g. `Array<Int>.Element`).
1423
case member(MemberType)
24+
/// A metatype (e.g. `Int.Type` or `Encodable.Protocol`).
1525
case metatype(MetatypeType)
26+
/// A placeholder for invalid types that the resilient parser ignored.
1627
case missing(MissingType)
28+
/// An optional type (e.g. `Int?`).
1729
case optional(OptionalType)
30+
/// A pack expansion type (e.g. `repeat each V`).
1831
case packExpansion(PackExpansionType)
32+
/// A pack reference type (e.g. `each V`).
1933
case packReference(PackReferenceType)
34+
/// A simple type (e.g. `Int` or `Box<Int>`).
2035
case simple(SimpleType)
36+
/// A suppressed type in a conformance position (e.g. `~Copyable`).
2137
case suppressed(SuppressedType)
38+
//// A tuple type (e.g. `(Int, String)`).
2239
case tuple(TupleType)
23-
40+
2441
public var _baseSyntax: TypeSyntax {
2542
let type: any TypeProtocol = switch self {
2643
case .array(let type): type
@@ -65,11 +82,11 @@ public enum Type: TypeProtocol, SyntaxExpressibleByStringInterpolation {
6582
return type._attributedSyntax
6683
}
6784

85+
/// Wrap a ``TypeSyntax`` (e.g. `Int?` or `MyStruct<[String]>!`).
6886
public init(_ syntax: TypeSyntax) {
6987
self.init(syntax, attributedSyntax: nil)
7088
}
7189

72-
/// Ignore the `attributedSyntax` attribute; it exists because of protocol conformance.
7390
public init(_ syntax: TypeSyntax, attributedSyntax: AttributedTypeSyntax? = nil) {
7491
// TODO: Move this weird initializer to an internal protocol if possible
7592
let syntax: TypeSyntaxProtocol = attributedSyntax ?? syntax
@@ -110,11 +127,15 @@ public enum Type: TypeProtocol, SyntaxExpressibleByStringInterpolation {
110127
}
111128
}
112129

130+
// TODO: add an optional version to all type syntax wrappers maybe?
131+
/// Allows string interpolation syntax to be used to express type syntax.
113132
public init(stringInterpolation: SyntaxStringInterpolation) {
114133
self.init(TypeSyntax(stringInterpolation: stringInterpolation))
115134
}
116135

136+
/// A normalized description of the type (e.g. for `()` this would be `Void`).
117137
public var normalizedDescription: String {
138+
// TODO: Implement proper type normalization
118139
// TODO: Normalize types nested within the type too (e.g. the parameter types of a function type)
119140
if let tupleSyntax = _syntax.as(TupleTypeSyntax.self) {
120141
if tupleSyntax.elements.count == 0 {
@@ -130,28 +151,33 @@ public enum Type: TypeProtocol, SyntaxExpressibleByStringInterpolation {
130151
}
131152
}
132153

154+
/// Gets whether the type is a void type (i.e. `Void`, `()`, `(Void)`, `((((()))))`, etc.).
133155
public var isVoid: Bool {
134156
normalizedDescription == "Void"
135157
}
136158

137159
// TODO: Generate type conversions with macro?
160+
/// Attempts to get the type as a simple type.
138161
public var asSimpleType: SimpleType? {
139162
switch self {
140163
case .simple(let type): type
141164
default: nil
142165
}
143166
}
144167

168+
/// Attempts to get the type as a function type.
145169
public var asFunctionType: FunctionType? {
146170
switch self {
147171
case .function(let type): type
148172
default: nil
149173
}
150174
}
175+
176+
// TODO: Implement rest of conversions
151177
}
152178

153-
extension Optional<Type> {
154-
/// If `nil`, the type is considered void, otherwise the underlying type is queried.
179+
extension Type? {
180+
/// If `nil`, the type is considered void, otherwise the underlying type is queried (see ``Type/isVoid``).
155181
public var isVoid: Bool {
156182
if let self = self {
157183
return self.isVoid

Sources/MacroToolkit/TypeProtocol.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
11
import SwiftSyntax
22

33
public protocol TypeProtocol {
4+
/// The underlying type syntax being wrapped.
45
associatedtype WrappedSyntax: TypeSyntaxProtocol
6+
/// The syntax associated with the type itself. For diagnostics ``_syntax``
7+
/// may be better (as it includes the syntax for associated attributes).
58
var _baseSyntax: WrappedSyntax { get }
9+
/// The syntax associated with the attributes attached to the type (if any).
10+
/// (e.g. the type `@escaping () -> ()` has the `@escaping` attribute).
611
var _attributedSyntax: AttributedTypeSyntax? { get }
12+
/// This initializer is an implementation detail and shouldn't need to be
13+
/// used for most normal usecases.
714
init(_ syntax: WrappedSyntax, attributedSyntax: AttributedTypeSyntax?)
815
}
916

1017
extension TypeProtocol {
18+
/// The syntax node referring to the entire type (including attributes if any).
19+
/// Use for diagnostics.
1120
public var _syntax: any TypeSyntaxProtocol {
1221
_attributedSyntax ?? _baseSyntax
1322
}
1423

24+
/// A textual representation of the type without trivia (not normalized).
1525
public var description: String {
1626
_syntax.withoutTrivia().description
1727
}
1828

29+
/// Attempts to convert wrapped type syntax to this specific type of type syntax
30+
/// (how confusing).
1931
public init?(_ type: Type) {
2032
guard let type = Self(type._syntax) else {
2133
return nil
2234
}
2335
self = type
2436
}
2537

38+
/// Initializes this type of type syntax from attributed syntax (the attributed
39+
/// syntax's inner type syntax is passed to the regular `init` and the attributes
40+
/// are stored in `_attributedTypeSyntax`).
2641
public init?(attributedSyntax: AttributedTypeSyntax) {
2742
guard let baseSyntax = attributedSyntax.baseType.as(WrappedSyntax.self) else {
2843
return nil
2944
}
3045
self.init(baseSyntax, attributedSyntax: attributedSyntax)
3146
}
3247

48+
/// Attempts to initialize this type of type syntax from arbitrary type syntax.
3349
public init?(_ syntax: any TypeSyntaxProtocol) {
3450
if let syntax = syntax.as(WrappedSyntax.self) {
3551
self.init(syntax, attributedSyntax: nil)

0 commit comments

Comments
 (0)