Skip to content
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.

Commit

Permalink
Add a way to return the conforming type when searching for type confo…
Browse files Browse the repository at this point in the history
…rmance (#35)

* Use SyntaxVisitor subclass instead of SyntaxRewriter

* Refactor tests to be able to build on top of them

* Add failing tests

* Add logic to find conforming type

* Fix incorrect indentation in tests

* Add tests for enum an struct

* PR feedback
  • Loading branch information
fdiaz authored Jul 21, 2020
1 parent 140dc1c commit 75bd9f4
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final class TypeConformanceCommandSpec: QuickSpec {
var path: String!

beforeEach {
fileURL = try? Temporary.makeFile(content: "final class Some: SomeType { }")
fileURL = try? Temporary.makeFile(content: "final class Foo: SomeType { }")
path = fileURL?.path ?? ""
}

Expand Down Expand Up @@ -109,6 +109,11 @@ final class TypeConformanceCommandSpec: QuickSpec {
let result = try? TestTask.run(withArguments: ["type-conformance", "--type-names", "SomeType", "--path", path])
expect(result?.outputMessage).to(contain(fileURL.lastPathComponent))
}

it("outputs the conforming type name") {
let result = try? TestTask.run(withArguments: ["type-conformance", "--type-names", "SomeType", "--path", path])
expect(result?.outputMessage).to(contain("Foo"))
}
}

}
Expand Down
9 changes: 8 additions & 1 deletion Sources/SwiftInspectorCommands/TypeConformanceCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ final class TypeConformanceCommand: ParsableCommand {

// Output to standard output
private func output(from conformance: TypeConformance) {
print("\(path) \(conformance.typeName) \(conformance.doesConform)")
guard !conformance.conformingTypeNames.isEmpty else {
print("\(path) \(conformance.typeName) \(conformance.doesConform)")
return
}

conformance.conformingTypeNames.forEach {
print("\(path) \(conformance.typeName) \(conformance.doesConform) \($0)")
}
}

}
155 changes: 112 additions & 43 deletions Sources/SwiftInspectorCore/Tests/TypeConformanceAnalyzerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,112 +30,181 @@ import Foundation

