Skip to content

Commit a3aa366

Browse files
art-divinRuslan Alikhamov
authored and
Ruslan Alikhamov
committed
Updated related classes
1 parent d0f1bd4 commit a3aa366

7 files changed

+160
-175
lines changed

Templates/Helpers.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class Helpers {
3535
}
3636
}
3737
static func extractGenericsList(_ associatedTypes: [String]?) -> [String] {
38-
return associatedTypes?.flatMap {
39-
split($0, byFirstOccurenceOf: " where ").0.replacingOccurrences(of: " ", with: "").characters.split(separator: ":").map(String.init).first
38+
return associatedTypes?.compactMap {
39+
split($0, byFirstOccurenceOf: " where ").0.replacingOccurrences(of: " ", with: "").split(separator: ":").map(String.init).first
4040
}.map { "\($0)" } ?? []
4141
}
4242
static func extractGenericTypesModifier(_ associatedTypes: [String]?) -> String {
@@ -46,11 +46,11 @@ class Helpers {
4646
}
4747
static func extractGenericTypesConstraints(_ associatedTypes: [String]?) -> String {
4848
guard let all = associatedTypes else { return "" }
49-
let constraints = all.flatMap { t -> String? in
49+
let constraints = all.compactMap { t -> String? in
5050
let splitted = split(t, byFirstOccurenceOf: " where ")
51-
let constraint = splitted.0.replacingOccurrences(of: " ", with: "").characters.split(separator: ":").map(String.init)
51+
let constraint = splitted.0.replacingOccurrences(of: " ", with: "").split(separator: ":").map(String.init)
5252
guard constraint.count == 2 else { return nil }
53-
let adopts = constraint[1].characters.split(separator: ",").map(String.init)
53+
let adopts = constraint[1].split(separator: ",").map(String.init)
5454
var mapped = adopts.map { "\(constraint[0]): \($0)" }
5555
if !splitted.1.isEmpty {
5656
mapped.append(splitted.1)
@@ -75,4 +75,4 @@ class Helpers {
7575
.sorted()
7676
.joined(separator: " ")
7777
}
78-
}
78+
}

Templates/MethodWrapper.swift

Lines changed: 63 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
func replacingSelf(_ value: String) -> String {
1+
func replacingSelf(_ value: String, current: Current) -> String {
22
return value
33
// TODO: proper regex here
44
// default < case >
5-
.replacingOccurrences(of: "<Self>", with: "<\(Current.selfType)>")
6-
.replacingOccurrences(of: "<Self ", with: "<\(Current.selfType) ")
7-
.replacingOccurrences(of: "<Self.", with: "<\(Current.selfType).")
8-
.replacingOccurrences(of: "<Self,", with: "<\(Current.selfType),")
9-
.replacingOccurrences(of: "<Self?", with: "<\(Current.selfType)?")
10-
.replacingOccurrences(of: " Self>", with: " \(Current.selfType)>")
11-
.replacingOccurrences(of: ",Self>", with: ",\(Current.selfType)>")
5+
.replacingOccurrences(of: "<Self>", with: "<\(current.selfType)>")
6+
.replacingOccurrences(of: "<Self ", with: "<\(current.selfType) ")
7+
.replacingOccurrences(of: "<Self.", with: "<\(current.selfType).")
8+
.replacingOccurrences(of: "<Self,", with: "<\(current.selfType),")
9+
.replacingOccurrences(of: "<Self?", with: "<\(current.selfType)?")
10+
.replacingOccurrences(of: " Self>", with: " \(current.selfType)>")
11+
.replacingOccurrences(of: ",Self>", with: ",\(current.selfType)>")
1212
// (Self) -> Case
13-
.replacingOccurrences(of: "(Self)", with: "(\(Current.selfType))")
14-
.replacingOccurrences(of: "(Self ", with: "(\(Current.selfType) ")
15-
.replacingOccurrences(of: "(Self.", with: "(\(Current.selfType).")
16-
.replacingOccurrences(of: "(Self,", with: "(\(Current.selfType),")
17-
.replacingOccurrences(of: "(Self?", with: "(\(Current.selfType)?")
18-
.replacingOccurrences(of: " Self)", with: " \(Current.selfType))")
19-
.replacingOccurrences(of: ",Self)", with: ",\(Current.selfType))")
13+
.replacingOccurrences(of: "(Self)", with: "(\(current.selfType))")
14+
.replacingOccurrences(of: "(Self ", with: "(\(current.selfType) ")
15+
.replacingOccurrences(of: "(Self.", with: "(\(current.selfType).")
16+
.replacingOccurrences(of: "(Self,", with: "(\(current.selfType),")
17+
.replacingOccurrences(of: "(Self?", with: "(\(current.selfType)?")
18+
.replacingOccurrences(of: " Self)", with: " \(current.selfType))")
19+
.replacingOccurrences(of: ",Self)", with: ",\(current.selfType))")
2020
// literals
21-
.replacingOccurrences(of: "[Self]", with: "[\(Current.selfType)]")
21+
.replacingOccurrences(of: "[Self]", with: "[\(current.selfType)]")
2222
// right
23-
.replacingOccurrences(of: "[Self ", with: "[\(Current.selfType) ")
24-
.replacingOccurrences(of: "[Self.", with: "[\(Current.selfType).")
25-
.replacingOccurrences(of: "[Self,", with: "[\(Current.selfType),")
26-
.replacingOccurrences(of: "[Self:", with: "[\(Current.selfType):")
27-
.replacingOccurrences(of: "[Self?", with: "[\(Current.selfType)?")
23+
.replacingOccurrences(of: "[Self ", with: "[\(current.selfType) ")
24+
.replacingOccurrences(of: "[Self.", with: "[\(current.selfType).")
25+
.replacingOccurrences(of: "[Self,", with: "[\(current.selfType),")
26+
.replacingOccurrences(of: "[Self:", with: "[\(current.selfType):")
27+
.replacingOccurrences(of: "[Self?", with: "[\(current.selfType)?")
2828
// left
29-
.replacingOccurrences(of: " Self]", with: " \(Current.selfType)]")
30-
.replacingOccurrences(of: ",Self]", with: ",\(Current.selfType)]")
31-
.replacingOccurrences(of: ":Self]", with: ":\(Current.selfType)]")
29+
.replacingOccurrences(of: " Self]", with: " \(current.selfType)]")
30+
.replacingOccurrences(of: ",Self]", with: ",\(current.selfType)]")
31+
.replacingOccurrences(of: ":Self]", with: ":\(current.selfType)]")
3232
// unknown
33-
.replacingOccurrences(of: " Self ", with: " \(Current.selfType) ")
34-
.replacingOccurrences(of: " Self.", with: " \(Current.selfType).")
35-
.replacingOccurrences(of: " Self,", with: " \(Current.selfType),")
36-
.replacingOccurrences(of: " Self:", with: " \(Current.selfType):")
37-
.replacingOccurrences(of: " Self?", with: " \(Current.selfType)?")
38-
.replacingOccurrences(of: ",Self ", with: ",\(Current.selfType) ")
39-
.replacingOccurrences(of: ",Self,", with: ",\(Current.selfType),")
40-
.replacingOccurrences(of: ",Self?", with: ",\(Current.selfType)?")
33+
.replacingOccurrences(of: " Self ", with: " \(current.selfType) ")
34+
.replacingOccurrences(of: " Self.", with: " \(current.selfType).")
35+
.replacingOccurrences(of: " Self,", with: " \(current.selfType),")
36+
.replacingOccurrences(of: " Self:", with: " \(current.selfType):")
37+
.replacingOccurrences(of: " Self?", with: " \(current.selfType)?")
38+
.replacingOccurrences(of: ",Self ", with: ",\(current.selfType) ")
39+
.replacingOccurrences(of: ",Self,", with: ",\(current.selfType),")
40+
.replacingOccurrences(of: ",Self?", with: ",\(current.selfType)?")
4141
}
4242

4343
class MethodWrapper {
@@ -47,16 +47,13 @@ class MethodWrapper {
4747
.replacingOccurrences(of: " )", with: ")")
4848
return "Stub return value not specified for \(methodName). Use given"
4949
}
50-
private static var registered: [String: Int] = [:]
51-
private static var suffixes: [String: Int] = [:]
52-
private static var suffixesWithoutReturnType: [String: Int] = [:]
5350

5451
let method: SourceryRuntime.Method
5552
var accessModifier: String {
5653
guard !method.isStatic else { return "public static" }
5754
guard !returnsGenericConstrainedToSelf else { return "public" }
5855
guard !parametersContainsSelf else { return "public" }
59-
return Current.accessModifier
56+
return current.accessModifier
6057
}
6158
var hasAvailability: Bool { method.attributes["available"]?.isEmpty == false }
6259
var isAsync: Bool {
@@ -106,9 +103,9 @@ class MethodWrapper {
106103
return "\(uniqueName)->\(returnTypeStripped)"
107104
}
108105
private var nameSuffix: String {
109-
guard let count = MethodWrapper.registered[registrationName] else { return "" }
106+
guard let count = methodRegistrar.registered[registrationName] else { return "" }
110107
guard count > 1 else { return "" }
111-
guard let index = MethodWrapper.suffixes[uniqueNameWithReturnType] else { return "" }
108+
guard let index = methodRegistrar.suffixes[uniqueNameWithReturnType] else { return "" }
112109
return "_\(index)"
113110
}
114111
private var methodAttributes: String {
@@ -122,7 +119,7 @@ class MethodWrapper {
122119
return "\(registrationName)\(nameSuffix)".replacingOccurrences(of: "`", with: "")
123120
}
124121
var parameters: [ParameterWrapper] {
125-
return filteredParameters.map { ParameterWrapper($0, self.getVariadicParametersNames()) }
122+
return filteredParameters.map { ParameterWrapper($0, self.getVariadicParametersNames(), current: current) }
126123
}
127124
var filteredParameters: [MethodParameter] {
128125
return method.parameters.filter { $0.name != "" }
@@ -139,7 +136,7 @@ class MethodWrapper {
139136
}()
140137

141138
let staticModifier: String = "\(accessModifier) "
142-
let params = replacingSelf(parametersForStubSignature())
139+
let params = replacingSelf(parametersForStubSignature(), current: current)
143140
var attributes = self.methodAttributes
144141
attributes = attributes.isEmpty ? "" : "\(attributes)\n\t"
145142
var asyncModifier = self.isAsync ? "async " : ""
@@ -175,7 +172,7 @@ class MethodWrapper {
175172
guard method.throws || !method.returnTypeName.isVoid else { return "" }
176173

177174
let methodType = filteredParameters.isEmpty ? ".\(prototype)" : ".\(prototype)(\(parametersForMethodCall()))"
178-
let returnType: String = returnsSelf ? "__Self__" : "\(TypeWrapper(method.returnTypeName).stripped)"
175+
let returnType: String = returnsSelf ? "__Self__" : "\(TypeWrapper(method.returnTypeName, current: current).stripped)"
179176

180177
if method.returnTypeName.isVoid {
181178
return """
@@ -263,65 +260,42 @@ class MethodWrapper {
263260

264261
var returnsSelf: Bool {
265262
guard !returnsGenericConstrainedToSelf else { return true }
266-
return !method.returnTypeName.isVoid && TypeWrapper(method.returnTypeName).isSelfType
263+
return !method.returnTypeName.isVoid && TypeWrapper(method.returnTypeName, current: current).isSelfType
267264
}
268265
var returnsGenericConstrainedToSelf: Bool {
269266
let defaultReturnType = "\(method.returnTypeName.name) "
270267
return defaultReturnType != returnTypeReplacingSelf
271268
}
272269
var returnTypeReplacingSelf: String {
273-
return replacingSelf("\(method.returnTypeName.name) ")
270+
return replacingSelf("\(method.returnTypeName.name) ", current: current)
274271
}
275272
var parametersContainsSelf: Bool {
276-
return replacingSelf(parametersForStubSignature()) != parametersForStubSignature()
273+
return replacingSelf(parametersForStubSignature(), current: current) != parametersForStubSignature()
277274
}
278275

276+
let current: Current
277+
let methodRegistrar: MethodRegistrar
278+
279279
var replaceSelf: String {
280-
return Current.selfType
280+
return current.selfType
281281
}
282282

283-
init(_ method: SourceryRuntime.Method) {
283+
init(_ method: SourceryRuntime.Method, current: Current, methodRegistrar: MethodRegistrar) {
284284
self.method = method
285-
}
286-
287-
public static func clear() -> String {
288-
MethodWrapper.registered = [:]
289-
MethodWrapper.suffixes = [:]
290-
MethodWrapper.suffixesWithoutReturnType = [:]
291-
return ""
285+
self.current = current
286+
self.methodRegistrar = methodRegistrar
292287
}
293288

294289
func register() {
295-
MethodWrapper.register(registrationName,uniqueName,uniqueNameWithReturnType)
296-
}
297-
298-
static func register(_ name: String, _ uniqueName: String, _ uniqueNameWithReturnType: String) {
299-
if let count = MethodWrapper.registered[name] {
300-
MethodWrapper.registered[name] = count + 1
301-
MethodWrapper.suffixes[uniqueNameWithReturnType] = count + 1
302-
} else {
303-
MethodWrapper.registered[name] = 1
304-
MethodWrapper.suffixes[uniqueNameWithReturnType] = 1
305-
}
306-
307-
if let count = MethodWrapper.suffixesWithoutReturnType[uniqueName] {
308-
MethodWrapper.suffixesWithoutReturnType[uniqueName] = count + 1
309-
} else {
310-
MethodWrapper.suffixesWithoutReturnType[uniqueName] = 1
311-
}
312-
}
313-
314-
func returnTypeMatters() -> Bool {
315-
let count = MethodWrapper.suffixesWithoutReturnType[uniqueName] ?? 0
316-
return count > 1
290+
methodRegistrar.register(registrationName,uniqueName,uniqueNameWithReturnType)
317291
}
318292

319293
func wrappedInMethodType() -> Bool {
320294
return !method.isInitializer
321295
}
322296

323297
func returningParameter(_ multiple: Bool, _ front: Bool) -> String {
324-
guard returnTypeMatters() else { return "" }
298+
guard methodRegistrar.returnTypeMatters(uniqueName: uniqueName) else { return "" }
325299
let returning: String = "returning: \(returnTypeStripped(method, type: true))"
326300
guard multiple else { return returning }
327301

@@ -343,7 +317,7 @@ class MethodWrapper {
343317
+ wrappedStubPostfix()
344318
}
345319
}()
346-
return replacingSelf(body)
320+
return replacingSelf(body, current: current)
347321
}
348322

349323
func wrappedStubPrefix() -> String {
@@ -393,7 +367,7 @@ class MethodWrapper {
393367
let returnTypeString: String = {
394368
guard !returnsGenericConstrainedToSelf else { return returnTypeReplacingSelf }
395369
guard !returnsSelf else { return replaceSelf }
396-
return TypeWrapper(method.returnTypeName).stripped
370+
return TypeWrapper(method.returnTypeName, current: current).stripped
397371
}()
398372
return returnTypeString
399373
}
@@ -519,7 +493,7 @@ class MethodWrapper {
519493
return "\(annotation)public static func \(methodName)(\(parametersForProxySignature()), \(returningParameter(true,false))perform: @escaping \(performProxyClosureType())) -> \(prefix)Perform\(genericConstrains)"
520494
}
521495
}()
522-
return replacingSelf(body)
496+
return replacingSelf(body, current: current)
523497
}
524498

525499
func performProxyConstructor(prefix: String = "") -> String {
@@ -547,7 +521,7 @@ class MethodWrapper {
547521
} else {
548522
let parameters = filteredParameters
549523
.map { p in
550-
let wrapped = ParameterWrapper(p, self.getVariadicParametersNames())
524+
let wrapped = ParameterWrapper(p, self.getVariadicParametersNames(), current: current)
551525
let isAutolosure = wrapped.justType.hasPrefix("@autoclosure")
552526
return "\(p.inout ? "&" : "")`\(p.name)`\(isAutolosure ? "()" : "")"
553527
}
@@ -578,13 +552,13 @@ class MethodWrapper {
578552
return parameters.map { param in
579553
if param.isGeneric(generics) { return param.genericType }
580554
if availability { return param.typeErasedType }
581-
return replacingSelf(param.nestedType)
555+
return replacingSelf(param.nestedType, current: current)
582556
}.joined(separator: ", ")
583557
}
584558

585559
private func parametersForProxySignature() -> String {
586560
return parameters.map { p in
587-
return "\(p.labelAndName()): \(replacingSelf(p.nestedType))"
561+
return "\(p.labelAndName()): \(replacingSelf(p.nestedType, current: current))"
588562
}.joined(separator: ", ")
589563
}
590564

@@ -635,13 +609,13 @@ class MethodWrapper {
635609
/// - Returns: Array of strings, where each strings represent generic name
636610
private func getGenericsWithoutConstraints() -> [String] {
637611
let name = method.shortName
638-
guard let start = name.index(of: "<"), let end = name.index(of: ">") else { return [] }
612+
guard let start = name.firstIndex(of: "<"), let end = name.firstIndex(of: ">") else { return [] }
639613

640614
var genPart = name[start...end]
641615
genPart.removeFirst()
642616
genPart.removeLast()
643617

644-
let parts = genPart.replacingOccurrences(of: " ", with: "").characters.split(separator: ",").map(String.init)
618+
let parts = genPart.replacingOccurrences(of: " ", with: "").split(separator: ",").map(String.init)
645619
return parts.map { stripGenPart(part: $0) }
646620
}
647621

@@ -650,13 +624,13 @@ class MethodWrapper {
650624
/// - Returns: Array of strings, like ["T: Codable", "U: Whatever"]
651625
private func getGenericsConstraints(_ generics: [String], filterSingle: Bool = true) -> [String] {
652626
let name = method.shortName
653-
guard let start = name.index(of: "<"), let end = name.index(of: ">") else { return [] }
627+
guard let start = name.firstIndex(of: "<"), let end = name.firstIndex(of: ">") else { return [] }
654628

655629
var genPart = name[start...end]
656630
genPart.removeFirst()
657631
genPart.removeLast()
658632

659-
let parts = genPart.replacingOccurrences(of: " ", with: "").characters.split(separator: ",").map(String.init)
633+
let parts = genPart.replacingOccurrences(of: " ", with: "").split(separator: ",").map(String.init)
660634
return parts.filter {
661635
let components = $0.components(separatedBy: ":")
662636
return (components.count == 2 || !filterSingle) && generics.contains(components[0])
@@ -678,7 +652,7 @@ class MethodWrapper {
678652
}
679653

680654
private func stripGenPart(part: String) -> String {
681-
return part.characters.split(separator: ":").map(String.init).first!
655+
return part.split(separator: ":").map(String.init).first!
682656
}
683657

684658
private func returnTypeStripped(_ method: SourceryRuntime.Method, type: Bool = false) -> String {
@@ -714,10 +688,10 @@ class MethodWrapper {
714688

715689
private func methodInfo() -> (annotation: String, methodName: String, genericConstrains: String) {
716690
let generics = getGenericsAmongParameters()
717-
let methodName = returnTypeMatters() ? method.shortName : "\(method.callName)\(wrapGenerics(generics))"
691+
let methodName = methodRegistrar.returnTypeMatters(uniqueName: uniqueName) ? method.shortName : "\(method.callName)\(wrapGenerics(generics))"
718692
let constraints: String = {
719693
let constraints: [String]
720-
if returnTypeMatters() {
694+
if methodRegistrar.returnTypeMatters(uniqueName: uniqueName) {
721695
constraints = whereClauseConstraints()
722696
} else {
723697
constraints = getGenericsConstraints(generics)

Templates/ParameterWrapper.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class ParameterWrapper {
22
let parameter: MethodParameter
33

44
var isVariadic = false
5+
let current: Current
56

67
var wrappedForCall: String {
78
let typeString = "\(type.actualTypeName ?? type)"
@@ -14,13 +15,13 @@ class ParameterWrapper {
1415
}
1516
}
1617
var nestedType: String {
17-
return "\(TypeWrapper(type, isVariadic).nestedParameter)"
18+
return "\(TypeWrapper(type, isVariadic, current: current).nestedParameter)"
1819
}
1920
var justType: String {
20-
return "\(TypeWrapper(type, isVariadic).replacingSelf())"
21+
return "\(TypeWrapper(type, isVariadic, current: current).replacingSelf())"
2122
}
2223
var justPerformType: String {
23-
return "\(TypeWrapper(type, isVariadic).replacingSelfRespectingVariadic())".replacingOccurrences(of: "!", with: "?")
24+
return "\(TypeWrapper(type, isVariadic, current: current).replacingSelfRespectingVariadic())".replacingOccurrences(of: "!", with: "?")
2425
}
2526
var genericType: String {
2627
return isVariadic ? "Parameter<[GenericAttribute]>" : "Parameter<GenericAttribute>"
@@ -46,13 +47,14 @@ class ParameterWrapper {
4647
return "results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: \(lhsName), rhs: \(rhsName), with: matcher), \(lhsName), \(rhsName), \"\(labelAndName())\"))"
4748
}
4849

49-
init(_ parameter: SourceryRuntime.MethodParameter, _ variadics: [String] = []) {
50+
init(_ parameter: SourceryRuntime.MethodParameter, _ variadics: [String] = [], current: Current) {
5051
self.parameter = parameter
5152
self.isVariadic = !variadics.isEmpty && variadics.contains(parameter.name)
53+
self.current = current
5254
}
5355

5456
func isGeneric(_ types: [String]) -> Bool {
55-
return TypeWrapper(type).isGeneric(types)
57+
return TypeWrapper(type, current: current).isGeneric(types)
5658
}
5759

5860
func wrappedForProxy(_ generics: [String], _ availability: Bool = false) -> String {
@@ -92,4 +94,4 @@ class ParameterWrapper {
9294
return "\(parameter.name)".replacingOccurrences(of: "`", with: "")
9395
}
9496
}
95-
}
97+
}

0 commit comments

Comments
 (0)