diff --git a/Sources/MockoloFramework/Models/ThrowingKind.swift b/Sources/MockoloFramework/Models/ThrowingKind.swift index 397d3a4e..5277e5c8 100644 --- a/Sources/MockoloFramework/Models/ThrowingKind.swift +++ b/Sources/MockoloFramework/Models/ThrowingKind.swift @@ -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 @@ -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))" - } - } } diff --git a/Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift b/Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift index 4e887719..c4a2ac81 100644 --- a/Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift +++ b/Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift @@ -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) diff --git a/Sources/MockoloFramework/Templates/MethodTemplate.swift b/Sources/MockoloFramework/Templates/MethodTemplate.swift index 1ed05302..b1abada3 100644 --- a/Sources/MockoloFramework/Templates/MethodTemplate.swift +++ b/Sources/MockoloFramework/Templates/MethodTemplate.swift @@ -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 + " " : "" diff --git a/Sources/MockoloFramework/Templates/NominalTemplate.swift b/Sources/MockoloFramework/Templates/NominalTemplate.swift index 62080c00..e6f5cefb 100644 --- a/Sources/MockoloFramework/Templates/NominalTemplate.swift +++ b/Sources/MockoloFramework/Templates/NominalTemplate.swift @@ -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 { diff --git a/Sources/MockoloFramework/Templates/ThrowingKindTemplate.swift b/Sources/MockoloFramework/Templates/ThrowingKindTemplate.swift new file mode 100644 index 00000000..8ec73a9b --- /dev/null +++ b/Sources/MockoloFramework/Templates/ThrowingKindTemplate.swift @@ -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))" + } + } +} diff --git a/Sources/MockoloFramework/Templates/VariableTemplate.swift b/Sources/MockoloFramework/Templates/VariableTemplate.swift index a31366cd..0aaa9f97 100644 --- a/Sources/MockoloFramework/Templates/VariableTemplate.swift +++ b/Sources/MockoloFramework/Templates/VariableTemplate.swift @@ -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)} @@ -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() }