1
- func replacingSelf( _ value: String ) -> String {
1
+ func replacingSelf( _ value: String , current : Current ) -> String {
2
2
return value
3
3
// TODO: proper regex here
4
4
// 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) > " )
12
12
// (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) ) " )
20
20
// literals
21
- . replacingOccurrences ( of: " [Self] " , with: " [ \( Current . selfType) ] " )
21
+ . replacingOccurrences ( of: " [Self] " , with: " [ \( current . selfType) ] " )
22
22
// 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) ? " )
28
28
// 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) ] " )
32
32
// 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) ? " )
41
41
}
42
42
43
43
class MethodWrapper {
@@ -47,16 +47,13 @@ class MethodWrapper {
47
47
. replacingOccurrences ( of: " ) " , with: " ) " )
48
48
return " Stub return value not specified for \( methodName) . Use given "
49
49
}
50
- private static var registered : [ String : Int ] = [ : ]
51
- private static var suffixes : [ String : Int ] = [ : ]
52
- private static var suffixesWithoutReturnType : [ String : Int ] = [ : ]
53
50
54
51
let method : SourceryRuntime . Method
55
52
var accessModifier : String {
56
53
guard !method. isStatic else { return " public static " }
57
54
guard !returnsGenericConstrainedToSelf else { return " public " }
58
55
guard !parametersContainsSelf else { return " public " }
59
- return Current . accessModifier
56
+ return current . accessModifier
60
57
}
61
58
var hasAvailability : Bool { method. attributes [ " available " ] ? . isEmpty == false }
62
59
var isAsync : Bool {
@@ -106,9 +103,9 @@ class MethodWrapper {
106
103
return " \( uniqueName) -> \( returnTypeStripped) "
107
104
}
108
105
private var nameSuffix : String {
109
- guard let count = MethodWrapper . registered [ registrationName] else { return " " }
106
+ guard let count = methodRegistrar . registered [ registrationName] else { return " " }
110
107
guard count > 1 else { return " " }
111
- guard let index = MethodWrapper . suffixes [ uniqueNameWithReturnType] else { return " " }
108
+ guard let index = methodRegistrar . suffixes [ uniqueNameWithReturnType] else { return " " }
112
109
return " _ \( index) "
113
110
}
114
111
private var methodAttributes : String {
@@ -122,7 +119,7 @@ class MethodWrapper {
122
119
return " \( registrationName) \( nameSuffix) " . replacingOccurrences ( of: " ` " , with: " " )
123
120
}
124
121
var parameters : [ ParameterWrapper ] {
125
- return filteredParameters. map { ParameterWrapper ( $0, self . getVariadicParametersNames ( ) ) }
122
+ return filteredParameters. map { ParameterWrapper ( $0, self . getVariadicParametersNames ( ) , current : current ) }
126
123
}
127
124
var filteredParameters : [ MethodParameter ] {
128
125
return method. parameters. filter { $0. name != " " }
@@ -139,7 +136,7 @@ class MethodWrapper {
139
136
} ( )
140
137
141
138
let staticModifier : String = " \( accessModifier) "
142
- let params = replacingSelf ( parametersForStubSignature ( ) )
139
+ let params = replacingSelf ( parametersForStubSignature ( ) , current : current )
143
140
var attributes = self . methodAttributes
144
141
attributes = attributes. isEmpty ? " " : " \( attributes) \n \t "
145
142
var asyncModifier = self . isAsync ? " async " : " "
@@ -175,7 +172,7 @@ class MethodWrapper {
175
172
guard method. throws || !method. returnTypeName. isVoid else { return " " }
176
173
177
174
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) "
179
176
180
177
if method. returnTypeName. isVoid {
181
178
return """
@@ -263,65 +260,42 @@ class MethodWrapper {
263
260
264
261
var returnsSelf : Bool {
265
262
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
267
264
}
268
265
var returnsGenericConstrainedToSelf : Bool {
269
266
let defaultReturnType = " \( method. returnTypeName. name) "
270
267
return defaultReturnType != returnTypeReplacingSelf
271
268
}
272
269
var returnTypeReplacingSelf : String {
273
- return replacingSelf ( " \( method. returnTypeName. name) " )
270
+ return replacingSelf ( " \( method. returnTypeName. name) " , current : current )
274
271
}
275
272
var parametersContainsSelf : Bool {
276
- return replacingSelf ( parametersForStubSignature ( ) ) != parametersForStubSignature ( )
273
+ return replacingSelf ( parametersForStubSignature ( ) , current : current ) != parametersForStubSignature ( )
277
274
}
278
275
276
+ let current : Current
277
+ let methodRegistrar : MethodRegistrar
278
+
279
279
var replaceSelf : String {
280
- return Current . selfType
280
+ return current . selfType
281
281
}
282
282
283
- init ( _ method: SourceryRuntime . Method ) {
283
+ init ( _ method: SourceryRuntime . Method , current : Current , methodRegistrar : MethodRegistrar ) {
284
284
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
292
287
}
293
288
294
289
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)
317
291
}
318
292
319
293
func wrappedInMethodType( ) -> Bool {
320
294
return !method. isInitializer
321
295
}
322
296
323
297
func returningParameter( _ multiple: Bool , _ front: Bool ) -> String {
324
- guard returnTypeMatters ( ) else { return " " }
298
+ guard methodRegistrar . returnTypeMatters ( uniqueName : uniqueName ) else { return " " }
325
299
let returning : String = " returning: \( returnTypeStripped ( method, type: true ) ) "
326
300
guard multiple else { return returning }
327
301
@@ -343,7 +317,7 @@ class MethodWrapper {
343
317
+ wrappedStubPostfix( )
344
318
}
345
319
} ( )
346
- return replacingSelf ( body)
320
+ return replacingSelf ( body, current : current )
347
321
}
348
322
349
323
func wrappedStubPrefix( ) -> String {
@@ -393,7 +367,7 @@ class MethodWrapper {
393
367
let returnTypeString : String = {
394
368
guard !returnsGenericConstrainedToSelf else { return returnTypeReplacingSelf }
395
369
guard !returnsSelf else { return replaceSelf }
396
- return TypeWrapper ( method. returnTypeName) . stripped
370
+ return TypeWrapper ( method. returnTypeName, current : current ) . stripped
397
371
} ( )
398
372
return returnTypeString
399
373
}
@@ -519,7 +493,7 @@ class MethodWrapper {
519
493
return " \( annotation) public static func \( methodName) ( \( parametersForProxySignature ( ) ) , \( returningParameter ( true , false ) ) perform: @escaping \( performProxyClosureType ( ) ) ) -> \( prefix) Perform \( genericConstrains) "
520
494
}
521
495
} ( )
522
- return replacingSelf ( body)
496
+ return replacingSelf ( body, current : current )
523
497
}
524
498
525
499
func performProxyConstructor( prefix: String = " " ) -> String {
@@ -547,7 +521,7 @@ class MethodWrapper {
547
521
} else {
548
522
let parameters = filteredParameters
549
523
. map { p in
550
- let wrapped = ParameterWrapper ( p, self . getVariadicParametersNames ( ) )
524
+ let wrapped = ParameterWrapper ( p, self . getVariadicParametersNames ( ) , current : current )
551
525
let isAutolosure = wrapped. justType. hasPrefix ( " @autoclosure " )
552
526
return " \( p. inout ? " & " : " " ) ` \( p. name) ` \( isAutolosure ? " () " : " " ) "
553
527
}
@@ -578,13 +552,13 @@ class MethodWrapper {
578
552
return parameters. map { param in
579
553
if param. isGeneric ( generics) { return param. genericType }
580
554
if availability { return param. typeErasedType }
581
- return replacingSelf ( param. nestedType)
555
+ return replacingSelf ( param. nestedType, current : current )
582
556
} . joined ( separator: " , " )
583
557
}
584
558
585
559
private func parametersForProxySignature( ) -> String {
586
560
return parameters. map { p in
587
- return " \( p. labelAndName ( ) ) : \( replacingSelf ( p. nestedType) ) "
561
+ return " \( p. labelAndName ( ) ) : \( replacingSelf ( p. nestedType, current : current ) ) "
588
562
} . joined ( separator: " , " )
589
563
}
590
564
@@ -635,13 +609,13 @@ class MethodWrapper {
635
609
/// - Returns: Array of strings, where each strings represent generic name
636
610
private func getGenericsWithoutConstraints( ) -> [ String ] {
637
611
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 [ ] }
639
613
640
614
var genPart = name [ start... end]
641
615
genPart. removeFirst ( )
642
616
genPart. removeLast ( )
643
617
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)
645
619
return parts. map { stripGenPart ( part: $0) }
646
620
}
647
621
@@ -650,13 +624,13 @@ class MethodWrapper {
650
624
/// - Returns: Array of strings, like ["T: Codable", "U: Whatever"]
651
625
private func getGenericsConstraints( _ generics: [ String ] , filterSingle: Bool = true ) -> [ String ] {
652
626
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 [ ] }
654
628
655
629
var genPart = name [ start... end]
656
630
genPart. removeFirst ( )
657
631
genPart. removeLast ( )
658
632
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)
660
634
return parts. filter {
661
635
let components = $0. components ( separatedBy: " : " )
662
636
return ( components. count == 2 || !filterSingle) && generics. contains ( components [ 0 ] )
@@ -678,7 +652,7 @@ class MethodWrapper {
678
652
}
679
653
680
654
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!
682
656
}
683
657
684
658
private func returnTypeStripped( _ method: SourceryRuntime . Method , type: Bool = false ) -> String {
@@ -714,10 +688,10 @@ class MethodWrapper {
714
688
715
689
private func methodInfo( ) -> ( annotation: String , methodName: String , genericConstrains: String ) {
716
690
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) ) "
718
692
let constraints : String = {
719
693
let constraints : [ String ]
720
- if returnTypeMatters ( ) {
694
+ if methodRegistrar . returnTypeMatters ( uniqueName : uniqueName ) {
721
695
constraints = whereClauseConstraints ( )
722
696
} else {
723
697
constraints = getGenericsConstraints ( generics)
0 commit comments