Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

refactor(ingestion): flatten the binding and type-ref helpers this week nested too deep (#3542)

Raghav Chamadiya766f2500c5ab
diff --git a/packages/core/src/repowise/core/ingestion/extractors/bindings/ts_js.py b/packages/core/src/repowise/core/ingestion/extractors/bindings/ts_js.py
index c27a8149e..d9bccc099 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/bindings/ts_js.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/bindings/ts_js.py
@@ -416,6 +416,17 @@ def cjs_statement_is_reexport(stmt_node: Node, src: str) -> bool:
return "module.exports" in head or stripped.startswith("exports.")
+def _collect_import_clause_specifiers(clause: Node, specifiers: list[Node]) -> bool:
+ """Append the clause's named import specifiers; True if it also binds a default or namespace."""
+ has_non_specifier_import = False
+ for sub in clause.children:
+ if sub.type in ("identifier", "namespace_import"):
+ has_non_specifier_import = True
+ elif sub.type == "named_imports":
+ specifiers.extend(spec for spec in sub.children if spec.type == "import_specifier")
+ return has_non_specifier_import
+
+
def is_type_only_ts_js_import(stmt_node: Node, src: str) -> bool:
"""True if a TypeScript import or re-export is purely type-level.
@@ -436,25 +447,20 @@ def is_type_only_ts_js_import(stmt_node: Node, src: str) -> bool:
for child in stmt_node.children:
if child.type == "import_clause":
- for sub in child.children:
- if sub.type in ("identifier", "namespace_import"):
- has_non_specifier_import = True
- elif sub.type == "named_imports":
- for spec in sub.children:
- if spec.type == "import_specifier":
- specifiers.append(spec)
+ if _collect_import_clause_specifiers(child, specifiers):
+ has_non_specifier_import = True
elif child.type == "export_clause":
- for spec in child.children:
- if spec.type == "export_specifier":
- specifiers.append(spec)
+ specifiers.extend(spec for spec in child.children if spec.type == "export_specifier")
elif child.type == "namespace_export":
has_non_specifier_import = True
if has_non_specifier_import or not specifiers:
return False
- return all(
- any(c.type == "type" or node_text(c, src) == "type" for c in spec.children)
- for spec in specifiers
- )
+ return all(_is_type_specifier(spec, src) for spec in specifiers)
+
+
+def _is_type_specifier(spec: Node, src: str) -> bool:
+ """True for a ``type X`` import or export specifier."""
+ return any(c.type == "type" or node_text(c, src) == "type" for c in spec.children)
diff --git a/packages/core/src/repowise/core/ingestion/graph/builder.py b/packages/core/src/repowise/core/ingestion/graph/builder.py
index 394ed0a7b..3d4ba8cdd 100644
--- a/packages/core/src/repowise/core/ingestion/graph/builder.py
+++ b/packages/core/src/repowise/core/ingestion/graph/builder.py
@@ -503,38 +503,7 @@ class GraphBuilder(MetricsMixin, ResolveMixin, EdgesMixin, SerializeMixin, Rehyd
imp.resolved_file = targets[0]
for target in targets:
file_imports.add(target)
- # Aggregate imported_names on parallel edges
- if self._graph.has_edge(path, target):
- existing = self._graph[path][target].get("imported_names", [])
- merged = list(set(existing + imp.imported_names))
- self._graph[path][target]["imported_names"] = merged
- if not imp.is_module_declaration:
- withdraw_declaration_hint(self._graph[path][target])
- _merge_load_kind(self._graph[path][target], imp)
- else:
- edge_attrs: dict[str, Any] = {
- "edge_type": "imports",
- "imported_names": list(imp.imported_names),
- "type_only": imp.type_only,
- }
- if imp.deferred:
- edge_attrs["deferred"] = True
- # ``external:`` targets are excluded before the
- # directory test, not after: an external node id has no
- # directory, and a single-segment one such as
- # ``external:strings`` yields parent ``"."`` — which is
- # exactly _own_dir for any root-level file, so every
- # dot-free stdlib import from the repo root would
- # otherwise be stamped a same-package sibling.
- if (
- _unit_fanout
- and not target.startswith("external:")
- and PurePosixPath(target).parent.as_posix() == _own_dir
- ):
- edge_attrs["hint_source"] = SAME_PACKAGE_HINT
- elif imp.is_module_declaration:
- edge_attrs["hint_source"] = MODULE_DECLARATION_HINT
- self._graph.add_edge(path, target, **edge_attrs)
+ self._add_import_edge(path, target, imp, _unit_fanout, _own_dir)
import_targets[path] = file_imports
lang_import_time[_lang] = lang_import_time.get(_lang, 0.0) + (_t.monotonic() - _t0)
if progress:
@@ -554,36 +523,7 @@ class GraphBuilder(MetricsMixin, ResolveMixin, EdgesMixin, SerializeMixin, Rehyd
# edges feed into heritage's import_targets propagation if a
# future language emits them, and so dead-code reachability
# sees every edge before any analysis pass runs.
- if progress:
- progress.on_phase_start("graph.type_refs", len(self._parsed_files))
- type_use_counts = resolve_type_refs(self._parsed_files, ctx, self._graph)
- for path in self._parsed_files:
- for _, target, data in self._graph.out_edges(path, data=True):
- # Treat both real ``using`` imports and synthesised
- # type-use edges as import-like for downstream heritage
- # / call resolution. Without this, type-use edges would
- # only contribute to file-reachability — not to
- # ``import_targets`` which gates cross-file call /
- # heritage lookups.
- # ``no_scope_widening`` marks a type-use edge whose target was
- # inferred (the repo declares that type name in exactly one
- # file) rather than read off an import the file actually
- # writes. Such an edge is real evidence the target is used, so
- # it still counts for file reachability and the unused-export
- # pass, but it is not evidence that this file's bare names
- # resolve into the target — feeding it to ``import_targets``
- # let unrelated same-named symbols match and minted false call
- # edges (measured on goose: 25% precision on the edges it
- # gained).
- if data.get("edge_type") in ("imports", "type_use") and not data.get(
- "no_scope_widening"
- ):
- import_targets.setdefault(path, set()).add(target)
- if progress:
- _phase_done = getattr(progress, "on_phase_done", None)
- if _phase_done is not None:
- _phase_done("graph.type_refs")
- del type_use_counts # used only for logging inside resolve_type_refs
+ self._resolve_type_ref_edges(ctx, import_targets, progress=progress)
# --- Phase 1c: Resolve C# member-access reads ---
# Bind `var x = new T(...); ... x.Prop` style property reads
@@ -687,6 +627,81 @@ class GraphBuilder(MetricsMixin, ResolveMixin, EdgesMixin, SerializeMixin, Rehyd
)
return self._graph
+ def _add_import_edge(
+ self, path: str, target: str, imp: Any, unit_fanout: bool, own_dir: str
+ ) -> None:
+ """Add or merge the ``imports`` edge ``path -> target`` for *imp*."""
+ # Aggregate imported_names on parallel edges
+ if self._graph.has_edge(path, target):
+ existing = self._graph[path][target].get("imported_names", [])
+ merged = list(set(existing + imp.imported_names))
+ self._graph[path][target]["imported_names"] = merged
+ if not imp.is_module_declaration:
+ withdraw_declaration_hint(self._graph[path][target])
+ _merge_load_kind(self._graph[path][target], imp)
+ return
+ edge_attrs: dict[str, Any] = {
+ "edge_type": "imports",
+ "imported_names": list(imp.imported_names),
+ "type_only": imp.type_only,
+ }
+ if imp.deferred:
+ edge_attrs["deferred"] = True
+ # ``external:`` targets are excluded before the
+ # directory test, not after: an external node id has no
+ # directory, and a single-segment one such as
+ # ``external:strings`` yields parent ``"."`` — which is
+ # exactly own_dir for any root-level file, so every
+ # dot-free stdlib import from the repo root would
+ # otherwise be stamped a same-package sibling.
+ if (
+ unit_fanout
+ and not target.startswith("external:")
+ and PurePosixPath(target).parent.as_posix() == own_dir
+ ):
+ edge_attrs["hint_source"] = SAME_PACKAGE_HINT
+ elif imp.is_module_declaration:
+ edge_attrs["hint_source"] = MODULE_DECLARATION_HINT
+ self._graph.add_edge(path, target, **edge_attrs)
+
+ def _resolve_type_ref_edges(
+ self,
+ ctx: ResolverContext,
+ import_targets: dict[str, set[str]],
+ progress: Any | None = None,
+ ) -> None:
+ """Add type-use edges and widen ``import_targets`` with import-like out-edges."""
+ if progress:
+ progress.on_phase_start("graph.type_refs", len(self._parsed_files))
+ type_use_counts = resolve_type_refs(self._parsed_files, ctx, self._graph)
+ for path in self._parsed_files:
+ for _, target, data in self._graph.out_edges(path, data=True):
+ # Treat both real ``using`` imports and synthesised
+ # type-use edges as import-like for downstream heritage
+ # / call resolution. Without this, type-use edges would
+ # only contribute to file-reachability — not to
+ # ``import_targets`` which gates cross-file call /
+ # heritage lookups.
+ # ``no_scope_widening`` marks a type-use edge whose target was
+ # inferred (the repo declares that type name in exactly one
+ # file) rather than read off an import the file actually
+ # writes. Such an edge is real evidence the target is used, so
+ # it still counts for file reachability and the unused-export
+ # pass, but it is not evidence that this file's bare names
+ # resolve into the target — feeding it to ``import_targets``
+ # let unrelated same-named symbols match and minted false call
+ # edges (measured on goose: 25% precision on the edges it
+ # gained).
+ if data.get("edge_type") in ("imports", "type_use") and not data.get(
+ "no_scope_widening"
+ ):
+ import_targets.setdefault(path, set()).add(target)
+ if progress:
+ _phase_done = getattr(progress, "on_phase_done", None)
+ if _phase_done is not None:
+ _phase_done("graph.type_refs")
+ del type_use_counts # used only for logging inside resolve_type_refs
+
def graph(self) -> nx.DiGraph:
"""Return the graph (building it first if necessary)."""
if not self._built:
diff --git a/packages/core/src/repowise/core/ingestion/type_ref_resolution.py b/packages/core/src/repowise/core/ingestion/type_ref_resolution.py
index e43fad782..5c2607dae 100644
--- a/packages/core/src/repowise/core/ingestion/type_ref_resolution.py
+++ b/packages/core/src/repowise/core/ingestion/type_ref_resolution.py
@@ -779,21 +779,26 @@ def _ambient_declaration_names(
if node.type == "ambient_declaration":
prefix = encoded[node.start_byte : min(node.end_byte, node.start_byte + 40)]
if re.match(rb"declare\s+global\b", prefix):
- nested = [node]
- while nested:
- declaration = nested.pop()
- name = declaration.child_by_field_name("name")
- if name is not None:
- type_name = encoded[name.start_byte : name.end_byte].decode(
- "utf-8", errors="replace"
- )
- if type_name in defined_names:
- global_names.add(type_name)
- nested.extend(declaration.children)
+ global_names |= _declared_names_under(node, encoded, defined_names)
stack.extend(node.children)
return global_names
+def _declared_names_under(node: Any, encoded: bytes, defined_names: set[str]) -> set[str]:
+ """Names in ``defined_names`` declared anywhere under ``node``."""
+ found: set[str] = set()
+ nested = [node]
+ while nested:
+ declaration = nested.pop()
+ name = declaration.child_by_field_name("name")
+ if name is not None:
+ type_name = encoded[name.start_byte : name.end_byte].decode("utf-8", errors="replace")
+ if type_name in defined_names:
+ found.add(type_name)
+ nested.extend(declaration.children)
+ return found
+
+
def _find_ts_type_in_stem_map(
type_name: str,
from_path: str,