@@ -295,56 +295,51 @@ def _compute_import_dirs(repo_root: Path | None, import_packages: set[str]) -> s
295295 import_dirs .add (repo_root / src_prefix / Path (* pkg .split ("." )))
296296 return import_dirs
297297
298- def _discover_single_hop (
299- self ,
300- source_files : list [Path ],
301- candidates : list [Path ],
302- repo_root : Path | None ,
303- ) -> list [Path ]:
304- type_refs , import_packages = self ._collect_source_refs (source_files )
305- source_dirs = {f .parent for f in source_files }
306- eligible_dirs = source_dirs | self ._compute_import_dirs (repo_root , import_packages )
307- source_set = set (source_files )
308-
298+ @staticmethod
299+ def _collect_frontier_classes (source_files : list [Path ]) -> set [str ]:
309300 frontier_classes : set [str ] = set ()
310- frontier_packages : set [str ] = set ()
311301 for f in source_files :
312302 try :
313303 content = f .read_text (encoding = "utf-8" )
314304 frontier_classes .update (_extract_classes (content , f ))
315- pkg = _extract_package (content )
316- if pkg :
317- frontier_packages .add (pkg )
318305 except (OSError , UnicodeDecodeError ):
319306 pass
307+ return frontier_classes
320308
321- discovered : list [Path ] = []
322- for candidate in candidates :
323- if candidate in source_set :
324- continue
325- try :
326- content = candidate .read_text (encoding = "utf-8" )
327- cand_classes = _extract_classes (content , candidate )
328-
329- if candidate .parent in eligible_dirs and cand_classes & type_refs :
330- discovered .append (candidate )
331- continue
332-
333- cand_type_refs = _extract_type_refs (content )
334- if cand_type_refs & frontier_classes :
335- discovered .append (candidate )
336- continue
337-
338- cand_imports = _extract_imports (content , candidate )
339- for imp in cand_imports :
340- imp_class = imp .rsplit ("." , 1 )[- 1 ]
341- if imp_class in frontier_classes :
342- discovered .append (candidate )
343- break
344- except (OSError , UnicodeDecodeError ):
345- pass
309+ @staticmethod
310+ def _candidate_matches_frontier (
311+ candidate : Path ,
312+ eligible_dirs : set [Path ],
313+ type_refs : set [str ],
314+ frontier_classes : set [str ],
315+ ) -> bool :
316+ try :
317+ content = candidate .read_text (encoding = "utf-8" )
318+ cand_classes = _extract_classes (content , candidate )
319+ if candidate .parent in eligible_dirs and cand_classes & type_refs :
320+ return True
321+ if _extract_type_refs (content ) & frontier_classes :
322+ return True
323+ return any (imp .rsplit ("." , 1 )[- 1 ] in frontier_classes for imp in _extract_imports (content , candidate ))
324+ except (OSError , UnicodeDecodeError ):
325+ return False
326+
327+ def _discover_single_hop (
328+ self ,
329+ source_files : list [Path ],
330+ candidates : list [Path ],
331+ repo_root : Path | None ,
332+ ) -> list [Path ]:
333+ type_refs , import_packages = self ._collect_source_refs (source_files )
334+ eligible_dirs = {f .parent for f in source_files } | self ._compute_import_dirs (repo_root , import_packages )
335+ source_set = set (source_files )
336+ frontier_classes = self ._collect_frontier_classes (source_files )
346337
347- return discovered
338+ return [
339+ c
340+ for c in candidates
341+ if c not in source_set and self ._candidate_matches_frontier (c , eligible_dirs , type_refs , frontier_classes )
342+ ]
348343
349344 def build (self , fragments : list [Fragment ], repo_root : Path | None = None ) -> EdgeDict :
350345 jvm_frags = [f for f in fragments if _is_jvm_file (f .path )]
0 commit comments