Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions backend/languages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ def to_dict(self) -> dict:
}


def dedupe_calls(info: FunctionInfo, found: list[tuple[int, str]]) -> None:
"""Folds a function's raw (lineno, callee-id) hits into its code lines and its own
unique `calls` list. Shared by every plugin's pass-2 call resolution: each callee
is recorded once per calling line and once overall, regardless of how many lines
or expressions on that line call it.
"""
line_index = {cl["lineno"]: cl for cl in info.code_lines}
seen: set[str] = set()
for lineno, target in found:
if lineno in line_index and target not in line_index[lineno]["calls"]:
line_index[lineno]["calls"].append(target)
if target not in seen:
seen.add(target)
info.calls.append(target)


def build_edges(functions: dict[str, FunctionInfo]) -> list[dict]:
"""One {"source", "target", "line"} entry per resolved call, read back off
each function's own code lines. Shared by every plugin's response building.
"""
edges = []
for info in functions.values():
for cl in info.code_lines:
for target in cl["calls"]:
edges.append({"source": info.id, "target": target, "line": cl["lineno"]})
return edges


class LanguagePlugin(ABC):
"""One entry in the language registry (see languages/__init__.py)."""

Expand Down
24 changes: 6 additions & 18 deletions backend/languages/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import ast
from dataclasses import dataclass, field

from .base import FunctionInfo, LanguagePlugin
from .base import FunctionInfo, LanguagePlugin, build_edges, dedupe_calls


@dataclass
Expand Down Expand Up @@ -202,6 +202,7 @@ def __init__(self, sources: dict[str, str]):
self.functions: dict[str, FunctionInfo] = {}
self.file_errors: dict[str, str] = {} # rel_path -> error message
self.file_functions: dict[str, list[str]] = {} # rel_path -> [function ids]
self._nodes: dict[str, ast.FunctionDef | ast.AsyncFunctionDef] = {} # func id -> its AST node, pass 1 to pass 2

def analyze(self) -> dict:
self._parse_files()
Expand Down Expand Up @@ -280,8 +281,7 @@ def _register_function(
)
self.functions[func_id] = info
self.file_functions[mod.rel_path].append(func_id)
# stash the AST node for pass 2
info._node = node # type: ignore[attr-defined]
self._nodes[func_id] = node
return info

# -- pass 2: resolve calls --
Expand All @@ -291,18 +291,10 @@ def _resolve_calls(self) -> None:
mod = self.modules[info.module]
resolver = self._make_resolver(mod)
collector = _CallCollector(resolver, info.class_name)
node = info._node # type: ignore[attr-defined]
node = self._nodes.pop(info.id)
for child in node.body:
collector.visit(child)
line_index = {cl["lineno"]: cl for cl in info.code_lines}
seen: set[str] = set()
for lineno, target in collector.found:
if lineno in line_index and target not in line_index[lineno]["calls"]:
line_index[lineno]["calls"].append(target)
if target not in seen:
seen.add(target)
info.calls.append(target)
del info._node # type: ignore[attr-defined]
dedupe_calls(info, collector.found)

def _resolve_class(self, name: str, mod: ModuleInfo) -> tuple[ModuleInfo, str] | None:
"""If `name` refers to a class visible in `mod` (defined locally or imported), return (owning module, class name)."""
Expand Down Expand Up @@ -372,11 +364,7 @@ def resolve(func: ast.expr, class_name: str | None) -> str | None:
# -- output --

