Skip to content

Commit

Permalink
Fix parsing computed properties inside Assemblies
Browse files Browse the repository at this point in the history
Previously if there was a computed property that had a function call within its body then the visitor would attempt to parse that function call.
  • Loading branch information
bradfol committed Aug 21, 2023
1 parent 072ccf9 commit f1502ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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

0 comments on commit f1502ad

Please sign in to comment.