Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/issue-261-t…
Browse files Browse the repository at this point in the history
…yped-throw
  • Loading branch information
fummicc1 committed Oct 26, 2024
2 parents f4406dd + 8f458e2 commit fa5ccbb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
29 changes: 16 additions & 13 deletions Sources/MockoloFramework/Models/ThrowingKind.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
//
// Copyright (c) 2018. Uber Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

enum ThrowingKind: Equatable {
case none
case any
Expand All @@ -16,17 +32,4 @@ enum ThrowingKind: Equatable {
return errorType != .neverType && errorType != "Swift.\(String.neverType)"
}
}

var syntax: String? {
switch self {
case .none:
return nil
case .any:
return .throws
case .rethrows:
return .rethrows
case .typed(let errorType):
return "\(String.throws)(\(errorType))"
}
}
}
6 changes: 1 addition & 5 deletions Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,7 @@ extension VariableDeclSyntax {
getterEffects.isAsync = true
}
if let `throws` = getterDecl.effectSpecifiers?.throwsClause {
if let throwsType = `throws`.type {
getterEffects.throwing = .typed(errorType: throwsType.trimmedDescription)
} else {
getterEffects.throwing = .any
}
getterEffects.throwing = .init(`throws`)
}
if getterEffects == .empty {
storageType = .stored(needsSetCount: false)
Expand Down
2 changes: 1 addition & 1 deletion Sources/MockoloFramework/Templates/MethodTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extension MethodModel {

let suffixStr = [
isAsync ? String.async : nil,
throwing.syntax,
throwing.applyThrowingTemplate(),
].compactMap { $0 }.joined(separator: " ") + " "
let returnStr = returnTypeName.isEmpty ? "" : "-> \(returnTypeName)"
let staticStr = isStatic ? String.static + " " : ""
Expand Down
2 changes: 1 addition & 1 deletion Sources/MockoloFramework/Templates/NominalTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ extension NominalModel {
let paramDeclsStr = m.params.compactMap{$0.render(with: "", encloser: "")}.joined(separator: ", ")
let suffixStr = [
m.isAsync ? String.async : nil,
m.throwing.syntax,
m.throwing.applyThrowingTemplate(),
].compactMap { $0 }.joined(separator: " ") + " "

if override {
Expand Down
30 changes: 30 additions & 0 deletions Sources/MockoloFramework/Templates/ThrowingKindTemplate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright (c) 2018. Uber Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

extension ThrowingKind {
func applyThrowingTemplate() -> String? {
switch self {
case .none:
return nil
case .any:
return .throws
case .rethrows:
return .rethrows
case .typed(let errorType):
return "\(String.throws)(\(errorType))"
}
}
}
17 changes: 5 additions & 12 deletions Sources/MockoloFramework/Templates/VariableTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ extension VariableModel {

return """
\(1.tab)\(acl)\(staticSpace)var \(name)\(String.handlerSuffix): (() \(effects.syntax)-> \(type.typeName))?
\(1.tab)\(acl)\(staticSpace)var \(name)\(String.handlerSuffix): (() \(effects.applyTemplate())-> \(type.typeName))?
\(1.tab)\(acl)\(staticSpace)\(overrideStr)\(modifierTypeStr)var \(name): \(type.typeName) {
\(2.tab)get \(effects.syntax){
\(2.tab)get \(effects.applyTemplate()){
\(body)
\(2.tab)}
\(1.tab)}
Expand Down Expand Up @@ -356,20 +356,13 @@ extension VariableModel {
}

extension VariableModel.GetterEffects {
fileprivate var syntax: String {
fileprivate func applyTemplate() -> String {
var clauses: [String] = []
if isAsync {
clauses.append(.async)
}
switch throwing {
case .none:
break
case .any:
clauses.append(.throws)
case .rethrows:
clauses.append(.rethrows)
case .typed(let errorType):
clauses.append("\(String.throws)(\(errorType))")
if let throwSyntax = throwing.applyThrowingTemplate() {
clauses.append(throwSyntax)
}
return clauses.map { "\($0) " }.joined()
}
Expand Down

0 comments on commit fa5ccbb

Please sign in to comment.