Skip to content

Commit

Permalink
update to support typed-throw
Browse files Browse the repository at this point in the history
  • Loading branch information
fummicc1 committed Jun 30, 2024
1 parent 41de219 commit 4ca5407
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
15 changes: 13 additions & 2 deletions Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ extension FunctionDeclSyntax {
genericTypeParams: genericTypeParams,
genericWhereClause: genericWhereClause,
params: params,
throwsOrRethrows: self.signature.effectSpecifiers?.throwsClause?.throwsSpecifier.text,
throwsOrRethrows: self.signature.effectSpecifiers?.throwsClause?.text,
asyncOrReasync: self.signature.effectSpecifiers?.asyncSpecifier?.text,
isStatic: isStatic,
offset: self.offset,
Expand Down Expand Up @@ -473,7 +473,7 @@ extension InitializerDeclSyntax {
genericTypeParams: genericTypeParams,
genericWhereClause: genericWhereClause,
params: params,
throwsOrRethrows: self.signature.effectSpecifiers?.throwsClause?.throwsSpecifier.text,
throwsOrRethrows: self.signature.effectSpecifiers?.throwsClause?.text,
asyncOrReasync: self.signature.effectSpecifiers?.asyncSpecifier?.text,
isStatic: false,
offset: self.offset,
Expand Down Expand Up @@ -796,3 +796,14 @@ extension Trivia {
return nil
}
}

extension ThrowsClauseSyntax {

var text: String {
if let type {
"\(throwsSpecifier.text)(\(type.description))"
} else {
throwsSpecifier.text
}
}
}
27 changes: 24 additions & 3 deletions Sources/MockoloFramework/Utils/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,34 @@ extension String {
///
"""


var hasThrowsOrRethrows: Bool {
return components(separatedBy: .whitespaces).contains { component in
return component == .throws || component == .rethrows
}
let hasTypedThrow = hasPrefix(String.throws.withLeftParen)
return component == .throws || hasTypedThrow || component == .rethrows
}
}

/// Extract Error type in typed-throw.
///
/// - Note: Because any keyword can appear, it was hard to split by whitespace.
///
/// - ex
/// ```
/// throws(any LocalizedError)
/// ↓ should extract
/// any LocalizedError
/// ```
var typedThrowTypeName: String {
let pattern = #"throws\((?<thrownType>.+)\)"#
guard let regex = try? Regex(pattern, as: (Substring, thrownType: Substring).self) else {
return ""
}
guard let match = firstMatch(of: regex) else {
return ""
}
return String(match.output.thrownType)
}

var hasAsync: Bool {
return components(separatedBy: .whitespaces).contains { component in
return component == .async
Expand Down
11 changes: 10 additions & 1 deletion Sources/MockoloFramework/Utils/TypeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,18 @@ public final class `Type` {
displayableReturnType = "(\(displayableReturnType))"
}

let hasThrowsOrRethrows = suffix.hasThrowsOrRethrows
let typedThrowTypeName = suffix.typedThrowTypeName

let thrownSuffix: String = if typedThrowTypeName.isNotEmpty {
"\(String.throws)(\(typedThrowTypeName))"
} else {
String.throws
}

let suffixStr = [
suffix.hasAsync ? String.async + " " : nil,
suffix.hasThrowsOrRethrows ? String.throws + " " : nil,
hasThrowsOrRethrows ? thrownSuffix + " " : nil,
].compactMap { $0 }.joined()

let typeStr = "((\(displayableParamStr)) \(suffixStr)-> \(displayableReturnType))?"
Expand Down

0 comments on commit 4ca5407

Please sign in to comment.