Skip to content

Commit 3be8974

Browse files
committed
dart: try exact-name matches before short-name matches
Scoped resolution merged exact and short-name index hits before requiring a unique match, so an imported top-level helper() became ambiguous with any visible Tool.helper method and the edge was dropped. Try exact names first and only widen to short names when nothing matched exactly, which also mirrors Dart's lexical-before-this lookup.
1 parent 77b51ff commit 3be8974

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎codewiki/src/be/dependency_analyzer/analysis/call_graph_analyzer.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,15 +614,15 @@ def _has_dart_scope(self, caller: Node) -> bool:
614614
return Path(caller.relative_path).as_posix() in self._dart_visible
615615

616616
def _dart_scope_candidates(
617-
self, key: str, lang_indexes: dict, scope: set[str], caller_file: str | None = None
617+
self, func_ids: list[str], scope: set[str], caller_file: str | None = None
618618
) -> list[str]:
619619
from codewiki.src.be.dependency_analyzer.analyzers.dart_imports import (
620620
is_private_dart_name,
621621
)
622622

623623
own_library = self._dart_library_members.get(caller_file, set()) if caller_file else set()
624624
candidates: list[str] = []
625-
for func_id in [*lang_indexes["exact"].get(key, []), *lang_indexes["simple"].get(key, [])]:
625+
for func_id in func_ids:
626626
func = self.functions.get(func_id)
627627
if func_id in candidates or func is None:
628628
continue
@@ -658,9 +658,16 @@ def _resolve_dart_callee(
658658
if "." in callee:
659659
keys.append(callee.split(".")[0])
660660
for key in keys:
661-
candidates = self._dart_scope_candidates(key, lang_indexes, scope, caller_file)
662-
if len(candidates) == 1:
663-
return candidates[0]
661+
# Exact names first: an imported `helper()` must not turn ambiguous
662+
# because some visible class also has a `helper` method.
663+
for index in ("exact", "simple"):
664+
candidates = self._dart_scope_candidates(
665+
lang_indexes[index].get(key, []), scope, caller_file
666+
)
667+
if len(candidates) == 1:
668+
return candidates[0]
669+
if candidates:
670+
break
664671

665672
from codewiki.src.be.dependency_analyzer.analyzers.dart_flutter import (
666673
RIVERPOD_NOTIFIER_TYPE,
@@ -673,7 +680,9 @@ def _resolve_dart_callee(
673680
for alias in provider_alias_candidates(callee):
674681
candidates = [
675682
func_id
676-
for func_id in self._dart_scope_candidates(alias, lang_indexes, scope, caller_file)
683+
for func_id in self._dart_scope_candidates(
684+
lang_indexes["exact"].get(alias, []), scope, caller_file
685+
)
677686
if self.functions[func_id].node_type
678687
in (RIVERPOD_PROVIDER_TYPE, RIVERPOD_NOTIFIER_TYPE)
679688
and self.functions[func_id].name == alias

‎tests/test_dart_analyzer.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,18 @@ def test_private_target_in_other_library_is_not_bound(tmp_path: Path) -> None:
493493
assert "lib/a.dart::_Ticker.tick" not in components["lib/b.dart::B.f"].depends_on
494494

495495

496+
def test_top_level_function_wins_over_same_named_method(tmp_path: Path) -> None:
497+
_write_repo(
498+
tmp_path,
499+
{
500+
"lib/util.dart": "void helper() {}\nclass Tool {\n void helper() {}\n}\n",
501+
"lib/main.dart": "import 'package:demo/util.dart';\nvoid go() { helper(); }\n",
502+
},
503+
)
504+
components = DependencyParser(str(tmp_path)).parse_repository()
505+
assert components["lib/main.dart::go"].depends_on == {"lib/util.dart::helper"}
506+
507+
496508
def test_barrel_reexport_selects_exported_definition(tmp_path: Path) -> None:
497509
_write_repo(
498510
tmp_path,

0 commit comments

Comments
 (0)