Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
fummicc1 committed Jun 30, 2024
1 parent 4ca5407 commit bbb6de0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 62 deletions.
10 changes: 2 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ let package = Package(
.library(name: "MockoloFramework", targets: ["MockoloFramework"]),
],
dependencies: [
.package(
url: "https://github.com/swiftlang/swift-syntax",
from: "600.0.0-prerelease-2024-06-12"
),
.package(
url: "https://github.com/apple/swift-argument-parser",
from: "1.2.2"
),
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.0-prerelease-2024-06-12"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"),
],
targets: [
.executableTarget(
Expand Down
22 changes: 11 additions & 11 deletions Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,17 @@ extension TypeAliasDeclSyntax {
}
}

extension ThrowsClauseSyntax {

var text: String {
if let type {
"\(throwsSpecifier.text)(\(type.description))"
} else {
throwsSpecifier.text
}
}
}

final class EntityVisitor: SyntaxVisitor {
var entities: [Entity] = []
var imports: [String: [String]] = [:]
Expand Down Expand Up @@ -796,14 +807,3 @@ extension Trivia {
return nil
}
}

extension ThrowsClauseSyntax {

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

var hasThrowsOrRethrows: Bool {
return components(separatedBy: .whitespaces).contains { component in
let hasTypedThrow = hasPrefix(String.throws.withLeftParen)
return component == .throws || hasTypedThrow || 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)
}
/// 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
Expand Down
16 changes: 8 additions & 8 deletions Sources/MockoloFramework/Utils/TypeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -558,18 +558,18 @@ public final class `Type` {
displayableReturnType = "(\(displayableReturnType))"
}

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

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

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

let typeStr = "((\(displayableParamStr)) \(suffixStr)-> \(displayableReturnType))?"
Expand Down
12 changes: 6 additions & 6 deletions Tests/TestFuncs/TestFuncThrow/FuncThrowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class FuncThrowTests: MockoloTestCase {
dstContent: funcThrowMock)
}

func testTypedThrows() {
verify(
srcContent: funcTypedThrow,
dstContent: funcTypedThrowMock
)
}
func testTypedThrows() {
verify(
srcContent: funcTypedThrow,
dstContent: funcTypedThrowMock
)
}
}
12 changes: 6 additions & 6 deletions Tests/TestInit/InitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class InitTests: MockoloTestCase {
)
}

func testThrowableInit() {
verify(
srcContent: throwableInit,
dstContent: throwableInitMock
)
}
func testThrowableInit() {
verify(
srcContent: throwableInit,
dstContent: throwableInitMock
)
}
}

0 comments on commit bbb6de0

Please sign in to comment.