Skip to content

Commit b8c27f2

Browse files
committed
add typeDescription
1 parent ffc6d06 commit b8c27f2

File tree

1 file changed

+104
-11
lines changed

1 file changed

+104
-11
lines changed

Sources/SyntaxKit/ParsedType.swift

+104-11
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public struct ParsedType {
7171
if let genericArgumentClause = type.genericArgumentClause,
7272
!genericArgumentClause.arguments.isEmpty {
7373
let arguments = genericArgumentClause.arguments
74-
switch (arguments.count, name.tokenKind) { // FIXME: Change from TRIMMED
74+
switch (arguments.count, name.tokenKind) {
7575
case (1, .identifier("Optional")):
7676
self.type = try .optional(of: Self(syntax: arguments.first!.argument))
7777
case (1, .identifier("Array")):
@@ -120,16 +120,51 @@ public struct ParsedType {
120120
} else if let type = syntax.as(MetatypeTypeSyntax.self) {
121121
let baseType = try Self(syntax: type.baseType)
122122
self.type = .metatype(base: baseType)
123-
// TODO: -
124-
/// - ``AttributedTypeSyntax``
125-
/// - ``ClassRestrictionTypeSyntax``
126-
/// - ``CompositionTypeSyntax``
127-
/// - ``ImplicitlyUnwrappedOptionalTypeSyntax``
128-
/// - ``MissingTypeSyntax``
129-
/// - ``NamedOpaqueReturnTypeSyntax``
130-
/// - ``PackElementTypeSyntax``
131-
/// - ``PackExpansionTypeSyntax``
132-
/// - ``SuppressedTypeSyntax``
123+
} else if let _ = syntax.as(AttributedTypeSyntax.self) {
124+
throw Error.unknownParameterType(
125+
syntax.trimmed.description,
126+
syntaxType: syntax.syntaxNodeType
127+
)
128+
} else if let _ = syntax.as(ClassRestrictionTypeSyntax.self) {
129+
throw Error.unknownParameterType(
130+
syntax.trimmed.description,
131+
syntaxType: syntax.syntaxNodeType
132+
)
133+
} else if let _ = syntax.as(CompositionTypeSyntax.self) {
134+
throw Error.unknownParameterType(
135+
syntax.trimmed.description,
136+
syntaxType: syntax.syntaxNodeType
137+
)
138+
} else if let _ = syntax.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) {
139+
throw Error.unknownParameterType(
140+
syntax.trimmed.description,
141+
syntaxType: syntax.syntaxNodeType
142+
)
143+
} else if let _ = syntax.as(MissingTypeSyntax.self) {
144+
throw Error.unknownParameterType(
145+
syntax.trimmed.description,
146+
syntaxType: syntax.syntaxNodeType
147+
)
148+
} else if let _ = syntax.as(NamedOpaqueReturnTypeSyntax.self) {
149+
throw Error.unknownParameterType(
150+
syntax.trimmed.description,
151+
syntaxType: syntax.syntaxNodeType
152+
)
153+
} else if let _ = syntax.as(PackElementTypeSyntax.self) {
154+
throw Error.unknownParameterType(
155+
syntax.trimmed.description,
156+
syntaxType: syntax.syntaxNodeType
157+
)
158+
} else if let _ = syntax.as(PackExpansionTypeSyntax.self) {
159+
throw Error.unknownParameterType(
160+
syntax.trimmed.description,
161+
syntaxType: syntax.syntaxNodeType
162+
)
163+
} else if let _ = syntax.as(SuppressedTypeSyntax.self) {
164+
throw Error.unknownParameterType(
165+
syntax.trimmed.description,
166+
syntaxType: syntax.syntaxNodeType
167+
)
133168
} else {
134169
throw Error.unknownParameterType(
135170
syntax.trimmed.description,
@@ -143,12 +178,20 @@ extension ParsedType: CustomStringConvertible {
143178
public var description: String {
144179
"ParsedType(syntax: \(String(describing: self.syntax)), type: \(self.type))"
145180
}
181+
182+
public var typeDescription: String {
183+
self.type.typeDescription
184+
}
146185
}
147186

148187
extension ParsedType: CustomDebugStringConvertible {
149188
public var debugDescription: String {
150189
"ParsedType(syntax: \(String(reflecting: self.syntax)), type: \(self.type.debugDescription))"
151190
}
191+
192+
public var typeDebugDescription: String {
193+
self.type.typeDebugDescription
194+
}
152195
}
153196

154197
extension ParsedType.BaseType: CustomStringConvertible {
@@ -176,6 +219,31 @@ extension ParsedType.BaseType: CustomStringConvertible {
176219
"\(name)<\(arguments.map(\.description).joined(separator: ", "))>"
177220
}
178221
}
222+
223+
public var typeDescription: String {
224+
switch self {
225+
case let .identifier(type):
226+
"\(type.trimmed.description)"
227+
case let .optional(type):
228+
"\(type.typeDescription)?"
229+
case let .array(type):
230+
"[\(type.typeDescription)]"
231+
case let .dictionary(key, value):
232+
"[\(key.typeDescription): \(value.typeDescription)]"
233+
case let .tuple(elements):
234+
"(\(elements.map(\.typeDescription).joined(separator: ", ")))"
235+
case let .some(type):
236+
"some \(type.typeDescription)"
237+
case let .any(type):
238+
"any \(type.typeDescription)"
239+
case let .member(base, `extension`):
240+
"\(base.typeDescription).\(`extension`.typeDescription)"
241+
case let .metatype(base):
242+
"\(base.typeDescription).Type"
243+
case let .unknownGeneric(name, arguments: arguments):
244+
"\(name)<\(arguments.map(\.typeDescription).joined(separator: ", "))>"
245+
}
246+
}
179247
}
180248

181249
extension ParsedType.BaseType: CustomDebugStringConvertible {
@@ -203,4 +271,29 @@ extension ParsedType.BaseType: CustomDebugStringConvertible {
203271
"\(name.debugDescription)<\(arguments.map(\.debugDescription).joined(separator: ", "))>"
204272
}
205273
}
274+
275+
public var typeDebugDescription: String {
276+
switch self {
277+
case let .identifier(type):
278+
"\(type.trimmed.debugDescription)"
279+
case let .optional(type):
280+
"\(type.type.typeDebugDescription)?"
281+
case let .array(type):
282+
"[\(type.typeDebugDescription)]"
283+
case let .dictionary(key, value):
284+
"[\(key.typeDebugDescription): \(value.typeDebugDescription)]"
285+
case let .tuple(elements):
286+
"(\(elements.map(\.typeDebugDescription).joined(separator: ", ")))"
287+
case let .some(type):
288+
"some \(type.typeDebugDescription)"
289+
case let .any(type):
290+
"any \(type.typeDebugDescription)"
291+
case let .member(base, `extension`):
292+
"\(base.typeDebugDescription).\(`extension`.typeDebugDescription)"
293+
case let .metatype(base):
294+
"\(base.typeDebugDescription).Type"
295+
case let .unknownGeneric(name, arguments: arguments):
296+
"\(name.typeDebugDescription)<\(arguments.map(\.typeDebugDescription).joined(separator: ", "))>"
297+
}
298+
}
206299
}

0 commit comments

Comments
 (0)