From a13b3e9d1b1d805f32acbfd91b9c429778144574 Mon Sep 17 00:00:00 2001 From: Sky Lan Date: Fri, 30 Oct 2020 10:25:34 -0700 Subject: [PATCH 1/2] Remove dependency on SourceKitten --- Package.swift | 2 - .../Utilities/ASTUtils.swift | 182 ------------------ .../Utilities/ASTUtilsTests.swift | 111 ----------- 3 files changed, 295 deletions(-) delete mode 100644 Sources/SourceParsingFramework/Utilities/ASTUtils.swift delete mode 100644 Tests/SourceParsingFrameworkTests/Utilities/ASTUtilsTests.swift diff --git a/Package.swift b/Package.swift index 1f2bd9c..19f5fbc 100644 --- a/Package.swift +++ b/Package.swift @@ -8,7 +8,6 @@ let package = Package( .library(name: "CommandFramework", targets: ["CommandFramework"]), ], dependencies: [ - .package(url: "https://github.com/jpsim/SourceKitten.git", from: "0.23.1"), .package(url: "https://github.com/apple/swift-tools-support-core.git", .upToNextMajor(from: "0.0.1")), .package(url: "https://github.com/uber/swift-concurrency.git", .upToNextMajor(from: "0.6.5")), ], @@ -18,7 +17,6 @@ let package = Package( dependencies: [ "SwiftToolsSupport-auto", "Concurrency", - "SourceKittenFramework", ]), .testTarget( name: "SourceParsingFrameworkTests", diff --git a/Sources/SourceParsingFramework/Utilities/ASTUtils.swift b/Sources/SourceParsingFramework/Utilities/ASTUtils.swift deleted file mode 100644 index 5e5d599..0000000 --- a/Sources/SourceParsingFramework/Utilities/ASTUtils.swift +++ /dev/null @@ -1,182 +0,0 @@ -// -// Copyright (c) 2018. Uber Technologies -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import Foundation -import SourceKittenFramework - -/// Extension of SourceKitten `Structure` to provide easy access to a set -/// of common AST properties. -extension Structure { - - /// The substructures of this structure. - public var substructures: [Structure] { - let substructures = (dictionary["key.substructure"] as? [SourceKitRepresentable]) ?? [] - - let result = substructures.compactMap { (substructure: SourceKitRepresentable) -> Structure? in - if let structure = substructure as? [String: SourceKitRepresentable] { - return Structure(sourceKitResponse: structure) - } else { - return nil - } - } - return result - } - - /// The type name of this structure. - public var name: String { - /// The type name of this structure. - return dictionary["key.name"] as! String - } - - /// The type of this structure. - public var type: SwiftDeclarationKind? { - if let kindString = dictionary["key.kind"] as? String { - switch kindString { - case SwiftDeclarationKind.associatedtype.rawValue: return .associatedtype - case SwiftDeclarationKind.class.rawValue: return .class - case SwiftDeclarationKind.enum.rawValue: return .enum - case SwiftDeclarationKind.enumcase.rawValue: return .enumcase - case SwiftDeclarationKind.enumelement.rawValue: return .enumelement - case SwiftDeclarationKind.extension.rawValue: return .extension - case SwiftDeclarationKind.extensionClass.rawValue: return .extensionClass - case SwiftDeclarationKind.extensionEnum.rawValue: return .extensionEnum - case SwiftDeclarationKind.extensionProtocol.rawValue: return .extensionProtocol - case SwiftDeclarationKind.extensionStruct.rawValue: return .extensionStruct - case SwiftDeclarationKind.functionAccessorAddress.rawValue: return .functionAccessorAddress - case SwiftDeclarationKind.functionAccessorDidset.rawValue: return .functionAccessorDidset - case SwiftDeclarationKind.functionAccessorGetter.rawValue: return .functionAccessorGetter - case SwiftDeclarationKind.functionAccessorMutableaddress.rawValue: return .functionAccessorMutableaddress - case SwiftDeclarationKind.functionAccessorSetter.rawValue: return .functionAccessorSetter - case SwiftDeclarationKind.functionAccessorWillset.rawValue: return .functionAccessorWillset - case SwiftDeclarationKind.functionConstructor.rawValue: return .functionConstructor - case SwiftDeclarationKind.functionDestructor.rawValue: return .functionDestructor - case SwiftDeclarationKind.functionFree.rawValue: return .functionFree - case SwiftDeclarationKind.functionMethodClass.rawValue: return .functionMethodClass - case SwiftDeclarationKind.functionMethodInstance.rawValue: return .functionMethodInstance - case SwiftDeclarationKind.functionMethodStatic.rawValue: return .functionMethodStatic - case SwiftDeclarationKind.functionOperator.rawValue: return .functionOperator - case SwiftDeclarationKind.functionOperatorInfix.rawValue: return .functionOperatorInfix - case SwiftDeclarationKind.functionOperatorPostfix.rawValue: return .functionOperatorPostfix - case SwiftDeclarationKind.functionOperatorPrefix.rawValue: return .functionOperatorPrefix - case SwiftDeclarationKind.functionSubscript.rawValue: return .functionSubscript - case SwiftDeclarationKind.genericTypeParam.rawValue: return .genericTypeParam - case SwiftDeclarationKind.module.rawValue: return .module - case SwiftDeclarationKind.precedenceGroup.rawValue: return .precedenceGroup - case SwiftDeclarationKind.protocol.rawValue: return .protocol - case SwiftDeclarationKind.struct.rawValue: return .struct - case SwiftDeclarationKind.typealias.rawValue: return .typealias - case SwiftDeclarationKind.varClass.rawValue: return .varClass - case SwiftDeclarationKind.varGlobal.rawValue: return .varGlobal - case SwiftDeclarationKind.varInstance.rawValue: return .varInstance - case SwiftDeclarationKind.varLocal.rawValue: return .varLocal - case SwiftDeclarationKind.varParameter.rawValue: return .varParameter - case SwiftDeclarationKind.varStatic.rawValue: return .varStatic - default: return nil - } - } - return nil - } - - /// If this structure is an expression call. - // This isn't a type in `SwiftDeclarationKind`. - public var isExpressionCall: Bool { - guard let kindString = dictionary["key.kind"] as? String else { - return false - } - return kindString == "source.lang.swift.expr.call" - } - - /// Check if this structure has the override attribute. - public var isOverride: Bool { - guard let attributes = dictionary["key.attributes"] as? [SourceKitRepresentable] else { - return false - } - for item in attributes { - guard let attribute = item as? [String: SourceKitRepresentable], let attributeValue = attribute["key.attribute"] as? String else { - continue - } - if attributeValue == SwiftDeclarationAttributeKind.override.rawValue { - return true - } - } - return false - } - - /// The return type of a property of method. - public var returnType: String? { - if let value = dictionary["key.typename"] as? String { - return value - } - return nil - } - - /// The unique set of expression call types in this structure. - public var uniqueExpressionCallNames: [String] { - let allNames = filterSubstructure(by: "source.lang.swift.expr.call", recursively: true) - .map { (substructure: Structure) -> String in - substructure.name - } - let set = Set(allNames) - return Array(set).sorted() - } - - /// The inherited types of this structure. - /// - /// - note: This may contain generic types such as Class. - public var inheritedTypes: [String] { - let types = dictionary["key.inheritedtypes"] as? [SourceKitRepresentable] ?? [] - return types.compactMap { (item: SourceKitRepresentable) -> String? in - ((item as? [String: String])?["key.name"])?.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: " ", with: "") - } - } - - /// The names of inherited types of this structure. - /// - /// - note: This property does not include any generic type - /// information. - public var inheritedTypeNames: [String] { - return inheritedTypes.map { (type: String) -> String in - if let index = type.firstIndex(of: "<") { - return String(type.prefix(upTo: index)) - } else { - return type - } - } - } - - /// Filter out substructures with the given `key.kind` value. - /// - /// - parameter kind: The `key.kind` value to filter for. - /// - parameter recursively: If the filter should include - /// nested substructures. - /// - returns: The matching structures. - public func filterSubstructure(by kind: String, recursively: Bool = false) -> [Structure] { - let substructures = self.substructures - let currentLevelSubstructures = substructures.compactMap { (substructure: Structure) -> Structure? in - if substructure.dictionary["key.kind"] as? String == kind { - return substructure - } - return nil - } - if recursively && !substructures.isEmpty { - return currentLevelSubstructures + substructures.flatMap { (substructure: Structure) -> [Structure] in - substructure.filterSubstructure(by: kind, recursively: recursively) - } - } else { - return currentLevelSubstructures - } - } -} diff --git a/Tests/SourceParsingFrameworkTests/Utilities/ASTUtilsTests.swift b/Tests/SourceParsingFrameworkTests/Utilities/ASTUtilsTests.swift deleted file mode 100644 index 0662893..0000000 --- a/Tests/SourceParsingFrameworkTests/Utilities/ASTUtilsTests.swift +++ /dev/null @@ -1,111 +0,0 @@ -// -// Copyright (c) 2018. Uber Technologies -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import SourceKittenFramework -import XCTest -@testable import SourceParsingFramework - -class ASTUtilsTests: AbstractSourceParsingTests { - - func test_inheritedTypes_withSingleLine_verifyResult() { - let structure = self.structure(for: "SingleLineInheritedTypes.swift").substructures[0] - let types = structure.inheritedTypes - - XCTAssertEqual(types, ["SuperClass"]) - } - - func test_inheritedTypes_withMultiLine_verifyResult() { - let structure = self.structure(for: "MultiLineInheritedTypes.swift").substructures[0] - let types = structure.inheritedTypes - - XCTAssertEqual(types, ["SuperClass"]) - } - - func test_inheritedTypeNames_withSingleLine_verifyResult() { - let structure = self.structure(for: "SingleLineInheritedTypes.swift").substructures[0] - let types = structure.inheritedTypeNames - - XCTAssertEqual(types, ["SuperClass"]) - } - - func test_inheritedTypeNames_withMultiLine_verifyResult() { - let structure = self.structure(for: "MultiLineInheritedTypes.swift").substructures[0] - let types = structure.inheritedTypeNames - - XCTAssertEqual(types, ["SuperClass"]) - } - - func test_type_name_returnType_verifyResults() { - let structure = self.structure(for: "Types.swift") - - let myClass = structure.substructures[0] - XCTAssertEqual(myClass.type, SwiftDeclarationKind.class) - XCTAssertEqual(myClass.name, "MyClass") - XCTAssertEqual(myClass.substructures[0].type, SwiftDeclarationKind.varInstance) - XCTAssertEqual(myClass.substructures[0].name, "a") - XCTAssertEqual(myClass.substructures[1].type, SwiftDeclarationKind.varInstance) - XCTAssertEqual(myClass.substructures[1].name, "myOtherProperty") - XCTAssertEqual(myClass.substructures[1].returnType, "Int") - XCTAssertTrue(myClass.substructures[2].isExpressionCall) - XCTAssertEqual(myClass.substructures[2].name, "someMethod") - XCTAssertEqual(myClass.substructures[3].type, SwiftDeclarationKind.functionMethodInstance) - XCTAssertEqual(myClass.substructures[3].name, "myMethod(_:arg2:_:)") - XCTAssertEqual(myClass.substructures[3].returnType, "String") - XCTAssertEqual(myClass.substructures[4].type, SwiftDeclarationKind.functionMethodInstance) - XCTAssertEqual(myClass.substructures[4].name, "voidReturnType()") - XCTAssertNil(myClass.substructures[4].returnType) - - let myProtocol = structure.substructures[1] - XCTAssertEqual(myProtocol.type, SwiftDeclarationKind.protocol) - XCTAssertEqual(myProtocol.name, "MyProtocol") - - let myStruct = structure.substructures[2] - XCTAssertEqual(myStruct.type, SwiftDeclarationKind.struct) - XCTAssertEqual(myStruct.name, "MyStruct") - - let myGlobalLet = structure.substructures[3] - XCTAssertEqual(myGlobalLet.type, SwiftDeclarationKind.varGlobal) - XCTAssertEqual(myGlobalLet.name, "globalLet") - - let myGlobalVar = structure.substructures[4] - XCTAssertEqual(myGlobalVar.type, SwiftDeclarationKind.varGlobal) - XCTAssertEqual(myGlobalVar.name, "globalVar") - } - - func test_isOverride_verifyResults() { - let structure = self.structure(for: "Attributes.swift") - - let childClass = structure.substructures[0] - XCTAssertEqual(childClass.substructures[0].name, "a") - XCTAssertFalse(childClass.substructures[0].isOverride) - - XCTAssertEqual(childClass.substructures[1].name, "myOtherProperty") - XCTAssertTrue(childClass.substructures[1].isOverride) - - XCTAssertEqual(childClass.substructures[3].name, "myMethod(_:arg2:_:)") - XCTAssertTrue(childClass.substructures[3].isOverride) - - XCTAssertEqual(childClass.substructures[4].name, "voidReturnType()") - XCTAssertFalse(childClass.substructures[4].isOverride) - } - - private func structure(for fileName: String) -> Structure { - let fileUrl = fixtureUrl(for: fileName) - let content = try! String(contentsOf: fileUrl) - let file = File(contents: content) - return try! Structure(file: file) - } -} From b42cdb11df5984bc89b83a50616164fb221e7433 Mon Sep 17 00:00:00 2001 From: Sky Lan Date: Fri, 30 Oct 2020 10:38:00 -0700 Subject: [PATCH 2/2] Fix missing imports --- .../SourceParsingFrameworkTests/Utilities/ExtensionsTests.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/SourceParsingFrameworkTests/Utilities/ExtensionsTests.swift b/Tests/SourceParsingFrameworkTests/Utilities/ExtensionsTests.swift index fc10224..0b01c7f 100644 --- a/Tests/SourceParsingFrameworkTests/Utilities/ExtensionsTests.swift +++ b/Tests/SourceParsingFrameworkTests/Utilities/ExtensionsTests.swift @@ -15,7 +15,6 @@ // import XCTest -@testable import SourceKittenFramework class ExtensionsTests: AbstractSourceParsingTests {