Skip to content

Commit

Permalink
Parse the TargetResolver from assemblies
Browse files Browse the repository at this point in the history
  • Loading branch information
skorulis-ap committed Nov 9, 2023
1 parent 747841d commit 3df4f5f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
19 changes: 18 additions & 1 deletion Sources/KnitCodeGen/AssemblyParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func parseSyntaxTree(
name: name,
registrations: assemblyFileVisitor.registrations,
registrationsIntoCollections: assemblyFileVisitor.registrationsIntoCollections,
imports: assemblyFileVisitor.imports
imports: assemblyFileVisitor.imports,
targetResolver: assemblyFileVisitor.targetResolver
)
}

Expand All @@ -67,6 +68,10 @@ private class AssemblyFileVisitor: SyntaxVisitor {
return classDeclVisitor?.registrationErrors ?? []
}

var targetResolver: String {
return classDeclVisitor?.targetResolver ?? "Resolver"
}

init() {
super.init(viewMode: .fixedUp)
}
Expand Down Expand Up @@ -116,6 +121,8 @@ private class ClassDeclVisitor: SyntaxVisitor {

private(set) var registrationErrors = [Error]()

private(set) var targetResolver: String?

init(viewMode: SyntaxTreeViewMode, directives: KnitDirectives) {
self.directives = directives
super.init(viewMode: viewMode)
Expand All @@ -138,6 +145,16 @@ private class ClassDeclVisitor: SyntaxVisitor {
return .skipChildren
}

override func visit(_ node: TypeAliasDeclSyntax) -> SyntaxVisitorContinueKind {
guard node.name.text == "TargetResolver",
let identifier = node.initializer.value.as(IdentifierTypeSyntax.self)
else {
return .skipChildren
}
self.targetResolver = identifier.name.text
return .skipChildren
}

}

extension NamedDeclSyntax {
Expand Down
7 changes: 5 additions & 2 deletions Sources/KnitCodeGen/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ public struct Configuration: Encodable {
public var registrationsIntoCollections: [RegistrationIntoCollection]

public var imports: [ImportDeclSyntax] = []
public var targetResolver: String

public init(
name: String,
registrations: [Registration],
registrationsIntoCollections: [RegistrationIntoCollection],
imports: [ImportDeclSyntax] = []
imports: [ImportDeclSyntax] = [],
targetResolver: String = "Resolver"
) {
self.name = name
self.registrations = registrations
self.registrationsIntoCollections = registrationsIntoCollections
self.imports = imports
self.targetResolver = targetResolver
}

public enum CodingKeys: CodingKey {
Expand All @@ -35,7 +38,7 @@ public extension Configuration {
func makeTypeSafetySourceFile() throws -> SourceFileSyntax {
return try TypeSafetySourceFile.make(
assemblyName: "\(name)Assembly",
extensionTarget: "Resolver",
extensionTarget: targetResolver,
registrations: registrations
)
}
Expand Down
13 changes: 13 additions & 0 deletions Tests/KnitCodeGenTests/AssemblyParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ final class AssemblyParsingTests: XCTestCase {

let config = try assertParsesSyntaxTree(sourceFile)
XCTAssertEqual(config.name, "Example")
XCTAssertEqual(config.targetResolver, "Resolver")
XCTAssertEqual(
config.registrations,
[
Expand Down Expand Up @@ -244,6 +245,18 @@ final class AssemblyParsingTests: XCTestCase {
)
}

func testCustomResolver() throws {
let sourceFile: SourceFileSyntax = """
class MyAssembly: Assembly {
typealias TargetResolver = TestResolver
}
"""

let config = try assertParsesSyntaxTree(sourceFile)
XCTAssertEqual(config.name, "My")
XCTAssertEqual(config.targetResolver, "TestResolver")
}

}

private func assertParsesSyntaxTree(
Expand Down

0 comments on commit 3df4f5f

Please sign in to comment.