/
Type.swift
350 lines (298 loc) · 15.6 KB
/
Type.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
//
// Created by Krzysztof Zablocki on 11/09/2016.
// Copyright (c) 2016 Pixle. All rights reserved.
//
import Foundation
/// Defines Swift type
@objcMembers public class Type: NSObject, SourceryModel, Annotated {
/// :nodoc:
public var module: String?
// All local typealiases
// sourcery: skipJSExport
/// :nodoc:
public var typealiases: [String: Typealias] {
didSet {
typealiases.values.forEach { $0.parent = self }
}
}
// sourcery: skipJSExport
/// Whether declaration is an extension of some type
public var isExtension: Bool
// sourcery: forceEquality
/// Kind of type declaration, i.e. `enum`, `struct`, `class`, `protocol` or `extension`
public var kind: String { return isExtension ? "extension" : "unknown" }
/// Type access level, i.e. `internal`, `private`, `fileprivate`, `public`, `open`
public let accessLevel: String
/// Type name in global scope. For inner types includes the name of its containing type, i.e. `Type.Inner`
public var name: String {
guard let parentName = parent?.name else { return localName }
return "\(parentName).\(localName)"
}
// sourcery: skipDescription
var globalName: String {
guard let module = module else { return name }
return "\(module).\(name)"
}
/// Whether type is generic
public var isGeneric: Bool
/// Type name in its own scope.
public var localName: String
/// Variables defined in this type only, inluding variables defined in its extensions,
/// but not including variables inherited from superclasses (for classes only) and protocols
public var variables: [Variable]
// sourcery: skipEquality, skipDescription
/// All variables defined for this type, including variables defined in extensions,
/// in superclasses (for classes only) and protocols
public var allVariables: [Variable] {
return flattenAll({
return $0.variables
}, filter: { all, extracted in
!all.contains(where: { $0.name == extracted.name && $0.isStatic == extracted.isStatic })
})
}
/// Methods defined in this type only, inluding methods defined in its extensions,
/// but not including methods inherited from superclasses (for classes only) and protocols
public var methods: [Method]
// sourcery: skipEquality, skipDescription
/// All methods defined for this type, including methods defined in extensions,
/// in superclasses (for classes only) and protocols
public var allMethods: [Method] {
return flattenAll({ $0.methods })
}
/// Subscripts defined in this type only, inluding subscripts defined in its extensions,
/// but not including subscripts inherited from superclasses (for classes only) and protocols
public var subscripts: [Subscript]
// sourcery: skipEquality, skipDescription
/// All subscripts defined for this type, including subscripts defined in extensions,
/// in superclasses (for classes only) and protocols
public var allSubscripts: [Subscript] {
return flattenAll({ $0.subscripts })
}
// sourcery: skipEquality, skipDescription, skipJSExport
/// Bytes position of the body of this type in its declaration file if available.
public var bodyBytesRange: BytesRange?
private func flattenAll<T>(_ extraction: @escaping (Type) -> [T], filter: (([T], T) -> Bool)? = nil) -> [T] {
let all = NSMutableOrderedSet()
all.addObjects(from: extraction(self))
let filteredExtraction = { (target: Type) -> [T] in
if let filter = filter {
// swiftlint:disable:next force_cast
let all = all.array as! [T]
let extracted = extraction(target).filter({ filter(all, $0) })
return extracted
} else {
return extraction(target)
}
}
inherits.values.sorted(by: { $0.name < $1.name }).forEach { all.addObjects(from: filteredExtraction($0)) }
implements.values.sorted(by: { $0.name < $1.name }).forEach { all.addObjects(from: filteredExtraction($0)) }
return all.array.compactMap { $0 as? T }
}
/// All initializers defined in this type
public var initializers: [Method] {
return methods.filter { $0.isInitializer }
}
/// All annotations for this type
public var annotations: [String: NSObject] = [:]
/// Static variables defined in this type
public var staticVariables: [Variable] {
return variables.filter { $0.isStatic }
}
/// Static methods defined in this type
public var staticMethods: [Method] {
return methods.filter { $0.isStatic }
}
/// Class methods defined in this type
public var classMethods: [Method] {
return methods.filter { $0.isClass }
}
/// Instance variables defined in this type
public var instanceVariables: [Variable] {
return variables.filter { !$0.isStatic }
}
/// Instance methods defined in this type
public var instanceMethods: [Method] {
return methods.filter { !$0.isStatic && !$0.isClass }
}
/// Computed instance variables defined in this type
public var computedVariables: [Variable] {
return variables.filter { $0.isComputed && !$0.isStatic }
}
/// Stored instance variables defined in this type
public var storedVariables: [Variable] {
return variables.filter { !$0.isComputed && !$0.isStatic }
}
/// Names of types this type inherits from (for classes only) and protocols it implements, in order of definition
public var inheritedTypes: [String] {
didSet {
based.removeAll()
inheritedTypes.forEach { name in
self.based[name] = name
}
}
}
// sourcery: skipEquality, skipDescription
/// Names of types or protocols this type inherits from, including unknown (not scanned) types
public var based = [String: String]()
// sourcery: skipEquality, skipDescription
/// Types this type inherits from (only for classes)
public var inherits = [String: Type]()
// sourcery: skipEquality, skipDescription
/// Protocols this type implements
public var implements = [String: Type]()
/// Contained types
public var containedTypes: [Type] {
didSet {
containedTypes.forEach {
containedType[$0.localName] = $0
$0.parent = self
}
}
}
// sourcery: skipEquality, skipDescription
/// Contained types groupd by their names
public private(set) var containedType: [String: Type] = [:]
/// Name of parent type (for contained types only)
public private(set) var parentName: String?
// sourcery: skipEquality, skipDescription
/// Parent type, if known (for contained types only)
public var parent: Type? {
didSet {
parentName = parent?.name
}
}
// sourcery: skipJSExport
/// :nodoc:
public var parentTypes: AnyIterator<Type> {
var next: Type? = self
return AnyIterator {
next = next?.parent
return next
}
}
// sourcery: skipEquality, skipDescription
/// Superclass type, if known (only for classes)
public var supertype: Type?
/// Type attributes, i.e. `@objc`
public var attributes: [String: Attribute]
// Underlying parser data, never to be used by anything else
// sourcery: skipDescription, skipEquality, skipCoding, skipJSExport
/// :nodoc:
public var __parserData: Any?
// Path to file where the type is defined
// sourcery: skipDescription, skipEquality, skipJSExport
/// :nodoc:
public var __path: String?
/// :nodoc:
public 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: [String: Attribute] = [:],
annotations: [String: NSObject] = [:],
isGeneric: Bool = false) {
self.localName = name
self.accessLevel = accessLevel.rawValue
self.isExtension = isExtension
self.variables = variables
self.methods = methods
self.subscripts = subscripts
self.inheritedTypes = inheritedTypes
self.containedTypes = containedTypes
self.typealiases = [:]
self.parent = parent
self.parentName = parent?.name
self.attributes = attributes
self.annotations = annotations
self.isGeneric = isGeneric
super.init()
containedTypes.forEach {
containedType[$0.localName] = $0
$0.parent = self
}
inheritedTypes.forEach { name in
self.based[name] = name
}
typealiases.forEach({
$0.parent = self
self.typealiases[$0.aliasName] = $0
})
}
/// :nodoc:
public func extend(_ type: Type) {
self.variables += type.variables
self.methods += type.methods
self.subscripts += type.subscripts
self.inheritedTypes += type.inheritedTypes
self.containedTypes += type.containedTypes
type.annotations.forEach { self.annotations[$0.key] = $0.value }
type.inherits.forEach { self.inherits[$0.key] = $0.value }
type.implements.forEach { self.implements[$0.key] = $0.value }
}
// sourcery:inline:Type.AutoCoding
/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
self.module = aDecoder.decode(forKey: "module")
guard let typealiases: [String: Typealias] = aDecoder.decode(forKey: "typealiases") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["typealiases"])); fatalError() }; self.typealiases = typealiases
self.isExtension = aDecoder.decode(forKey: "isExtension")
guard let accessLevel: String = aDecoder.decode(forKey: "accessLevel") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["accessLevel"])); fatalError() }; self.accessLevel = accessLevel
self.isGeneric = aDecoder.decode(forKey: "isGeneric")
guard let localName: String = aDecoder.decode(forKey: "localName") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["localName"])); fatalError() }; self.localName = localName
guard let variables: [Variable] = aDecoder.decode(forKey: "variables") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["variables"])); fatalError() }; self.variables = variables
guard let methods: [Method] = aDecoder.decode(forKey: "methods") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["methods"])); fatalError() }; self.methods = methods
guard let subscripts: [Subscript] = aDecoder.decode(forKey: "subscripts") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["subscripts"])); fatalError() }; self.subscripts = subscripts
self.bodyBytesRange = aDecoder.decode(forKey: "bodyBytesRange")
guard let annotations: [String: NSObject] = aDecoder.decode(forKey: "annotations") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["annotations"])); fatalError() }; self.annotations = annotations
guard let inheritedTypes: [String] = aDecoder.decode(forKey: "inheritedTypes") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["inheritedTypes"])); fatalError() }; self.inheritedTypes = inheritedTypes
guard let based: [String: String] = aDecoder.decode(forKey: "based") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["based"])); fatalError() }; self.based = based
guard let inherits: [String: Type] = aDecoder.decode(forKey: "inherits") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["inherits"])); fatalError() }; self.inherits = inherits
guard let implements: [String: Type] = aDecoder.decode(forKey: "implements") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["implements"])); fatalError() }; self.implements = implements
guard let containedTypes: [Type] = aDecoder.decode(forKey: "containedTypes") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["containedTypes"])); fatalError() }; self.containedTypes = containedTypes
guard let containedType: [String: Type] = aDecoder.decode(forKey: "containedType") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["containedType"])); fatalError() }; self.containedType = containedType
self.parentName = aDecoder.decode(forKey: "parentName")
self.parent = aDecoder.decode(forKey: "parent")
self.supertype = aDecoder.decode(forKey: "supertype")
guard let attributes: [String: Attribute] = aDecoder.decode(forKey: "attributes") else { NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: getVaList(["attributes"])); fatalError() }; self.attributes = attributes
self.__path = aDecoder.decode(forKey: "__path")
}
/// :nodoc:
public func encode(with aCoder: NSCoder) {
aCoder.encode(self.module, forKey: "module")
aCoder.encode(self.typealiases, forKey: "typealiases")
aCoder.encode(self.isExtension, forKey: "isExtension")
aCoder.encode(self.accessLevel, forKey: "accessLevel")
aCoder.encode(self.isGeneric, forKey: "isGeneric")
aCoder.encode(self.localName, forKey: "localName")
aCoder.encode(self.variables, forKey: "variables")
aCoder.encode(self.methods, forKey: "methods")
aCoder.encode(self.subscripts, forKey: "subscripts")
aCoder.encode(self.bodyBytesRange, forKey: "bodyBytesRange")
aCoder.encode(self.annotations, forKey: "annotations")
aCoder.encode(self.inheritedTypes, forKey: "inheritedTypes")
aCoder.encode(self.based, forKey: "based")
aCoder.encode(self.inherits, forKey: "inherits")
aCoder.encode(self.implements, forKey: "implements")
aCoder.encode(self.containedTypes, forKey: "containedTypes")
aCoder.encode(self.containedType, forKey: "containedType")
aCoder.encode(self.parentName, forKey: "parentName")
aCoder.encode(self.parent, forKey: "parent")
aCoder.encode(self.supertype, forKey: "supertype")
aCoder.encode(self.attributes, forKey: "attributes")
aCoder.encode(self.__path, forKey: "__path")
}
// sourcery:end
}
extension Type {
// sourcery: skipDescription, skipJSExport
/// :nodoc:
var isClass: Bool {
let isNotClass = self is Struct || self is Enum || self is Protocol
return !isNotClass && !isExtension
}
}