def _to_response(self) -> dict:
edges = []
for info in self.functions.values():
for cl in info.code_lines:
for target in cl["calls"]:
edges.append({"source": info.id, "target": target, "line": cl["lineno"]})
edges = build_edges(self.functions)
files = [
{
"path": rel_path,
Expand Down
54 changes: 19 additions & 35 deletions backend/languages/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
from __future__ import annotations

import tree_sitter_rust
from collections.abc import Iterator
from dataclasses import dataclass, field
from tree_sitter import Language, Node, Parser

from .base import FunctionInfo, LanguagePlugin
from .base import FunctionInfo, LanguagePlugin, build_edges, dedupe_calls

_RUST_LANGUAGE = Language(tree_sitter_rust.language())

Expand Down Expand Up @@ -265,6 +266,8 @@ def __init__(self, sources: dict[str, str]):
self.functions: dict[str, FunctionInfo] = {}
self.file_errors: dict[str, str] = {}
self.file_functions: dict[str, list[str]] = {}
self._nodes: dict[str, Node] = {} # func id -> its AST node, pass 1 to pass 2
self._param_types: dict[str, dict[str, str]] = {} # func id -> its params' inferred types, pass 1 to pass 2

def analyze(self) -> dict:
self._parse_files()
Expand Down Expand Up @@ -411,20 +414,20 @@ def _register_function(
)
self.functions[func_id] = info
self.file_functions[mod.rel_path].append(func_id)
info._node = fn_node # type: ignore[attr-defined]
info._param_types = _extract_param_types(fn_node, source) # type: ignore[attr-defined]
self._nodes[func_id] = fn_node
self._param_types[func_id] = _extract_param_types(fn_node, source)
return info

# -- pass 2: resolve calls --

def _resolve_calls(self) -> None:
for info in self.functions.values():
mod = self.modules[info.module]
node = info._node # type: ignore[attr-defined]
node = self._nodes.pop(info.id)
source = self.sources_bytes[mod.rel_path]
body = node.child_by_field_name("body")

local_types = dict(info._param_types) # type: ignore[attr-defined]
local_types = dict(self._param_types.pop(info.id))
if body is not None:
local_types.update(_collect_let_types(body, source))

Expand All @@ -434,16 +437,13 @@ def _resolve_calls(self) -> None:
for child in body.children:
collector.visit(child)

line_index = {cl["lineno"]: cl for cl in info.code_lines}
seen: set[str] = set()
for lineno, target in collector.found:
if lineno in line_index and target not in line_index[lineno]["calls"]:
line_index[lineno]["calls"].append(target)
if target not in seen:
seen.add(target)
info.calls.append(target)
del info._node # type: ignore[attr-defined]
del info._param_types # type: ignore[attr-defined]
dedupe_calls(info, collector.found)

def _crate_candidates(self, crate_name: str) -> Iterator[RustModuleInfo]:
"""Every module belonging to `crate_name` - itself or a descendant of its `::` path."""
for candidate in self.modules.values():
if candidate.module == crate_name or candidate.module.startswith(crate_name + "::"):
yield candidate

def _resolve_method_on_type(self, mod: RustModuleInfo, type_name: str, method: str) -> str | None:
"""Resolves `<a value or type of type_name>.method()` / `TypeName::method()`."""
Expand All @@ -462,18 +462,10 @@ def _resolve_method_on_type(self, mod: RustModuleInfo, type_name: str, method: s
return None

def _find_impl_in_crate(self, crate_name: str, type_name: str) -> RustModuleInfo | None:
for candidate in self.modules.values():
if candidate.module == crate_name or candidate.module.startswith(crate_name + "::"):
if type_name in candidate.classes:
return candidate
return None
return next((c for c in self._crate_candidates(crate_name) if type_name in c.classes), None)

def _find_fn_in_crate(self, crate_name: str, fn_name: str) -> RustModuleInfo | None:
for candidate in self.modules.values():
if candidate.module == crate_name or candidate.module.startswith(crate_name + "::"):
if fn_name in candidate.top_level:
return candidate
return None
return next((c for c in self._crate_candidates(crate_name) if fn_name in c.top_level), None)

def _find_submodule_in_crate(self, crate_name: str, base: str) -> RustModuleInfo | None:
"""The one module in `crate_name` whose own path ends in `::base` - i.e. `base` really is a
Expand All @@ -484,11 +476,7 @@ def _find_submodule_in_crate(self, crate_name: str, base: str) -> RustModuleInfo
matching by name alone there would wire the call to an unrelated same-named function anywhere
in the crate. Requiring `base` itself to be a real, unique module name avoids that.
"""
matches = [
candidate
for candidate in self.modules.values()
if candidate.module.startswith(crate_name + "::") and candidate.module.rsplit("::", 1)[-1] == base
]
matches = [c for c in self._crate_candidates(crate_name) if c.module.rsplit("::", 1)[-1] == base]
return matches[0] if len(matches) == 1 else None

@staticmethod
Expand Down Expand Up @@ -576,11 +564,7 @@ def resolve(func_node: Node, class_name: str | None) -> str | None:
# -- output --

def _to_response(self) -> dict:
edges = []
for info in self.functions.values():
for cl in info.code_lines:
for target in cl["calls"]:
edges.append({"source": info.id, "target": target, "line": cl["lineno"]})
edges = build_edges(self.functions)
files = [
{"path": rel_path, "functions": ids, "error": self.file_errors.get(rel_path)}
for rel_path, ids in sorted(self.file_functions.items())
Expand Down
23 changes: 6 additions & 17 deletions backend/languages/typescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
from dataclasses import dataclass, field
from tree_sitter import Language, Node, Parser

from .base import FunctionInfo, LanguagePlugin
from .base import FunctionInfo, LanguagePlugin, build_edges, dedupe_calls

_TS_GRAMMAR = Language(tree_sitter_typescript.language_typescript())
_TSX_GRAMMAR = Language(tree_sitter_typescript.language_tsx())
Expand Down Expand Up @@ -360,6 +360,7 @@ def __init__(self, sources: dict[str, str]):
self.file_errors: dict[str, str] = {}
self.file_functions: dict[str, list[str]] = {}
self._specifier_cache: dict[tuple[str, str], _ModuleInfo | None] = {}
self._nodes: dict[str, Node] = {} # func id -> its AST node, pass 1 to pass 2

def analyze(self) -> dict:
self._parse_files()
Expand Down Expand Up @@ -658,31 +659,23 @@ def _register_function(
)
self.functions[func_id] = info
self.file_functions[mod.rel_path].append(func_id)
info._node = fn_node # type: ignore[attr-defined]
self._nodes[func_id] = fn_node
return info

# -- pass 2: resolve calls --

def _resolve_calls(self) -> None:
for info in self.functions.values():
mod = self.modules[info.file]
node = info._node # type: ignore[attr-defined]
node = self._nodes.pop(info.id)
body = node.child_by_field_name("body")

resolver = self._make_resolver(mod, info.class_name, _collect_local_types(node, self.sources_bytes[mod.rel_path]))
collector = _CallCollector(resolver)
if body is not None:
collector.visit(body)

line_index = {cl["lineno"]: cl for cl in info.code_lines}
seen: set[str] = set()
for lineno, target in collector.found:
if lineno in line_index and target not in line_index[lineno]["calls"]:
line_index[lineno]["calls"].append(target)
if target not in seen:
seen.add(target)
info.calls.append(target)
del info._node # type: ignore[attr-defined]
dedupe_calls(info, collector.found)

# -- module and export resolution --

Expand Down Expand Up @@ -872,11 +865,7 @@ def resolve(func_node: Node, is_new: bool = False) -> str | None:
# -- output --

def _to_response(self) -> dict:
edges = []
for info in self.functions.values():
for cl in info.code_lines:
for target in cl["calls"]:
edges.append({"source": info.id, "target": target, "line": cl["lineno"]})
edges = build_edges(self.functions)
files = [
{
"path": rel_path,
Expand Down
Loading