Files
SwiftLint/Source/SwiftLintFramework/Helpers/NamespaceCollector.swift
T
JP Simard b83e0991b9 Remove all file headers
The MIT license doesn't require that all files be prepended with this
licensing or copyright information. Realm confirmed that they're ok with this
change. This will enable some companies to contribute to SwiftLint and the
date & authorship information will remain accessible via git source control.
2018-05-04 13:42:02 -07:00

57 lines
1.8 KiB
Swift

import Foundation
import SourceKittenFramework
struct NamespaceCollector {
struct Element {
let name: String
let kind: SwiftDeclarationKind
let offset: Int
let dictionary: [String: SourceKitRepresentable]
init?(dictionary: [String: SourceKitRepresentable], namespace: [String]) {
guard let name = dictionary.name,
let kind = dictionary.kind.flatMap(SwiftDeclarationKind.init),
let offset = dictionary.offset else {
return nil
}
self.name = (namespace + [name]).joined(separator: ".")
self.kind = kind
self.offset = offset
self.dictionary = dictionary
}
}
private let dictionary: [String: SourceKitRepresentable]
init(dictionary: [String: SourceKitRepresentable]) {
self.dictionary = dictionary
}
func findAllElements(of types: Set<SwiftDeclarationKind>,
namespace: [String] = []) -> [Element] {
return findAllElements(in: dictionary, of: types, namespace: namespace)
}
private func findAllElements(in dictionary: [String: SourceKitRepresentable],
of types: Set<SwiftDeclarationKind>,
namespace: [String] = []) -> [Element] {
return dictionary.substructure.flatMap { subDict -> [Element] in
var elements: [Element] = []
guard let element = Element(dictionary: subDict, namespace: namespace) else {
return elements
}
if types.contains(element.kind) {
elements.append(element)
}
elements += findAllElements(in: subDict, of: types, namespace: [element.name])
return elements
}
}
}