Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ReturnSelf] Allows Protocols to Return Self #118

Merged
merged 4 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/MockoloFramework/Models/ClosureModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ final class ClosureModel: Model {
}


init(name: String, genericTypeParams: [ParamModel], paramNames: [String], paramTypes: [Type], suffix: String, returnType: Type) {
init(name: String, genericTypeParams: [ParamModel], paramNames: [String], paramTypes: [Type], suffix: String, returnType: Type, encloser: String) {
self.name = name + .handlerSuffix
self.suffix = suffix
let genericTypeNameList = genericTypeParams.map(path: \.name)
self.genericTypeNames = genericTypeNameList
self.paramNames = paramNames
self.paramTypes = paramTypes
self.funcReturnType = returnType
self.type = Type.toClosureType(with: paramTypes, typeParams: genericTypeNameList, suffix: suffix, returnType: returnType)
self.type = Type.toClosureType(with: paramTypes, typeParams: genericTypeNameList, suffix: suffix, returnType: returnType, encloser: encloser)
}

func render(with identifier: String, encloser: String, useTemplateFunc: Bool = false, useMockObservable: Bool = false, enableFuncArgsHistory: Bool = false) -> String? {
Expand Down
9 changes: 5 additions & 4 deletions Sources/MockoloFramework/Models/MethodModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ final class MethodModel: Model {
return ret
}()

lazy var handler: ClosureModel? = {
func handler(encloser: String) -> ClosureModel? {
if isInitializer {
return nil
}
Expand All @@ -114,10 +114,11 @@ final class MethodModel: Model {
paramNames: paramNames,
paramTypes: paramTypes,
suffix: suffix,
returnType: type)
returnType: type,
encloser: encloser)

return ret
}()
}


init(name: String,
Expand Down Expand Up @@ -192,7 +193,7 @@ final class MethodModel: Model {
accessLevel: accessLevel,
suffix: suffix,
argsHistory: argsHistory,
handler: handler)
handler: handler(encloser: encloser))
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ extension ProtocolDeclSyntax: EntityNode {
var name: String {
return identifier.text
}

var accessLevel: String {
return self.modifiers?.acl ?? ""
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/MockoloFramework/Templates/MethodTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension MethodModel {
var template = ""

let returnTypeName = returnType.isUnknown ? "" : returnType.typeName

let acl = accessLevel.isEmpty ? "" : accessLevel+" "
let genericTypeDeclsStr = genericTypeParams.compactMap {$0.render(with: "", encloser: "")}.joined(separator: ", ")
let genericTypesStr = genericTypeDeclsStr.isEmpty ? "" : "<\(genericTypeDeclsStr)>"
Expand Down Expand Up @@ -142,4 +142,3 @@ extension MethodModel {
return template
}
}

1 change: 1 addition & 0 deletions Sources/MockoloFramework/Utils/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extension String {

static let `inout` = "inout"
static let hasBlankInit = "_hasBlankInit"
static let `Self` = "Self"
static let `static` = "static"
static let importSpace = "import "
static public let `class` = "class"
Expand Down
15 changes: 14 additions & 1 deletion Sources/MockoloFramework/Utils/TypeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public final class Type {
return typeName.hasPrefix(String.escaping)
}

var isSelf: Bool {
return typeName == String.`Self`
}

var splitByClosure: Bool {
let arg = typeName
if let closureOpRange = arg.range(of: String.closureArrow) {
Expand Down Expand Up @@ -499,14 +503,16 @@ public final class Type {
}


static func toClosureType(with params: [Type], typeParams: [String], suffix: String, returnType: Type) -> Type {
static func toClosureType(with params: [Type], typeParams: [String], suffix: String, returnType: Type, encloser: String) -> Type {


let displayableParamTypes = params.map { (subtype: Type) -> String in
return subtype.processTypeParams(with: typeParams)
}

let displayableParamStr = displayableParamTypes.joined(separator: ", ")
var displayableReturnType = returnType.typeName

let returnComps = displayableReturnType.literalComponents

var returnAsStr = ""
Expand All @@ -521,6 +527,8 @@ public final class Type {
} else if returnType.isIUO {
displayableReturnType = .any + "!"
returnAsStr.removeLast()
} else if returnType.isSelf {
returnAsStr = String.`Self`
} else {
displayableReturnType = .any
}
Expand All @@ -530,6 +538,11 @@ public final class Type {
}
}

if returnType.isSelf {
displayableReturnType = encloser
returnTypeCast = " as! " + String.`Self`
}

let isSimpleTuple = displayableReturnType.hasPrefix("(") && displayableReturnType.hasSuffix(")") &&
displayableReturnType.components(separatedBy: CharacterSet(charactersIn: "()")).filter({!$0.isEmpty}).count <= 1

Expand Down
Loading