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.

fix(health): drop clones between shadcn/ui kit files (#2980)

Raghav Chamadiyad64600a37db3
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.py
index 2d3fb8029..a48eee0e4 100644
--- a/packages/core/src/repowise/core/analysis/health/engine.py
+++ b/packages/core/src/repowise/core/analysis/health/engine.py
@@ -20,7 +20,7 @@ from __future__ import annotations
import asyncio
import os
from datetime import UTC, datetime
-from pathlib import Path
+from pathlib import Path, PurePosixPath
from typing import Any
import structlog
@@ -38,6 +38,7 @@ from ...ingestion.git_indexer.function_blame import (
from ...ingestion.package_roots import module_for as _module_for
from ...ingestion.package_roots import package_roots_from_paths as _package_roots
from ...ingestion.package_roots import scan_package_roots as _scan_package_roots
+from ...installed_components import is_installed_component
from ...test_paths import paired_test_names
from ..dead_code.file_reachability import file_dependency_neighbors
from ..graph_view import HasEdge, ImportEdgeView
@@ -49,6 +50,7 @@ from .complexity import FileComplexity, FunctionComplexity, walk_file
from .coverage import is_test_file as _coverage_is_test_file
from .dataflow import FileDataflowCache
from .duplication import ClonePair, DuplicationReport
+from .duplication.detector import clone_ranges, union_line_count
from .duplication.isolation import detect_clones_with_isolation as detect_clones
from .finding_identity import SYMBOL_INDEX_KEY, SYMBOL_KEY, SYMBOL_LINE_KEY
from .function_blame_rollup import blame_commit_entries, commit_spans
@@ -524,6 +526,22 @@ def _commit_entries(fcx: FileComplexity, git_meta: dict) -> list:
return list(git_meta.get("function_commit_shas") or ())
+def _partner(file_path: str, pair: ClonePair) -> str:
+ """The other file of *pair*: *file_path* itself for a clone inside it."""
+ return pair.file_b if pair.file_a == file_path else pair.file_a
+
+
+def _pct_of_kept(
+ file_path: str, clones: list[ClonePair], kept: list[ClonePair], dup_pct: float | None
+) -> float | None:
+ """*dup_pct* scaled to the lines the *kept* clones still cover."""
+ if len(kept) == len(clones) or not dup_pct:
+ return dup_pct
+ covered = union_line_count(clone_ranges(file_path, clones))
+ share = union_line_count(clone_ranges(file_path, kept)) / covered if covered else 0.0
+ return round(dup_pct * share, 2)
+
+
def _read_source_lines(abs_path: str, read_source: SourceReader) -> list[str] | None:
"""Read a file's source as 1-indexed lines for the Extract Helper snippet.
@@ -726,6 +744,7 @@ class HealthAnalyzer:
# answer cannot live on the cached walk.
self._project_name: str | None = None
self._origins: dict[str, CodeOrigin] = {}
+ self._installed_ui_dirs: dict[str, tuple[PurePosixPath, ...]] = {}
# Every source read in the pass. Defaults to the working tree; a
# revision comparison supplies bytes instead.
self.read_source: SourceReader = source_reader or disk_source_reader
@@ -1427,6 +1446,30 @@ class HealthAnalyzer:
project=self._project(),
)
+ def _installed(self, path: str) -> bool:
+ return self.repo_root is not None and is_installed_component(
+ self.repo_root, path, self._installed_ui_dirs
+ )
+
+ def _without_kit_clones(
+ self, file_path: str, clones: list[ClonePair], dup_pct: float | None
+ ) -> tuple[list[ClonePair], float | None]:
+ """*clones* less those both of whose sides are components a component
+ CLI installed.
+
+ Copies of one upstream kit (shadcn/ui's ``dialog.tsx`` and
+ ``alert-dialog.tsx``, or the repeated item wrappers inside one of them)
+ are similar by design, so such a clone is not duplication anyone here
+ wrote. Every other finding on those files stands, and a clone with a
+ file outside the kit still counts.
+ The file's duplication share shrinks with the lines the dropped clones
+ alone covered.
+ """
+ if not clones or not self._installed(file_path):
+ return clones, dup_pct
+ kept = [p for p in clones if not self._installed(_partner(file_path, p))]
+ return kept, _pct_of_kept(file_path, clones, kept, dup_pct)
+
def _save_walk_cache(self) -> None:
"""Persist the walk entries this pass used or produced, if any."""
if self._walk_cache is not None:
@@ -1513,8 +1556,11 @@ class HealthAnalyzer:
covered_lines: set[int] = set(cov.get("covered_lines") or ()) if cov else set()
total_coverable_lines = int(cov.get("total_coverable_lines", 0)) if cov else 0
- clones = dup_report.pairs_by_file.get(file_path, [])
- dup_pct = dup_report.duplication_pct.get(file_path)
+ clones, dup_pct = self._without_kit_clones(
+ file_path,
+ dup_report.pairs_by_file.get(file_path, []),
+ dup_report.duplication_pct.get(file_path),
+ )
# Read only for clone-bearing files, keeping the read proportional.
source_lines = (
_read_source_lines(pf.file_info.abs_path, self.read_source) if clones else None
diff --git a/packages/core/src/repowise/core/installed_components.py b/packages/core/src/repowise/core/installed_components.py
new file mode 100644
index 000000000..204e6b9f9
--- /dev/null
+++ b/packages/core/src/repowise/core/installed_components.py
@@ -0,0 +1,101 @@
+"""Files a component CLI copied into the repository, read from the CLI's config.
+
+shadcn/ui's ``add`` command writes its primitive wrappers (``dialog.tsx``,
+``context-menu.tsx``) into the directory that ``components.json`` names as the
+``ui`` alias. The copies are near-identical to one another by design, so a
+clone detector reads the kit as duplication; the health pass drops clones
+between two kit files and keeps every other finding on them, since teams edit
+their copies. Two pieces of evidence must agree: the config file declares the
+directory, and the file carries a registry component's name directly in it. A
+``components/ui`` folder with no ``components.json`` above it, and a team's
+own component, story or test kept beside the kit (``PathTooltip.tsx``,
+``button.stories.tsx``), are not kit files.
+
+Ceilings: the match is by name, so a heavily edited copy still counts as kit
+for clones between kit files; and the ``@/`` alias is taken to mean the
+config's directory or its ``src/``, without reading tsconfig ``paths``.
+"""
+
+from __future__ import annotations
+
+import json
+import os
+from pathlib import Path, PurePosixPath
+
+__all__ = ["is_installed_component"]
+
+_CONFIG = "components.json"
+# Where each path alias a ``components.json`` uses points, relative to the
+# config's directory: the tsconfig ``@/*`` / ``~/*`` alias maps to the project
+# root or its ``src/``.
+_ALIAS_ROOTS: dict[str, tuple[str, ...]] = {"@/": ("", "src/"), "~/": ("", "src/")}
+# The file names ``shadcn add`` writes into the ``ui`` directory.
+_REGISTRY_UI_NAMES = frozenset(
+ {
+ "accordion", "alert", "alert-dialog", "aspect-ratio", "avatar", "badge",
+ "breadcrumb", "button", "button-group", "calendar", "card", "carousel",
+ "chart", "checkbox", "collapsible", "combobox", "command", "context-menu",
+ "dialog", "drawer", "dropdown-menu", "empty", "field", "form", "hover-card",
+ "input", "input-group", "input-otp", "item", "kbd", "label", "menubar",
+ "native-select", "navigation-menu", "pagination", "popover", "progress",
+ "radio-group", "resizable", "scroll-area", "select", "separator", "sheet",
+ "sidebar", "skeleton", "slider", "sonner", "spinner", "switch", "table",
+ "tabs", "textarea", "toast", "toaster", "toggle", "toggle-group", "tooltip",
+ "use-toast",
+ }
+)
+_COMPONENT_SUFFIXES = frozenset({".tsx", ".ts", ".jsx", ".js"})
+
+
+def _aliases(config_dir: Path) -> dict:
+ """The ``aliases`` table of a ``components.json`` in *config_dir*, or empty."""
+ try:
+ data = json.loads((config_dir / _CONFIG).read_text(encoding="utf-8"))
+ except (OSError, ValueError):
+ return {}
+ aliases = data.get("aliases") if isinstance(data, dict) else None
+ return aliases if isinstance(aliases, dict) else {}
+
+
+def _ui_alias(aliases: dict) -> str | None:
+ """The ``ui`` alias; configs written before it existed put the kit under
+ ``<components>/ui``."""
+ ui = aliases.get("ui")
+ if isinstance(ui, str):
+ return ui
+ components = aliases.get("components")
+ return f"{components}/ui" if isinstance(components, str) else None
+
+
+def _ui_dirs(config_dir: Path) -> tuple[PurePosixPath, ...]:
+ """The ``ui`` directories a ``components.json`` in *config_dir* declares,
+ relative to *config_dir*; empty when there is no readable config."""
+ ui = _ui_alias(_aliases(config_dir))
+ for prefix, roots in _ALIAS_ROOTS.items():
+ if ui is not None and ui.startswith(prefix):
+ rest = ui[len(prefix) :].strip("/")
+ return tuple(PurePosixPath(root + rest) for root in roots)
+ return ()
+
+
+def is_installed_component(
+ repo_root: str | os.PathLike[str],
+ rel_path: str,
+ cache: dict[str, tuple[PurePosixPath, ...]],
+) -> bool:
+ """Whether *rel_path* is a registry component directly in a ``ui``
+ directory that a ``components.json`` in one of its ancestors declares.
+
+ *cache* maps a repo-relative directory to its declared ``ui`` directories,
+ so each directory's config is read once per pass.
+ """
+ path = PurePosixPath(rel_path.replace("\\", "/"))
+ if path.suffix not in _COMPONENT_SUFFIXES or path.stem not in _REGISTRY_UI_NAMES:
+ return False
+ for base in path.parents:
+ key = base.as_posix()
+ if key not in cache:
+ cache[key] = _ui_dirs(Path(repo_root, key))
+ if any(path.parent == base / ui for ui in cache[key]):
+ return True
+ return False
diff --git a/tests/unit/test_installed_components.py b/tests/unit/test_installed_components.py
new file mode 100644
index 000000000..d8f233279
--- /dev/null
+++ b/tests/unit/test_installed_components.py
@@ -0,0 +1,101 @@
+"""Files a component CLI installed, decided from its components.json, and the
+clones between them the health pass drops."""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+import pytest
+
+from repowise.core.analysis.health.duplication import ClonePair
+from repowise.core.analysis.health.engine import HealthAnalyzer
+from repowise.core.installed_components import is_installed_component
+
+
+def _config(directory: Path, aliases: dict[str, str]) -> None:
+ directory.mkdir(parents=True, exist_ok=True)
+ payload = {"$schema": "https://ui.shadcn.com/schema.json", "aliases": aliases}
+ (directory / "components.json").write_text(json.dumps(payload), encoding="utf-8")
+
+
+_ALIASES = {"components": "@/components", "utils": "@/lib/utils", "ui": "@/components/ui"}
+
+
+ ("config_dir", "aliases", "path", "expected"),
+ [
+ # A sub-project's config with the ui alias under src/ (Roo-Code, cline).
+ ("webview-ui", _ALIASES, "webview-ui/src/components/ui/dialog.tsx", True),
+ ("webview-ui", _ALIASES, "webview-ui/components/ui/dialog.tsx", True),
+ ("", _ALIASES, "components/ui/use-toast.ts", True),
+ # Configs from before the ui alias put the kit under <components>/ui.
+ ("", {"components": "@/components"}, "components/ui/button.tsx", True),
+ # Must not change: the team's own files beside the kit, other folders,
+ # other sub-projects, aliases that do not resolve to a path.
+ ("webview-ui", _ALIASES, "webview-ui/src/components/ui/PathTooltip.tsx", False),
+ ("webview-ui", _ALIASES, "webview-ui/src/components/ui/button.stories.tsx", False),
+ ("webview-ui", _ALIASES, "webview-ui/src/components/ui/__tests__/button.tsx", False),
+ ("webview-ui", _ALIASES, "webview-ui/src/components/chat/dialog.tsx", False),
+ ("webview-ui", _ALIASES, "other/src/components/ui/dialog.tsx", False),
+ ("", {"ui": "@workspace/ui/components"}, "components/ui/dialog.tsx", False),
+ ],
+)
+def test_ui_dir_from_components_json(
+ tmp_path: Path, config_dir: str, aliases: dict[str, str], path: str, expected: bool
+) -> None:
+ _config(tmp_path / config_dir, aliases)
+ assert is_installed_component(tmp_path, path, {}) is expected
+
+
+def test_no_config_means_no_claim(tmp_path: Path) -> None:
+ # A components/ui folder alone is the repository's own code.
+ assert is_installed_component(tmp_path, "components/ui/dialog.tsx", {}) is False
+
+
+def test_malformed_config_is_ignored(tmp_path: Path) -> None:
+ (tmp_path / "components.json").write_text("{not json", encoding="utf-8")
+ assert is_installed_component(tmp_path, "components/ui/dialog.tsx", {}) is False
+
+
+def _pair(a: str, b: str, lines: tuple[int, int] = (1, 40)) -> ClonePair:
+ start, end = lines
+ return ClonePair(
+ file_a=a,
+ file_b=b,
+ a_start_line=start,
+ a_end_line=end,
+ b_start_line=start,
+ b_end_line=end,
+ token_count=200,
+ )
+
+
+def test_clones_between_kit_files_are_dropped(tmp_path: Path) -> None:
+ _config(tmp_path / "web", _ALIASES)
+ analyzer = HealthAnalyzer(graph=None, repo_root=tmp_path)
+ kit = "web/src/components/ui/dialog.tsx"
+ kit_pair = _pair(kit, "web/src/components/ui/alert-dialog.tsx")
+ inside = _pair(kit, kit, (60, 70))
+ own_pair = _pair(kit, "web/src/components/Header.tsx", (50, 59))
+ kept, pct = analyzer._without_kit_clones(kit, [kit_pair, inside, own_pair], 50.0)
+ # The clone with the team's own file still counts; the share shrinks to it.
+ assert kept == [own_pair]
+ assert pct == 8.2 # 50% scaled by 10 of the 61 cloned lines
+
+
+def test_clones_of_a_file_outside_the_kit_are_untouched(tmp_path: Path) -> None:
+ _config(tmp_path / "web", _ALIASES)
+ analyzer = HealthAnalyzer(graph=None, repo_root=tmp_path)
+ own = "web/src/components/Header.tsx"
+ pair = _pair(own, "web/src/components/ui/dialog.tsx")
+ assert analyzer._without_kit_clones(own, [pair], 30.0) == ([pair], 30.0)
+
+
+def test_no_components_json_keeps_every_clone(tmp_path: Path) -> None:
+ analyzer = HealthAnalyzer(graph=None, repo_root=tmp_path)
+ pair = _pair("components/ui/dialog.tsx", "components/ui/alert-dialog.tsx")
+ assert analyzer._without_kit_clones("components/ui/dialog.tsx", [pair], 80.0) == (
+ [pair],
+ 80.0,
+ )