Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing computed properties inside Assemblies #75

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/KnitCodeGen/AssemblyParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ private class ClassDeclVisitor: SyntaxVisitor {
return .skipChildren
}

override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
// There could be computed properties that contain other function calls we don't want to parse
return .skipChildren
}

}

extension IdentifiedDeclSyntax {
Expand Down
23 changes: 23 additions & 0 deletions Tests/KnitCodeGenTests/AssemblyParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,29 @@ final class AssemblyParsingTests: XCTestCase {
)
}

func testAdditionalFunctionsInComputedPropertyAreNotParsed() throws {
let sourceFile: SourceFile = """
class ExampleAssembly: Assembly {
func assemble(container: Container) {
container.register(MyService.self) { }
}
static var dependencies: [any ModuleAssembly.Type] {
return ExampleAssembly.generatedDependencies.filter { $0.resolverType == AppResolver.self }
}
}
"""

let config = try assertParsesSyntaxTree(sourceFile)
XCTAssertEqual(config.name, "Example")
XCTAssertEqual(
config.registrations,
[
.init(service: "MyService", accessLevel: .internal),
],
"The `dependencies` computed property should be ignored"
)
}

// MARK: - ClassDecl Extension

func testClassDeclExtension() {
Expand Down