Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(ingestion): support Swift optional-binding call assignments (#3768)
Rin48e85ecac7fb
diff --git a/packages/core/src/repowise/core/ingestion/languages/receiver_types.py b/packages/core/src/repowise/core/ingestion/languages/receiver_types.pyindex 94a33cd33..de5d3f25c 100644--- a/packages/core/src/repowise/core/ingestion/languages/receiver_types.py+++ b/packages/core/src/repowise/core/ingestion/languages/receiver_types.py@@ -1115,6 +1115,8 @@ _LANGUAGE_STATEMENT_ENDS = {"python": frozenset("\n"), "dart": frozenset(";")}_NEXT_LINE_CONTINUES = {language: re.compile(r"[.?:+\-*/%&|=!<>]|(?:as|is)\b") for language in ("kotlin", "swift")}+_SWIFT_OPTIONAL_BINDING_PREFIX = re.compile(r"(?:\b(?:if|guard|while)|,)\s*$")+_SWIFT_BINDING_ENDS = re.compile(r"[{,]|\belse\b")def _call_end(text: str, index: int) -> int:@@ -1137,12 +1139,18 @@ def _call_end(text: str, index: int) -> int:return -1-def _call_ends_statement(cleaned: str, opened: int, language: str) -> bool:+def _call_ends_statement(cleaned: str, opened: int, language: str, start: int = 0) -> bool:"""Does the call whose ``(`` ends just before *opened* end its statement?"""end = _call_end(cleaned, opened)if end < 0:return Falserest = cleaned[end:].lstrip(" \t\n" if language == "dart" else " \t")+ if (+ language == "swift"+ and _SWIFT_OPTIONAL_BINDING_PREFIX.search(cleaned[:start])+ and _SWIFT_BINDING_ENDS.match(rest)+ ):+ return Trueif rest and rest[0] not in _LANGUAGE_STATEMENT_ENDS.get(language, _STATEMENT_ENDS):return Falsecontinues = _NEXT_LINE_CONTINUES.get(language)@@ -1162,7 +1170,7 @@ def scan_call_assignments(text: str, language: str) -> tuple[CallAssignment, ...starts = [0, *(newline.end() for newline in _NEWLINE.finditer(cleaned))]found: list[CallAssignment] = []for match in pattern.finditer(cleaned):- if not _call_ends_statement(cleaned, match.end(), language):+ if not _call_ends_statement(cleaned, match.end(), language, match.start()):continuefound.append(CallAssignment(diff --git a/tests/unit/ingestion/test_receiver_types.py b/tests/unit/ingestion/test_receiver_types.pyindex 511a6bc26..a0a780fe8 100644--- a/tests/unit/ingestion/test_receiver_types.py+++ b/tests/unit/ingestion/test_receiver_types.py@@ -845,6 +845,39 @@ class TestKotlinSwiftDartCallAssignments:body = "f() {\n val r = make()\n r.load()\n}"assert scan_call_assignments(body, "kotlin") == (CallAssignment(2, "r", None, "make"),)+ @pytest.mark.parametrize(+ ("statement", "expected"),+ [+ ("if let r = findRepo() {", CallAssignment(2, "r", None, "findRepo")),+ ("if var r = findRepo() {", CallAssignment(2, "r", None, "findRepo")),+ ("guard let r = findRepo() else { return }", CallAssignment(2, "r", None, "findRepo")),+ ("while let r = findRepo() {", CallAssignment(2, "r", None, "findRepo")),+ ],+ )+ def test_swift_optional_binding(+ self, statement: str, expected: CallAssignment+ ) -> None:+ body = f"f() {{\n {statement}\n}}"+ assert scan_call_assignments(body, "swift") == (expected,)++ def test_swift_optional_binding_multiple_clauses(self) -> None:+ body = "f() {\n if let o = makeRepo(), let r = findRepo() {\n }\n}"+ assert scan_call_assignments(body, "swift") == (+ CallAssignment(2, "o", None, "makeRepo"),+ CallAssignment(2, "r", None, "findRepo"),+ )++ @pytest.mark.parametrize(+ "statement",+ [+ "let r = makeRepo() { Other() }",+ "if case let .some(r) = findRepo() {",+ ],+ )+ def test_swift_call_without_a_binding_is_refused(self, statement: str) -> None:+ body = f"f() {{\n {statement}\n}}"+ assert scan_call_assignments(body, "swift") == ()+class TestClassScope:"""What counts as a field, given that a class span contains every method.diff --git a/tests/unit/ingestion/test_return_type_chain_resolution.py b/tests/unit/ingestion/test_return_type_chain_resolution.pyindex 57aff8f4f..bc07a246c 100644--- a/tests/unit/ingestion/test_return_type_chain_resolution.py+++ b/tests/unit/ingestion/test_return_type_chain_resolution.py@@ -903,6 +903,7 @@ _SWIFT_LOCALS = ("class Repo {\n func load() {}\n}\n""class Other {\n func load() {}\n}\n""func makeRepo() throws -> Repo { return Repo() }\n"+ "func findRepo() -> Repo? { return nil }\n""func mk(a: Int) -> Repo { return Repo() }\nfunc mk(a: String) -> Other { return Other() }\n""func fetch() async -> Repo { return Repo() }\n""enum Factory {\n static func other() async -> Other { return Other() }\n}\n"@@ -936,6 +937,11 @@ def _last_line_callees(tmp_path: Path, language: str, body: str) -> set[str]:("swift", "let r = await Factory.other()\nr.load()", "a.swift::Other::load"),("swift", "let r = try? makeRepo()\nr?.load()", "a.swift::Repo::load"),("swift", "async let r = fetch()\nawait r.load()", "a.swift::Repo::load"),+ ("swift", "if let r = findRepo() {\nr.load()", "a.swift::Repo::load"),+ ("swift", "if var r = findRepo() {\nr.load()", "a.swift::Repo::load"),+ ("swift", "guard let r = findRepo() else { return }\nr.load()", "a.swift::Repo::load"),+ ("swift", "while let r = findRepo() {\nr.load()", "a.swift::Repo::load"),+ ("swift", "if let o = makeRepo(), let r = findRepo() {\nr.load()", "a.swift::Repo::load"),],)def test_kotlin_and_swift_local_takes_the_type_its_call_returns(@@ -953,6 +959,8 @@ def test_kotlin_and_swift_local_takes_the_type_its_call_returns(("kotlin", "val r = makeRepo()\n ?: Other()"),("kotlin", "val r = makeRepo()\n as Other"),("swift", "let r = try makeRepo()\n as! Other"),+ ("swift", "let r = makeRepo() { Other() }"),+ ("swift", "if case let .some(r) = findRepo() {"),("kotlin", "val r = mk(1)"), # overloads of one arity that return different types("swift", "let r = mk(a: 1)"),("kotlin", "val r = gen()"), # a type parameter is no class