-
-
Notifications
You must be signed in to change notification settings - Fork 603
/
SourceryRuntime.content.generated.swift
7325 lines (6402 loc) · 299 KB
/
SourceryRuntime.content.generated.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let sourceryRuntimeFiles: [FolderSynchronizer.File] = [
.init(name: "AccessLevel.swift", content:
"""
//
// Created by Krzysztof Zablocki on 13/09/2016.
// Copyright (c) 2016 Pixle. All rights reserved.
//
import Foundation
/// :nodoc:
public enum AccessLevel: String {
case `internal` = "internal"
case `private` = "private"
case `fileprivate` = "fileprivate"
case `public` = "public"
case `open` = "open"
case none = ""
}
"""),
.init(name: "Actor.swift", content:
"""
import Foundation
// sourcery: skipDescription
/// Descibes Swift actor
@objc(SwiftActor) @objcMembers public final class Actor: Type {
/// Returns "actor"
public override var kind: String { return "actor" }
/// Whether type is final
public var isFinal: Bool {
return modifiers.contains { $0.name == "final" }
}
/// :nodoc:
public override init(name: String = "",
parent: Type? = nil,
accessLevel: AccessLevel = .internal,
isExtension: Bool = false,
variables: [Variable] = [],
methods: [Method] = [],
subscripts: [Subscript] = [],
inheritedTypes: [String] = [],
containedTypes: [Type] = [],
typealiases: [Typealias] = [],
attributes: AttributeList = [:],
modifiers: [SourceryModifier] = [],
annotations: [String: NSObject] = [:],
documentation: [String] = [],
isGeneric: Bool = false) {
super.init(
name: name,
parent: parent,
accessLevel: accessLevel,
isExtension: isExtension,
variables: variables,
methods: methods,
subscripts: subscripts,
inheritedTypes: inheritedTypes,
containedTypes: containedTypes,
typealiases: typealiases,
attributes: attributes,
modifiers: modifiers,
annotations: annotations,
documentation: documentation,
isGeneric: isGeneric
)
}
// sourcery:inline:Actor.AutoCoding
/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
/// :nodoc:
override public func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
}
// sourcery:end
}
"""),
.init(name: "Annotations.swift", content:
"""
import Foundation
public typealias Annotations = [String: NSObject]
/// Describes annotated declaration, i.e. type, method, variable, enum case
public protocol Annotated {
/**
All annotations of declaration stored by their name. Value can be `bool`, `String`, float `NSNumber`
or array of those types if you use several annotations with the same name.
**Example:**
```
//sourcery: booleanAnnotation
//sourcery: stringAnnotation = "value"
//sourcery: numericAnnotation = 0.5
[
"booleanAnnotation": true,
"stringAnnotation": "value",
"numericAnnotation": 0.5
]
```
*/
var annotations: Annotations { get }
}
"""),
.init(name: "Array+Parallel.swift", content:
"""
//
// Created by Krzysztof Zablocki on 06/01/2017.
// Copyright (c) 2017 Pixle. All rights reserved.
//
import Foundation
public extension Array {
func parallelFlatMap<T>(transform: (Element) -> [T]) -> [T] {
return parallelMap(transform: transform).flatMap { $0 }
}
func parallelCompactMap<T>(transform: (Element) -> T?) -> [T] {
return parallelMap(transform: transform).compactMap { $0 }
}
func parallelMap<T>(transform: (Element) -> T) -> [T] {
var result = ContiguousArray<T?>(repeating: nil, count: count)
return result.withUnsafeMutableBufferPointer { buffer in
DispatchQueue.concurrentPerform(iterations: buffer.count) { idx in
buffer[idx] = transform(self[idx])
}
return buffer.map { $0! }
}
}
func parallelPerform(transform: (Element) -> Void) {
DispatchQueue.concurrentPerform(iterations: count) { idx in
transform(self[idx])
}
}
}
"""),
.init(name: "Array.swift", content:
"""
import Foundation
/// Describes array type
@objcMembers public final class ArrayType: NSObject, SourceryModel {
/// Type name used in declaration
public var name: String
/// Array element type name
public var elementTypeName: TypeName
// sourcery: skipEquality, skipDescription
/// Array element type, if known
public var elementType: Type?
/// :nodoc:
public init(name: String, elementTypeName: TypeName, elementType: Type? = nil) {
self.name = name
self.elementTypeName = elementTypeName
self.elementType = elementType
}
/// Returns array as generic type
public var asGeneric: GenericType {
GenericType(name: "Array", typeParameters: [
.init(typeName: elementTypeName)
])
}
public var asSource: String {
"[\\(elementTypeName.asSource)]"
}
// sourcery:inline:ArrayType.AutoCoding
/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
guard let name: String = aDecoder.decode(forKey: "name") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["name"])); fatalError() }; self.name = name
guard let elementTypeName: TypeName = aDecoder.decode(forKey: "elementTypeName") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["elementTypeName"])); fatalError() }; self.elementTypeName = elementTypeName
self.elementType = aDecoder.decode(forKey: "elementType")
}
/// :nodoc:
public func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: "name")
aCoder.encode(self.elementTypeName, forKey: "elementTypeName")
aCoder.encode(self.elementType, forKey: "elementType")
}
// sourcery:end
}
"""),
.init(name: "AssociatedType.swift", content:
"""
import Foundation
/// Describes Swift AssociatedType
@objcMembers public final class AssociatedType: NSObject, SourceryModel {
/// Associated type name
public let name: String
/// Associated type type constraint name, if specified
public let typeName: TypeName?
// sourcery: skipEquality, skipDescription
/// Associated type constrained type, if known, i.e. if the type is declared in the scanned sources.
public var type: Type?
/// :nodoc:
public init(name: String, typeName: TypeName? = nil, type: Type? = nil) {
self.name = name
self.typeName = typeName
self.type = type
}
// sourcery:inline:AssociatedType.AutoCoding
/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
guard let name: String = aDecoder.decode(forKey: "name") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["name"])); fatalError() }; self.name = name
self.typeName = aDecoder.decode(forKey: "typeName")
self.type = aDecoder.decode(forKey: "type")
}
/// :nodoc:
public func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: "name")
aCoder.encode(self.typeName, forKey: "typeName")
aCoder.encode(self.type, forKey: "type")
}
// sourcery:end
}
"""),
.init(name: "Attribute.swift", content:
"""
import Foundation
/// Describes Swift attribute
@objcMembers public class Attribute: NSObject, AutoCoding, AutoEquatable, AutoDiffable, AutoJSExport {
/// Attribute name
public let name: String
/// Attribute arguments
public let arguments: [String: NSObject]
// sourcery: skipJSExport
let _description: String
// sourcery: skipEquality, skipDescription, skipCoding, skipJSExport
/// :nodoc:
public var __parserData: Any?
/// :nodoc:
public init(name: String, arguments: [String: NSObject] = [:], description: String? = nil) {
self.name = name
self.arguments = arguments
self._description = description ?? "@\\(name)"
}
/// TODO: unify `asSource` / `description`?
public var asSource: String {
description
}
/// Attribute description that can be used in a template.
public override var description: String {
return _description
}
/// :nodoc:
public enum Identifier: String {
case convenience
case required
case available
case discardableResult
case GKInspectable = "gkinspectable"
case objc
case objcMembers
case nonobjc
case NSApplicationMain
case NSCopying
case NSManaged
case UIApplicationMain
case IBOutlet = "iboutlet"
case IBInspectable = "ibinspectable"
case IBDesignable = "ibdesignable"
case autoclosure
case convention
case mutating
case escaping
case final
case open
case lazy
case `public` = "public"
case `internal` = "internal"
case `private` = "private"
case `fileprivate` = "fileprivate"
case publicSetter = "setter_access.public"
case internalSetter = "setter_access.internal"
case privateSetter = "setter_access.private"
case fileprivateSetter = "setter_access.fileprivate"
case optional
public init?(identifier: String) {
let identifier = identifier.trimmingPrefix("source.decl.attribute.")
if identifier == "objc.name" {
self.init(rawValue: "objc")
} else {
self.init(rawValue: identifier)
}
}
public static func from(string: String) -> Identifier? {
switch string {
case "GKInspectable":
return Identifier.GKInspectable
case "objc":
return .objc
case "IBOutlet":
return .IBOutlet
case "IBInspectable":
return .IBInspectable
case "IBDesignable":
return .IBDesignable
default:
return Identifier(rawValue: string)
}
}
public var name: String {
switch self {
case .GKInspectable:
return "GKInspectable"
case .objc:
return "objc"
case .IBOutlet:
return "IBOutlet"
case .IBInspectable:
return "IBInspectable"
case .IBDesignable:
return "IBDesignable"
case .fileprivateSetter:
return "fileprivate"
case .privateSetter:
return "private"
case .internalSetter:
return "internal"
case .publicSetter:
return "public"
default:
return rawValue
}
}
public var description: String {
return hasAtPrefix ? "@\\(name)" : name
}
public var hasAtPrefix: Bool {
switch self {
case .available,
.discardableResult,
.GKInspectable,
.objc,
.objcMembers,
.nonobjc,
.NSApplicationMain,
.NSCopying,
.NSManaged,
.UIApplicationMain,
.IBOutlet,
.IBInspectable,
.IBDesignable,
.autoclosure,
.convention,
.escaping:
return true
default:
return false
}
}
}
// sourcery:inline:Attribute.AutoCoding
/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
guard let name: String = aDecoder.decode(forKey: "name") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["name"])); fatalError() }; self.name = name
guard let arguments: [String: NSObject] = aDecoder.decode(forKey: "arguments") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["arguments"])); fatalError() }; self.arguments = arguments
guard let _description: String = aDecoder.decode(forKey: "_description") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["_description"])); fatalError() }; self._description = _description
}
/// :nodoc:
public func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: "name")
aCoder.encode(self.arguments, forKey: "arguments")
aCoder.encode(self._description, forKey: "_description")
}
// sourcery:end
}
"""),
.init(name: "AutoHashable.generated.swift", content:
"""
// Generated using Sourcery 1.3.1 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
// swiftlint:disable all
// MARK: - AutoHashable for classes, protocols, structs
// MARK: - AutoHashable for Enums
"""),
.init(name: "BytesRange.swift", content:
"""
//
// Created by Sébastien Duperron on 03/01/2018.
// Copyright © 2018 Pixle. All rights reserved.
//
import Foundation
/// :nodoc:
@objcMembers public final class BytesRange: NSObject, SourceryModel {
public let offset: Int64
public let length: Int64
public init(offset: Int64, length: Int64) {
self.offset = offset
self.length = length
}
public convenience init(range: (offset: Int64, length: Int64)) {
self.init(offset: range.offset, length: range.length)
}
// sourcery:inline:BytesRange.AutoCoding
/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
self.offset = aDecoder.decodeInt64(forKey: "offset")
self.length = aDecoder.decodeInt64(forKey: "length")
}
/// :nodoc:
public func encode(with aCoder: NSCoder) {
aCoder.encode(self.offset, forKey: "offset")
aCoder.encode(self.length, forKey: "length")
}
// sourcery:end
}
"""),
.init(name: "Class.swift", content:
"""
import Foundation
// sourcery: skipDescription
/// Descibes Swift class
@objc(SwiftClass) @objcMembers public final class Class: Type {
/// Returns "class"
public override var kind: String { return "class" }
/// Whether type is final
public var isFinal: Bool {
return modifiers.contains { $0.name == "final" }
}
/// :nodoc:
public override init(name: String = "",
parent: Type? = nil,
accessLevel: AccessLevel = .internal,
isExtension: Bool = false,
variables: [Variable] = [],
methods: [Method] = [],
subscripts: [Subscript] = [],
inheritedTypes: [String] = [],
containedTypes: [Type] = [],
typealiases: [Typealias] = [],
attributes: AttributeList = [:],
modifiers: [SourceryModifier] = [],
annotations: [String: NSObject] = [:],
documentation: [String] = [],
isGeneric: Bool = false) {
super.init(
name: name,
parent: parent,
accessLevel: accessLevel,
isExtension: isExtension,
variables: variables,
methods: methods,
subscripts: subscripts,
inheritedTypes: inheritedTypes,
containedTypes: containedTypes,
typealiases: typealiases,
attributes: attributes,
modifiers: modifiers,
annotations: annotations,
documentation: documentation,
isGeneric: isGeneric
)
}
// sourcery:inline:Class.AutoCoding
/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
/// :nodoc:
override public func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
}
// sourcery:end
}
"""),
.init(name: "Closure.swift", content:
"""
import Foundation
/// Describes closure type
@objcMembers public final class ClosureType: NSObject, SourceryModel {
/// Type name used in declaration with stripped whitespaces and new lines
public let name: String
/// List of closure parameters
public let parameters: [ClosureParameter]
/// Return value type name
public let returnTypeName: TypeName
/// Actual return value type name if declaration uses typealias, otherwise just a `returnTypeName`
public var actualReturnTypeName: TypeName {
return returnTypeName.actualTypeName ?? returnTypeName
}
// sourcery: skipEquality, skipDescription
/// Actual return value type, if known
public var returnType: Type?
// sourcery: skipEquality, skipDescription
/// Whether return value type is optional
public var isOptionalReturnType: Bool {
return returnTypeName.isOptional
}
// sourcery: skipEquality, skipDescription
/// Whether return value type is implicitly unwrapped optional
public var isImplicitlyUnwrappedOptionalReturnType: Bool {
return returnTypeName.isImplicitlyUnwrappedOptional
}
// sourcery: skipEquality, skipDescription
/// Return value type name without attributes and optional type information
public var unwrappedReturnTypeName: String {
return returnTypeName.unwrappedTypeName
}
/// Whether method is async method
public let isAsync: Bool
/// async keyword
public let asyncKeyword: String?
/// Whether closure throws
public let `throws`: Bool
/// throws or rethrows keyword
public let throwsOrRethrowsKeyword: String?
/// :nodoc:
public init(name: String, parameters: [ClosureParameter], returnTypeName: TypeName, returnType: Type? = nil, asyncKeyword: String? = nil, throwsOrRethrowsKeyword: String? = nil) {
self.name = name
self.parameters = parameters
self.returnTypeName = returnTypeName
self.returnType = returnType
self.asyncKeyword = asyncKeyword
self.isAsync = asyncKeyword != nil
self.throwsOrRethrowsKeyword = throwsOrRethrowsKeyword
self.`throws` = throwsOrRethrowsKeyword != nil
}
public var asSource: String {
"\\(parameters.asSource)\\(asyncKeyword != nil ? " \\(asyncKeyword!)" : "")\\(throwsOrRethrowsKeyword != nil ? " \\(throwsOrRethrowsKeyword!)" : "") -> \\(returnTypeName.asSource)"
}
// sourcery:inline:ClosureType.AutoCoding
/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
guard let name: String = aDecoder.decode(forKey: "name") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["name"])); fatalError() }; self.name = name
guard let parameters: [ClosureParameter] = aDecoder.decode(forKey: "parameters") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["parameters"])); fatalError() }; self.parameters = parameters
guard let returnTypeName: TypeName = aDecoder.decode(forKey: "returnTypeName") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["returnTypeName"])); fatalError() }; self.returnTypeName = returnTypeName
self.returnType = aDecoder.decode(forKey: "returnType")
self.isAsync = aDecoder.decode(forKey: "isAsync")
self.asyncKeyword = aDecoder.decode(forKey: "asyncKeyword")
self.`throws` = aDecoder.decode(forKey: "`throws`")
self.throwsOrRethrowsKeyword = aDecoder.decode(forKey: "throwsOrRethrowsKeyword")
}
/// :nodoc:
public func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: "name")
aCoder.encode(self.parameters, forKey: "parameters")
aCoder.encode(self.returnTypeName, forKey: "returnTypeName")
aCoder.encode(self.returnType, forKey: "returnType")
aCoder.encode(self.isAsync, forKey: "isAsync")
aCoder.encode(self.asyncKeyword, forKey: "asyncKeyword")
aCoder.encode(self.`throws`, forKey: "`throws`")
aCoder.encode(self.throwsOrRethrowsKeyword, forKey: "throwsOrRethrowsKeyword")
}
// sourcery:end
}
"""),
.init(name: "Coding.generated.swift", content:
"""
// Generated using Sourcery 2.0.1 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
// swiftlint:disable vertical_whitespace trailing_newline
import Foundation
extension NSCoder {
@nonobjc func decode(forKey: String) -> String? {
return self.maybeDecode(forKey: forKey) as String?
}
@nonobjc func decode(forKey: String) -> TypeName? {
return self.maybeDecode(forKey: forKey) as TypeName?
}
@nonobjc func decode(forKey: String) -> AccessLevel? {
return self.maybeDecode(forKey: forKey) as AccessLevel?
}
@nonobjc func decode(forKey: String) -> Bool {
return self.decodeBool(forKey: forKey)
}
@nonobjc func decode(forKey: String) -> Int {
return self.decodeInteger(forKey: forKey)
}
func decode<E>(forKey: String) -> E? {
return maybeDecode(forKey: forKey) as E?
}
fileprivate func maybeDecode<E>(forKey: String) -> E? {
guard let object = self.decodeObject(forKey: forKey) else {
return nil
}
return object as? E
}
}
extension ArrayType: NSCoding {}
extension AssociatedType: NSCoding {}
extension AssociatedValue: NSCoding {}
extension Attribute: NSCoding {}
extension BytesRange: NSCoding {}
extension ClosureParameter: NSCoding {}
extension ClosureType: NSCoding {}
extension DictionaryType: NSCoding {}
extension EnumCase: NSCoding {}
extension FileParserResult: NSCoding {}
extension GenericRequirement: NSCoding {}
extension GenericType: NSCoding {}
extension GenericTypeParameter: NSCoding {}
extension Import: NSCoding {}
extension Method: NSCoding {}
extension MethodParameter: NSCoding {}
extension Modifier: NSCoding {}
extension Subscript: NSCoding {}
extension TupleElement: NSCoding {}
extension TupleType: NSCoding {}
extension Type: NSCoding {}
extension TypeName: NSCoding {}
extension Typealias: NSCoding {}
extension Types: NSCoding {}
extension Variable: NSCoding {}
"""),
.init(name: "Composer.swift", content:
"""
//
// Created by Krzysztof Zablocki on 31/12/2016.
// Copyright (c) 2016 Pixle. All rights reserved.
//
import Foundation
private func currentTimestamp() -> TimeInterval {
return CFAbsoluteTimeGetCurrent()
}
/// Responsible for composing results of `FileParser`.
public enum Composer {
/// Performs final processing of discovered types:
/// - extends types with their corresponding extensions;
/// - replaces typealiases with actual types
/// - finds actual types for variables and enums raw values
/// - filters out any private types and extensions
///
/// - Parameter parserResult: Result of parsing source code.
/// - Returns: Final types and extensions of unknown types.
public static func uniqueTypesAndFunctions(_ parserResult: FileParserResult) -> (types: [Type], functions: [SourceryMethod], typealiases: [Typealias]) {
let composed = ParserResultsComposed(parserResult: parserResult)
let resolveType = { (typeName: TypeName, containingType: Type?) -> Type? in
return composed.resolveType(typeName: typeName, containingType: containingType)
}
composed.types.parallelPerform { type in
type.variables.forEach {
resolveVariableTypes($0, of: type, resolve: resolveType)
}
type.methods.forEach {
resolveMethodTypes($0, of: type, resolve: resolveType)
}
type.subscripts.forEach {
resolveSubscriptTypes($0, of: type, resolve: resolveType)
}
if let enumeration = type as? Enum {
resolveEnumTypes(enumeration, types: composed.typeMap, resolve: resolveType)
}
if let composition = type as? ProtocolComposition {
resolveProtocolCompositionTypes(composition, resolve: resolveType)
}
if let sourceryProtocol = type as? SourceryProtocol {
resolveProtocolTypes(sourceryProtocol, resolve: resolveType)
}
}
composed.functions.parallelPerform { function in
resolveMethodTypes(function, of: nil, resolve: resolveType)
}
updateTypeRelationships(types: composed.types)
return (
types: composed.types.sorted { $0.globalName < $1.globalName },
functions: composed.functions.sorted { $0.name < $1.name },
typealiases: composed.unresolvedTypealiases.values.sorted(by: { $0.name < $1.name })
)
}
typealias TypeResolver = (TypeName, Type?) -> Type?
private static func resolveVariableTypes(_ variable: Variable, of type: Type, resolve: TypeResolver) {
variable.type = resolve(variable.typeName, type)
/// The actual `definedInType` is assigned in `uniqueTypes` but we still
/// need to resolve the type to correctly parse typealiases
/// @see https://github.com/krzysztofzablocki/Sourcery/pull/374
if let definedInTypeName = variable.definedInTypeName {
_ = resolve(definedInTypeName, type)
}
}
private static func resolveSubscriptTypes(_ subscript: Subscript, of type: Type, resolve: TypeResolver) {
`subscript`.parameters.forEach { (parameter) in
parameter.type = resolve(parameter.typeName, type)
}
`subscript`.returnType = resolve(`subscript`.returnTypeName, type)
if let definedInTypeName = `subscript`.definedInTypeName {
_ = resolve(definedInTypeName, type)
}
}
private static func resolveMethodTypes(_ method: SourceryMethod, of type: Type?, resolve: TypeResolver) {
method.parameters.forEach { parameter in
parameter.type = resolve(parameter.typeName, type)
}
/// The actual `definedInType` is assigned in `uniqueTypes` but we still
/// need to resolve the type to correctly parse typealiases
/// @see https://github.com/krzysztofzablocki/Sourcery/pull/374
var definedInType: Type?
if let definedInTypeName = method.definedInTypeName {
definedInType = resolve(definedInTypeName, type)
}
guard !method.returnTypeName.isVoid else { return }
if method.isInitializer || method.isFailableInitializer {
method.returnType = definedInType
if let type = method.actualDefinedInTypeName {
if method.isFailableInitializer {
method.returnTypeName = TypeName(
name: type.name,
isOptional: true,
isImplicitlyUnwrappedOptional: false,
tuple: type.tuple,
array: type.array,
dictionary: type.dictionary,
closure: type.closure,
generic: type.generic,
isProtocolComposition: type.isProtocolComposition
)
} else if method.isInitializer {
method.returnTypeName = type
}
}
} else {
method.returnType = resolve(method.returnTypeName, type)
}
}
private static func resolveEnumTypes(_ enumeration: Enum, types: [String: Type], resolve: TypeResolver) {
enumeration.cases.forEach { enumCase in
enumCase.associatedValues.forEach { associatedValue in
associatedValue.type = resolve(associatedValue.typeName, enumeration)
}
}
guard enumeration.hasRawType else { return }
if let rawValueVariable = enumeration.variables.first(where: { $0.name == "rawValue" && !$0.isStatic }) {
enumeration.rawTypeName = rawValueVariable.actualTypeName
enumeration.rawType = rawValueVariable.type
} else if let rawTypeName = enumeration.inheritedTypes.first {
// enums with no cases or enums with cases that contain associated values can't have raw type
guard !enumeration.cases.isEmpty,
!enumeration.hasAssociatedValues else {
return enumeration.rawTypeName = nil
}
if let rawTypeCandidate = types[rawTypeName] {
if !((rawTypeCandidate is SourceryProtocol) || (rawTypeCandidate is ProtocolComposition)) {
enumeration.rawTypeName = TypeName(rawTypeName)
enumeration.rawType = rawTypeCandidate
}
} else {
enumeration.rawTypeName = TypeName(rawTypeName)
}
}
}
private static func resolveProtocolCompositionTypes(_ protocolComposition: ProtocolComposition, resolve: TypeResolver) {
let composedTypes = protocolComposition.composedTypeNames.compactMap { typeName in
resolve(typeName, protocolComposition)
}
protocolComposition.composedTypes = composedTypes
}
private static func resolveProtocolTypes(_ sourceryProtocol: SourceryProtocol, resolve: TypeResolver) {
sourceryProtocol.associatedTypes.forEach { (_, value) in
guard let typeName = value.typeName,
let type = resolve(typeName, sourceryProtocol)
else { return }
value.type = type
}
sourceryProtocol.genericRequirements.forEach { requirment in
if let knownAssociatedType = sourceryProtocol.associatedTypes[requirment.leftType.name] {
requirment.leftType = knownAssociatedType
}
requirment.rightType.type = resolve(requirment.rightType.typeName, sourceryProtocol)
}
}
private static func updateTypeRelationships(types: [Type]) {
var typesByName = [String: Type]()
types.forEach { typesByName[$0.globalName] = $0 }
var processed = [String: Bool]()
types.forEach { type in
if let type = type as? Class, let supertype = type.inheritedTypes.first.flatMap({ typesByName[$0] }) as? Class {
type.supertype = supertype
}
processed[type.globalName] = true
updateTypeRelationship(for: type, typesByName: typesByName, processed: &processed)
}
}
private static func findBaseType(for type: Type, name: String, typesByName: [String: Type]) -> Type? {
if let baseType = typesByName[name] {
return baseType
}
if let module = type.module, let baseType = typesByName["\\(module).\\(name)"] {
return baseType
}
for importModule in type.imports {
if let baseType = typesByName["\\(importModule).\\(name)"] {
return baseType
}
}
return nil
}
private static func updateTypeRelationship(for type: Type, typesByName: [String: Type], processed: inout [String: Bool]) {
type.based.keys.forEach { name in
guard let baseType = findBaseType(for: type, name: name, typesByName: typesByName) else { return }
let globalName = baseType.globalName
if processed[globalName] != true {
processed[globalName] = true
updateTypeRelationship(for: baseType, typesByName: typesByName, processed: &processed)
}
baseType.based.keys.forEach { type.based[$0] = $0 }
baseType.basedTypes.forEach { type.basedTypes[$0.key] = $0.value }
baseType.inherits.forEach { type.inherits[$0.key] = $0.value }
baseType.implements.forEach { type.implements[$0.key] = $0.value }
if baseType is Class {
type.inherits[globalName] = baseType
} else if let baseProtocol = baseType as? SourceryProtocol {
type.implements[globalName] = baseProtocol
if let extendingProtocol = type as? SourceryProtocol {
baseProtocol.associatedTypes.forEach {
if extendingProtocol.associatedTypes[$0.key] == nil {
extendingProtocol.associatedTypes[$0.key] = $0.value
}
}
}
} else if baseType is ProtocolComposition {
// TODO: associated types?
type.implements[globalName] = baseType
}
type.basedTypes[globalName] = baseType
}
}
}
"""),
.init(name: "Definition.swift", content:
"""
import Foundation
/// Describes that the object is defined in a context of some `Type`
public protocol Definition: AnyObject {
/// Reference to type name where the object is defined,
/// nil if defined outside of any `enum`, `struct`, `class` etc
var definedInTypeName: TypeName? { get }
/// Reference to actual type where the object is defined,