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
feat(ingestion): type Dart for-in loop variables from the container they walk (#3853)
Amana3da28de18d0
diff --git a/packages/core/src/repowise/core/ingestion/call_receiver_typing.py b/packages/core/src/repowise/core/ingestion/call_receiver_typing.pyindex f98aa7610..15fbb821a 100644--- a/packages/core/src/repowise/core/ingestion/call_receiver_typing.py+++ b/packages/core/src/repowise/core/ingestion/call_receiver_typing.py@@ -141,6 +141,18 @@ def _store_capped(cache: dict[_K, _V], key: _K, value: _V, cap: int) -> None:cache[key] = value+def _is_its_own_container(type_name: str | None, spelling: str | None, language: str) -> bool:+ """Whether *type_name*, a name's value type, is just the head of its container.++ Dart types ``List<Repo> repos`` as a ``List`` value as well as a container+ of ``Repo``; the two agree, so the container still answers. Go types a+ slice as no value at all, so for it any value type is a conflict.+ """+ if language != "dart" or type_name is None or spelling is None:+ return False+ return strip_type_arguments(spelling).strip() == type_name++class _Scope(NamedTuple):"""What one symbol's span binds, less the symbol's own name."""@@ -1787,7 +1799,12 @@ class ReceiverTypingMixin:the class's package is not found, which costs the edge."""if not clause.member:- return (None if clause.head in types else containers.get(clause.head)), file_path+ spelling = containers.get(clause.head)+ if clause.head in types and not _is_its_own_container(+ types[clause.head], spelling, language+ ):+ return None, file_path+ return spelling, file_pathhead_type = types.get(clause.head)class_id = None if head_type is None else self._range_class(file_path, head_type, language)if class_id is None:diff --git a/packages/core/src/repowise/core/ingestion/languages/receiver_types.py b/packages/core/src/repowise/core/ingestion/languages/receiver_types.pyindex c9619a18e..6316d6fc8 100644--- a/packages/core/src/repowise/core/ingestion/languages/receiver_types.py+++ b/packages/core/src/repowise/core/ingestion/languages/receiver_types.py@@ -254,7 +254,7 @@ _SWIFT_CONSTRUCTED = re.compile(# out `{Dao d = const Dao()}` too. An initialising formal (`Repo(this.api)`)# spells no type: the field it assigns already carries one._DART_TYPED = re.compile(- rf"(?<![\w.])(?P<type>{_TYPE})\??\s+(?P<name>[a-z_]\w*)\s*(?=(?P<closer>[=;,)}}]))"+ rf"(?<![\w.])(?P<type>{_TYPE})\??\s+(?P<name>[a-z_]\w*)\s*(?=(?P<closer>[=;,)}}]|in\b))")# `final x = Foo()`, `var`/`const`, with `new`/`const` before the type. The@@ -1387,7 +1387,7 @@ def types_by_class(# is whatever ranging over ``expr``'s container yields in its position. Two# scans feed it, the clauses and the containers they may range over, and the# resolver joins them, since only it can type ``expr`` itself.-RANGE_LANGUAGES = frozenset({"go"})+RANGE_LANGUAGES = frozenset({"go", "dart"})# What ranging over each container shape yields, the group name giving the# position: ``[]T``, ``[N]T`` and a variadic ``...T`` an int index, then a@@ -1432,10 +1432,50 @@ _GO_RANGE = re.compile()+# Dart's ``for (final p in head[.member[()]])``, and ``await for`` over a+# stream, declare one variable with no type spelled: it is the element of the+# container walked. A written type (``for (Repo p in xs)``) is read by+# ``_DART_TYPED`` and never reaches here, since ``Repo`` is no ``[a-z_]`` name.+# The variable is the clause's *value*; Dart has no key position.+_DART_NAME = r"[a-z_]\w*"+_DART_TYPE_NAME = r"[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)?"++# ``List<T>``, ``Iterable<T>``, ``Set<T>``, ``Queue<T>`` and ``Stream<T>`` yield a+# ``T``, nullable element included. A ``Map`` is not iterable, and a nested+# generic (``List<List<T>>``) or an un-awaited ``Future<List<T>>`` is no shape+# here, so each yields nothing.+_DART_ITERABLE = r"(?:List|Iterable|Set|Queue|Stream)"+_DART_ELEMENT_SHAPES = (+ re.compile(rf"{_DART_ITERABLE}\s*<\s*(?P<value>{_DART_TYPE_NAME})\??\s*>"),+)++# The whole spelling, nested arguments too, so the element shape refuses it+# instead of the declaration going unseen.+_DART_CONTAINER = rf"{_DART_ITERABLE}\s*<[^<>\n]*(?:<[^<>\n]*>[^<>\n]*)*>"++# A parameter, a local or a field, ``List<T> name`` with an optional ``final``+# before it and ``?`` after the type. The closer is kept, as ``_DART_TYPED``+# keeps it: ``;`` or ``=`` makes a field at class scope, unless the match sits+# inside parentheses (``Repo({List<T> xs = const []})``), which owns none.+_DART_CONTAINER_DECLARATIONS = (+ re.compile(+ rf"(?<![\w.])(?P<type>{_DART_CONTAINER})\??\s+(?P<name>{_DART_NAME})\s*"+ rf"(?=(?P<closer>[=;,)}}]))"+ ),+)++_DART_FOR_IN = re.compile(+ rf"\bfor\s*\(\s*(?:final|var)\s+(?P<value>{_DART_NAME})\s+in\s+"+ rf"(?P<head>{_DART_NAME})(?:\.(?P<member>[A-Za-z_]\w*)(?P<call>\(\))?)?\s*\)"+)++class RangeClause(NamedTuple):"""One ``for key, value := range head[.member[()]]`` at one line.``value`` is empty when only one variable is declared; ``_`` names none.+ Dart's ``for (final value in head[.member[()]])`` fills ``value`` alone:+ it has no key position."""line: int@@ -1459,25 +1499,34 @@ def scan_ranges(text: str, language: str) -> RangeScan:return RangeScan((), ())cleaned = _without_comments(text, language)starts = [0, *(newline.end() for newline in _NEWLINE.finditer(cleaned))]+ if language == "dart":+ clause_pattern, declaration_patterns = _DART_FOR_IN, _DART_CONTAINER_DECLARATIONS+ inside = _inside_parentheses(cleaned)+ else:+ clause_pattern, declaration_patterns = _GO_RANGE, _GO_CONTAINER_DECLARATIONS+ inside = Noneclauses = tuple(RangeClause(bisect_right(starts, match.start()),- match.group("key"),- match.group("value") or "",+ match.groupdict().get("key") or "",+ match.groupdict().get("value") or "",match.group("head"),match.group("member") or "",bool(match.group("call")),)- for match in _GO_RANGE.finditer(cleaned)+ for match in clause_pattern.finditer(cleaned))containers = sorted(Declaration(bisect_right(starts, match.start()),match.group("name"),match.group("type"),+ ""+ if inside is not None and inside(match.start())+ else (match.groupdict().get("closer") or ""),member=bool(match.groupdict().get("member")),)- for pattern in _GO_CONTAINER_DECLARATIONS+ for pattern in declaration_patternsfor match in pattern.finditer(cleaned))return RangeScan(clauses, tuple(containers))@@ -1500,7 +1549,8 @@ def range_element_types(return Nonereturn _usable_type_name(raw, language)[0]- for shape in _GO_ELEMENT_SHAPES:+ shapes = _DART_ELEMENT_SHAPES if language == "dart" else _GO_ELEMENT_SHAPES+ for shape in shapes:match = shape.fullmatch(spelling)if match is not None:groups = match.groupdict()diff --git a/tests/unit/ingestion/test_dart_range_receivers.py b/tests/unit/ingestion/test_dart_range_receivers.pynew file mode 100644index 000000000..bfc2301d4--- /dev/null+++ b/tests/unit/ingestion/test_dart_range_receivers.py@@ -0,0 +1,218 @@+"""Dart ``for (final p in xs)`` variables typed from the container they walk.++``for (final p in repos)`` spells no type for ``p``: it is the element of the+container. The scan reads the clauses and the container declarations; the+resolver joins them. Every negative removes one proof, and must leave the+variable untyped rather than guessed.+"""++from __future__ import annotations++from pathlib import Path++import pytest++from repowise.core.ingestion.call_resolver import CallResolver+from repowise.core.ingestion.languages.receiver_types import (+ RangeClause,+ range_element_types,+ scan_declarations,+ scan_ranges,+ types_in_span,+)+from tests.unit.ingestion.test_chained_receiver_calls import _parse_all+++def _clauses(text: str) -> list[RangeClause]:+ return list(scan_ranges(text, "dart").clauses)+++def _containers(text: str) -> dict[str, str | None]:+ return types_in_span(scan_ranges(text, "dart").containers, 1, 10_000)+++class TestElementTypes:+ @pytest.mark.parametrize(+ ("spelling", "expected"),+ [+ ("List<Repo>", (None, "Repo")),+ ("Iterable<Repo>", (None, "Repo")),+ ("Set<Repo>", (None, "Repo")),+ ("Queue<Repo>", (None, "Repo")),+ ("Stream<Repo>", (None, "Repo")),+ ("List<Repo?>", (None, "Repo")),+ ("List<models.Repo>", (None, "Repo")),+ ],+ )+ def test_a_known_shape(self, spelling: str, expected: tuple[str | None, str | None]) -> None:+ assert range_element_types(spelling, "dart") == expected++ @pytest.mark.parametrize(+ "spelling",+ [+ "Map<String, Repo>",+ "List<List<Repo>>",+ "Future<List<Repo>>",+ "Future<Repo>",+ "Repo",+ "",+ ],+ )+ def test_an_unknown_shape_is_refused(self, spelling: str) -> None:+ assert range_element_types(spelling, "dart") is None+++class TestClauses:+ def test_final_over_a_name(self) -> None:+ (clause,) = _clauses("for (final p in repos) {")+ assert (clause.key, clause.value, clause.head, clause.member, clause.call) == (+ "",+ "p",+ "repos",+ "",+ False,+ )++ def test_var_over_a_field(self) -> None:+ (clause,) = _clauses("for (var p in r.items) {")+ assert (clause.value, clause.head, clause.member, clause.call) == ("p", "r", "items", False)++ def test_a_zero_argument_method(self) -> None:+ (clause,) = _clauses("for (final p in r.all()) {")+ assert (clause.member, clause.call) == ("all", True)++ def test_await_for(self) -> None:+ (clause,) = _clauses("await for (final p in events) {")+ assert (clause.value, clause.head) == ("p", "events")++ @pytest.mark.parametrize(+ "line",+ [+ "for (Repo p in repos) {", # a written type is _DART_TYPED's+ "for (final Repo p in repos) {",+ "for (final p in r.a.b) {",+ "for (final p in f().items) {",+ "for (final p in r.all(1)) {",+ "for (var i = 0; i < n; i++) {",+ "// for (final p in repos) {",+ ],+ )+ def test_anything_else_is_not_read(self, line: str) -> None:+ assert _clauses(line) == []+++class TestContainers:+ def test_parameter_local_and_field(self) -> None:+ text = "void f(Iterable<Repo> rs, Set<Repo>? ss) {\n Queue<Repo> q = Queue();\n}\n"+ assert _containers(text) == {"rs": "Iterable<Repo>", "ss": "Set<Repo>", "q": "Queue<Repo>"}++ def test_a_field_keeps_the_closer_that_makes_it_one(self) -> None:+ (field,) = scan_ranges("class A {\n final List<Repo> repos;\n}\n", "dart").containers+ assert (field.name, field.type_name, field.closer) == ("repos", "List<Repo>", ";")++ def test_a_parameter_inside_parentheses_owns_no_field_closer(self) -> None:+ text = "class A {\n A({List<Repo> xs = const []});\n void f(List<Repo> ys) {}\n}\n"+ assert {d.closer for d in scan_ranges(text, "dart").containers} == {""}++ def test_a_map_is_no_container(self) -> None:+ assert _containers("void f(Map<String, Repo> m) {}") == {}+++class TestTypedLoopVariable:+ def test_a_written_type_is_declared(self) -> None:+ text = "void f() {\n for (Repo p in repos) {}\n for (final Repo q in repos) {}\n}\n"+ names = {d.name: d.type_name for d in scan_declarations(text, "dart")}+ assert names == {"p": "Repo", "q": "Repo"}+++_REPO = (+ "class Item {\n void save() {}\n}\n"+ "class Repo {\n"+ " Repo();\n"+ " final List<Item> items = [];\n"+ " final Map<String, Item> byName = {};\n"+ " List<Item> all() => items;\n"+ " Item get current => Item();\n"+ " void close() {}\n"+ "}\n"+)++_PREFIX = 4 # lines of ``app.dart`` before the body+++def _callees(tmp_path: Path, body: str, params: str = "Repo r, List<Repo> repos") -> set[str]:+ app = f"import 'repo.dart';\nclass App {{\n void run({params}) {{\n{body}\n }}\n}}\n"+ parsed = _parse_all(tmp_path, {"repo.dart": ("dart", _REPO), "app.dart": ("dart", app)})+ for imp in parsed["app.dart"].imports:+ imp.resolved_file = "repo.dart"+ resolver = CallResolver(+ parsed, {"app.dart": {"repo.dart"}, "repo.dart": set()}, repo_path=str(tmp_path)+ )+ return {+ rc.callee_id+ for rc in resolver.resolve_file("app.dart", parsed["app.dart"].calls)+ if rc.caller_id == "app.dart::App::run"+ }+++CLOSE = "repo.dart::Repo::close"+SAVE = "repo.dart::Item::save"+++class TestResolution:+ def test_final_over_a_list_parameter(self, tmp_path: Path) -> None:+ assert CLOSE in _callees(tmp_path, "for (final p in repos) {\n p.close();\n}")++ def test_var_over_a_list_parameter(self, tmp_path: Path) -> None:+ assert CLOSE in _callees(tmp_path, "for (var p in repos) {\n p.close();\n}")++ def test_over_an_iterable_local(self, tmp_path: Path) -> None:+ body = "Iterable<Repo> rs = repos;\nfor (final p in rs) {\n p.close();\n}"+ assert CLOSE in _callees(tmp_path, body)++ def test_over_a_field_of_a_typed_receiver(self, tmp_path: Path) -> None:+ assert SAVE in _callees(tmp_path, "for (final i in r.items) {\n i.save();\n}")++ def test_over_a_method_result(self, tmp_path: Path) -> None:+ assert SAVE in _callees(tmp_path, "for (final i in r.all()) {\n i.save();\n}")++ def test_await_for_over_a_stream(self, tmp_path: Path) -> None:+ body = "await for (final p in events) {\n p.close();\n}"+ assert CLOSE in _callees(tmp_path, body, "Stream<Repo> events")++ def test_a_written_type_still_binds(self, tmp_path: Path) -> None:+ assert CLOSE in _callees(tmp_path, "for (Repo p in repos) {\n p.close();\n}")+ assert CLOSE in _callees(tmp_path, "for (final Repo p in repos) {\n p.close();\n}")++ def test_a_written_type_wins_over_the_container(self, tmp_path: Path) -> None:+ body = "for (Item p in repos) {\n p.save();\n}"+ assert _callees(tmp_path, body) == {SAVE}++ def test_a_map_binds_nothing(self, tmp_path: Path) -> None:+ body = "for (final p in m) {\n p.close();\n}"+ assert CLOSE not in _callees(tmp_path, body, "Map<String, Repo> m")++ def test_a_map_field_binds_nothing(self, tmp_path: Path) -> None:+ assert SAVE not in _callees(tmp_path, "for (final i in r.byName) {\n i.save();\n}")++ def test_a_type_parameter_binds_nothing(self, tmp_path: Path) -> None:+ body = "for (final p in xs) {\n p.close();\n}"+ assert CLOSE not in _callees(tmp_path, body, "List<T> xs")++ def test_a_nested_generic_binds_nothing(self, tmp_path: Path) -> None:+ body = "for (final p in xs) {\n p.close();\n}"+ assert CLOSE not in _callees(tmp_path, body, "List<List<Repo>> xs")++ def test_an_unawaited_future_binds_nothing(self, tmp_path: Path) -> None:+ body = "for (final p in xs) {\n p.close();\n}"+ assert CLOSE not in _callees(tmp_path, body, "Future<List<Repo>> xs")++ def test_an_unknown_container_binds_nothing(self, tmp_path: Path) -> None:+ assert CLOSE not in _callees(tmp_path, "for (final p in mystery) {\n p.close();\n}")++ def test_a_loop_variable_reusing_a_declared_name_is_skipped(self, tmp_path: Path) -> None:+ """The parameter ``r`` stays a ``Repo`` for the call after the loop."""+ body = "for (final r in other) {\n r.save();\n}\nr.close();"+ callees = _callees(tmp_path, body, "Repo r, List<Item> other")+ assert CLOSE in callees+ assert SAVE not in callees