|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from fnmatch import fnmatch |
| 4 | +from pathlib import PurePosixPath |
| 5 | + |
| 6 | +from backend.app.services.graph.models import CodeGraphEdge, CodeGraphNode |
| 7 | + |
| 8 | +LOCKFILE_NAMES = {"uv.lock", "package-lock.json", "pnpm-lock.yaml", "yarn.lock"} |
| 9 | +GENERATED_DIR_NAMES = { |
| 10 | + ".next", |
| 11 | + ".nuxt", |
| 12 | + ".svelte-kit", |
| 13 | + "build", |
| 14 | + "coverage", |
| 15 | + "dist", |
| 16 | + "htmlcov", |
| 17 | + "out", |
| 18 | + "target", |
| 19 | +} |
| 20 | +GENERATED_FILE_SUFFIXES = { |
| 21 | + ".bundle.js", |
| 22 | + ".bundle.css", |
| 23 | + ".d.ts", |
| 24 | + ".generated.py", |
| 25 | + ".generated.ts", |
| 26 | + ".generated.tsx", |
| 27 | + ".min.css", |
| 28 | + ".min.js", |
| 29 | +} |
| 30 | +GENERATED_FILE_NAMES = LOCKFILE_NAMES |
| 31 | +VENDOR_DIR_NAMES = { |
| 32 | + ".git", |
| 33 | + ".hg", |
| 34 | + ".svn", |
| 35 | + ".venv", |
| 36 | + "node_modules", |
| 37 | + "site-packages", |
| 38 | + "vendor", |
| 39 | + "vendors", |
| 40 | + "venv", |
| 41 | +} |
| 42 | +TEST_DIR_NAMES = {"__tests__", "e2e", "spec", "specs", "test", "tests"} |
| 43 | + |
| 44 | + |
| 45 | +def normalize_file_path(file_path: str) -> str: |
| 46 | + return file_path.replace("\\", "/").strip("/") |
| 47 | + |
| 48 | + |
| 49 | +def is_test_file(file_path: str, test_glob: str | None = None) -> bool: |
| 50 | + normalized = normalize_file_path(file_path) |
| 51 | + lowered = normalized.lower() |
| 52 | + if test_glob and fnmatch(normalized, test_glob): |
| 53 | + return True |
| 54 | + name = lowered.rsplit("/", 1)[-1] |
| 55 | + parts = set(PurePosixPath(lowered).parts) |
| 56 | + return ( |
| 57 | + name.startswith("test_") |
| 58 | + or name.endswith("_test.py") |
| 59 | + or name.endswith("_test.go") |
| 60 | + or ".test." in name |
| 61 | + or ".spec." in name |
| 62 | + or bool(parts & TEST_DIR_NAMES) |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def is_generated_file(file_path: str) -> bool: |
| 67 | + normalized = normalize_file_path(file_path) |
| 68 | + lowered = normalized.lower() |
| 69 | + name = lowered.rsplit("/", 1)[-1] |
| 70 | + parts = set(PurePosixPath(lowered).parts) |
| 71 | + return ( |
| 72 | + name in GENERATED_FILE_NAMES |
| 73 | + or any(name.endswith(suffix) for suffix in GENERATED_FILE_SUFFIXES) |
| 74 | + or bool(parts & GENERATED_DIR_NAMES) |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def is_vendor_file(file_path: str) -> bool: |
| 79 | + parts = set(PurePosixPath(normalize_file_path(file_path).lower()).parts) |
| 80 | + return bool(parts & VENDOR_DIR_NAMES) |
| 81 | + |
| 82 | + |
| 83 | +def is_wiki_noise_file(file_path: str) -> bool: |
| 84 | + return is_test_file(file_path) or is_generated_file(file_path) or is_vendor_file(file_path) |
| 85 | + |
| 86 | + |
| 87 | +def is_wiki_noise_node(node: CodeGraphNode) -> bool: |
| 88 | + if node.metadata.get("external"): |
| 89 | + return True |
| 90 | + return bool(node.file_path and is_wiki_noise_file(node.file_path)) |
| 91 | + |
| 92 | + |
| 93 | +def filter_wiki_graph( |
| 94 | + nodes: list[CodeGraphNode], |
| 95 | + edges: list[CodeGraphEdge], |
| 96 | +) -> tuple[list[CodeGraphNode], list[CodeGraphEdge]]: |
| 97 | + filtered_nodes = [node for node in nodes if not is_wiki_noise_node(node)] |
| 98 | + node_ids = {node.id for node in filtered_nodes} |
| 99 | + filtered_edges = [ |
| 100 | + edge for edge in edges if edge.source_id in node_ids and edge.target_id in node_ids |
| 101 | + ] |
| 102 | + return filtered_nodes, filtered_edges |
0 commit comments