final class TypeConformanceAnalyzerSpec: QuickSpec {
private var fileURL: URL!

override func spec() {
afterEach {
guard let fileURL = self.fileURL else {
return
}
try? Temporary.removeItem(at: fileURL)
}

describe("analyze(fileURL:)") {

var result: TypeConformance?

context("when a type conforms to a protocol") {
context("with only one conformance") {
it("conforms") {
beforeEach {
let content = """
protocol Some {}
class Another: Some {}
"""

self.fileURL = try? Temporary.makeFile(content: content)
let sut = TypeConformanceAnalyzer(typeName: "Some")
result = try? sut.analyze(fileURL: self.fileURL)
}

it("conforms") {
expect(result?.doesConform) == true
}

it("returns the conforming type name") {
expect(result?.conformingTypeNames) == ["Another"]
}
}

context("with a struct conforming to the protocol") {
beforeEach {
let content = """
protocol Some {}
struct Another: Some {}
"""

self.fileURL = try? Temporary.makeFile(content: content)
let sut = TypeConformanceAnalyzer(typeName: "Some")
result = try? sut.analyze(fileURL: self.fileURL)
}

it("conforms") {
expect(result?.doesConform) == true
}

it("returns the conforming type name") {
expect(result?.conformingTypeNames) == ["Another"]
}
}

context("with an enum conforming to the protocol") {
beforeEach {
let content = """
protocol Some {}
enum Another: Some {}
"""

self.fileURL = try? Temporary.makeFile(content: content)
let sut = TypeConformanceAnalyzer(typeName: "Some")
let result = try? sut.analyze(fileURL: self.fileURL)
result = try? sut.analyze(fileURL: self.fileURL)
}

it("conforms") {
expect(result?.doesConform) == true
}

context("when the type has multiple conformances") {
it("conforms") {
let content = """
it("returns the conforming type name") {
expect(result?.conformingTypeNames) == ["Another"]
}
}

context("when the type has multiple conformances") {
beforeEach {
let content = """
protocol Foo {}
protocol Bar {}
class Another: Foo, Bar {}
class Second: Foo {}
"""

self.fileURL = try? Temporary.makeFile(content: content)

let sut = TypeConformanceAnalyzer(typeName: "Bar")
let result = try? sut.analyze(fileURL: self.fileURL)

expect(result?.doesConform) == true
}

self.fileURL = try? Temporary.makeFile(content: content)
let sut = TypeConformanceAnalyzer(typeName: "Foo")
result = try? sut.analyze(fileURL: self.fileURL)
}

context("when the types conform in a different line") {
it("conforms") {
let content = """

it("conforms") {
expect(result?.doesConform) == true
}

it("returns the conforming type names") {
expect(result?.conformingTypeNames) == ["Another", "Second"]
}
}

context("when the types conform in a different line") {
beforeEach {
let content = """
protocol A {}
protocol B {}
protocol C {}
class Another: A
,B, C {}
"""

self.fileURL = try? Temporary.makeFile(content: content)

let sut = TypeConformanceAnalyzer(typeName: "B")
let result = try? sut.analyze(fileURL: self.fileURL)

expect(result?.doesConform) == true
}

self.fileURL = try? Temporary.makeFile(content: content)
let sut = TypeConformanceAnalyzer(typeName: "B")
result = try? sut.analyze(fileURL: self.fileURL)
}

it("conforms") {
expect(result?.doesConform) == true
}

it("returns the conforming type name") {
expect(result?.conformingTypeNames) == ["Another"]
}

}
}

context("when a type implements a subclass") {
it("is marked as conforms") {
beforeEach {
let content = """
open class Some {}
class Another: Some {}
"""

self.fileURL = try? Temporary.makeFile(content: content)

let sut = TypeConformanceAnalyzer(typeName: "Some")
let result = try? sut.analyze(fileURL: self.fileURL)

result = try? sut.analyze(fileURL: self.fileURL)
}

it("is marked as conforms") {
expect(result?.doesConform) == true
}

it("returns the conforming type name") {
expect(result?.conformingTypeNames) == ["Another"]
}
}

context("when the type is not present") {
it("is not marked as conforms") {
beforeEach {
let content = """
protocol Some {}
class Another: Some {}
"""

self.fileURL = try? Temporary.makeFile(content: content)

let sut = TypeConformanceAnalyzer(typeName: "AnotherType")
let result = try? sut.analyze(fileURL: self.fileURL)

result = try? sut.analyze(fileURL: self.fileURL)
}

it("is not marked as conforms") {
expect(result?.doesConform) == false
}

it("returns an empty array for conforming types") {
expect(result?.conformingTypeNames) == []
}
}

}
}

}
41 changes: 33 additions & 8 deletions Sources/SwiftInspectorCore/TypeConformanceAnalyzer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ public final class TypeConformanceAnalyzer: Analyzer {
/// - Parameter fileURL: The fileURL where the Swift file is located
public func analyze(fileURL: URL) throws -> TypeConformance {
var doesConform = false
var conformingTypes: [String] = []

let syntax: SourceFileSyntax = try cachedSyntaxTree.syntaxTree(for: fileURL)
let reader = TypeConformanceSyntaxReader() { [unowned self] node in
doesConform = doesConform || self.isSyntaxNode(node, ofType: self.typeName)
let reader = TypeConformanceSyntaxVisitor() { [unowned self] node in
let nodeConforms = self.isSyntaxNode(node, ofType: self.typeName)
guard nodeConforms else { return }
doesConform = doesConform || nodeConforms

guard let node = self.findConformingType(of: node) else { return }
conformingTypes.append(node)
}
_ = reader.visit(syntax)

return TypeConformance(typeName: typeName, doesConform: doesConform)
reader.walk(syntax)

return TypeConformance(typeName: typeName, doesConform: doesConform, conformingTypeNames: conformingTypes)
}

// MARK: Private
Expand All @@ -55,6 +62,24 @@ public final class TypeConformanceAnalyzer: Analyzer {
let syntaxTypeName = String(describing: node.typeName).trimmingCharacters(in: .whitespaces)
return (syntaxTypeName == self.typeName)
}

private func findConformingType(of node: SyntaxProtocol?) -> String? {
guard let originalNode = node else { return nil }

guard let parent = originalNode.parent else { return nil }

// A conforming type can only be a class, struct or enum
// See: https://docs.swift.org/swift-book/LanguageGuide/Protocols.html
if let classSyntax = parent.as(ClassDeclSyntax.self) {
return classSyntax.identifier.text
} else if let structSyntax = parent.as(StructDeclSyntax.self) {
return structSyntax.identifier.text
} else if let enumSyntax = parent.as(EnumDeclSyntax.self) {
return enumSyntax.identifier.text
} else {
return findConformingType(of: parent.parent)
}
}

private let typeName: String
private let cachedSyntaxTree: CachedSyntaxTree
Expand All @@ -63,17 +88,17 @@ public final class TypeConformanceAnalyzer: Analyzer {
public struct TypeConformance: Equatable {
public let typeName: String
public let doesConform: Bool
public let conformingTypeNames: [String]
}

// TODO: Update to use SyntaxVisitor when this bug is resolved (https://bugs.swift.org/browse/SR-11591)
private final class TypeConformanceSyntaxReader: SyntaxRewriter {
private final class TypeConformanceSyntaxVisitor: SyntaxVisitor {
init(onNodeVisit: @escaping (InheritedTypeSyntax) -> Void) {
self.onNodeVisit = onNodeVisit
}

override func visit(_ node: InheritedTypeSyntax) -> Syntax {
override func visit(_ node: InheritedTypeSyntax) -> SyntaxVisitorContinueKind {
onNodeVisit(node)
return super.visit(node)
return .visitChildren
}

private let onNodeVisit: (InheritedTypeSyntax) -> Void
Expand Down

0 comments on commit 75bd9f4

Please sign in to comment.