Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(health): name an Extract Method helper in the file's language (#2895)
Chaitanya Reddybe6689b2407c
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/extract_method.py b/packages/core/src/repowise/core/analysis/health/refactoring/extract_method.pyindex d9f51cb52..0f785d1f4 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/extract_method.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/extract_method.py@@ -69,7 +69,7 @@ from ..complexity.languages import get_language_mapfrom ..dataflow import find_extractionsfrom ..scoring import severity_deductionfrom .models import RefactoringContext, RefactoringSuggestion-from .naming import identifier_slug+from .naming import join_identifier, split_wordsfrom .registry import RefactoringDetector, effort_bucket, registerif TYPE_CHECKING:@@ -83,6 +83,24 @@ _UNINFORMATIVE_OUT = frozenset({"out", "result", "results", "value", "values", "ret", "tmp", "temp", "data", "item"})+# How each language that reaches this detector joins the words of a helper+# name. Only the languages the Extract Method slicer has a dialect for+# (``dataflow/dialects/__init__.py``) can appear here. C++ is deliberately+# absent: it has no single convention (the standard library is snake_case,+# Google style is PascalCase, Qt is camelCase), so it keeps the snake_case+# default rather than getting one answer that is wrong for most C++ repos.+_NAME_CONVENTION: dict[str, str] = {+ "go": "camelCase",+ "java": "camelCase",+ "typescript": "camelCase",+ "tsx": "camelCase",+ "javascript": "camelCase",+ "jsx": "camelCase",+ "svelte": "camelCase",+ "vue": "camelCase",+}+_SNAKE_CASE = "snake_case"+# The function-level structural biomarkers this detector answers. A function is# only offered an extraction when one of these flagged it, so the suggestion# list never exceeds (and stays consistent with) what health surfaces.@@ -154,7 +172,7 @@ class ExtractMethodDetector(RefactoringDetector):"span": {"start": best.start_line, "end": best.end_line},"params": list(best.params),"returns": list(best.returns),- "suggested_name": self._suggested_name(analysis, best),+ "suggested_name": self._suggested_name(analysis, best, ctx.language),},evidence={"slice_nloc": best.slice_nloc,@@ -205,8 +223,11 @@ class ExtractMethodDetector(RefactoringDetector):return best[0], best[1], best[3]@staticmethod- def _suggested_name(analysis: FunctionAnalysis, extraction: Extraction) -> str | None:- """A deterministic starting name for the lifted helper.+ def _suggested_name(+ analysis: FunctionAnalysis, extraction: Extraction, language: str | None = None+ ) -> str | None:+ """A deterministic starting name for the lifted helper, in *language*'s+ identifier convention.Same posture as Extract Helper (see ``naming``): anchor the name tosomething the plan already knows rather than guess what the block does.@@ -219,6 +240,14 @@ class ExtractMethodDetector(RefactoringDetector):repo's index, 545 (64%) come from the OUT value and 309 from theenclosing function.+ Convention is per language: Python, Rust and C++ keep ``compute_average``;+ Go, Java and the TypeScript/JavaScript family take ``computeAverage``.+ C++ gets no convention because it has no single one -- the standard+ library is snake_case and Google style is PascalCase, so a fixed answer+ would be wrong for as many repos as it fixed. The out value's own casing+ is kept as word boundaries rather than thrown away, so ``meanValue`` is+ ``computeMeanValue`` in Java, not ``compute_meanvalue``.+**Not unique within a file, by design.** Two functions in one file caneach produce a value with the same name, and both spans then get thesame ``compute_*``: 28 of those 854 plans, across 14 files, collide with@@ -228,11 +257,17 @@ class ExtractMethodDetector(RefactoringDetector):every persisted row. The name is a starting point every surface framesas editable, so the surfaces say to rename on a clash instead."""- if len(extraction.returns) == 1:- slug = identifier_slug(extraction.returns[0])- if slug and slug not in _UNINFORMATIVE_OUT:- return f"compute_{slug}"- return None+ if len(extraction.returns) != 1:+ return None+ out_words = split_words(extraction.returns[0])+ if not out_words:+ return None+ # ``_UNINFORMATIVE_OUT`` is keyed on the single-word slug, matching the+ # names it holds (``meanValue`` is a product, ``result`` is a role).+ if "_".join(out_words) in _UNINFORMATIVE_OUT:+ return None+ convention = _NAME_CONVENTION.get(language or "", _SNAKE_CASE)+ return join_identifier(["compute", *out_words], convention)@staticmethoddef _confidence(extraction: Extraction, share: float) -> str:diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/naming.py b/packages/core/src/repowise/core/analysis/health/refactoring/naming.pyindex c805eedbc..900ec5257 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/naming.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/naming.py@@ -38,3 +38,54 @@ def identifier_slug(label: str | None) -> str:if slug and slug[0].isdigit():slug = f"_{slug}"return slug+++def _starts_new_word(ch: str, current: list[str]) -> bool:+ """True when *ch* opens a new word after the letters collected in *current*.++ A capital that follows a lowercase letter or a digit starts a new word+ (``meanValue``, ``file2Name``). A capital after a capital does not, so a run+ of capitals stays one word.+ """+ if not current:+ return False+ if not ch.isupper():+ return False+ return current[-1].islower() or current[-1].isdigit()+++def split_words(label: str | None) -> list[str]:+ """*label* split into its words, lowercased.++ Splits on any non-alphanumeric run and on a lower-to-upper boundary, so+ ``meanValue``, ``mean_value`` and ``MeanValue`` all give ``["mean", "value"]``.+ A run of capitals stays one word (``HTTPStatus`` -> ``["httpstatus"]``),+ because splitting it needs a dictionary to tell ``HTTPS`` from ``HttpStatus``+ and this module names code, it does not read it.++ Letters are tested with ``str.isalnum`` rather than an ASCII character class,+ so a non-ASCII identifier keeps its characters instead of losing them to a+ dropped word, the same way ``identifier_slug`` treats them.+ """+ if not label:+ return []+ words: list[str] = []+ current: list[str] = []+ for ch in label:+ if ch.isalnum() and not _starts_new_word(ch, current):+ current.append(ch)+ continue+ words.append("".join(current))+ current = [ch] if ch.isalnum() else []+ words.append("".join(current))+ return [w.lower() for w in words if w]+++def join_identifier(words: list[str], convention: str) -> str:+ """*words* rendered in *convention*: ``"snake_case"`` or ``"camelCase"``."""+ if not words:+ return ""+ if convention == "camelCase":+ head, *rest = words+ return head + "".join(w.capitalize() for w in rest)+ return "_".join(words)diff --git a/tests/unit/health/test_refactoring_extract_method.py b/tests/unit/health/test_refactoring_extract_method.pyindex d2f7b623e..3d85c26a6 100644--- a/tests/unit/health/test_refactoring_extract_method.py+++ b/tests/unit/health/test_refactoring_extract_method.py@@ -365,6 +365,196 @@ def _ctx(src: str, findings):)+def _process_sources() -> dict[str, tuple[str, str]]:+ """``language -> (extension, source)`` for one shape in every language the+ slicer has a dialect for. The source is the same algorithm spelled the way+ each language writes it, so a name difference cannot come from the code."""+ py = textwrap.dedent(_PROCESS)+ java = textwrap.dedent(+ """+ class Demo {+ int process(int[] records, int threshold) {+ int errors = 0;+ java.util.List<Integer> results = new java.util.ArrayList<>();+ for (int r : records) {+ if (r < 0) {+ errors++;+ continue;+ }+ results.add(r);+ }+ int total = 0;+ int count = 0;+ for (int v : results) {+ if (v > threshold) {+ total += v;+ count++;+ } else {+ total -= v;+ }+ }+ int average = 0;+ if (count > 0) {+ average = total / count;+ }+ return average + errors;+ }+ }+ """+ )+ ts = textwrap.dedent(+ """+ function process(records: number[], threshold: number): number {+ const results: number[] = [];+ let errors = 0;+ for (const r of records) {+ if (r < 0) {+ errors += 1;+ continue;+ }+ results.push(r);+ }+ let total = 0;+ let count = 0;+ for (const v of results) {+ if (v > threshold) {+ total += v;+ count += 1;+ } else {+ total -= v;+ }+ }+ let average = 0;+ if (count > 0) {+ average = total / count;+ }+ return average + errors;+ }+ """+ )+ go = textwrap.dedent(+ """+ package main++ func process(records []int, threshold int) int {+ errors := 0+ results := []int{}+ for _, r := range records {+ if r < 0 {+ errors+++ continue+ }+ results = append(results, r)+ }+ total := 0+ count := 0+ for _, v := range results {+ if v > threshold {+ total += v+ count+++ } else {+ total -= v+ }+ }+ average := 0+ if count > 0 {+ average = total / count+ }+ return average + errors+ }+ """+ )+ rust = textwrap.dedent(+ """+ fn process(records: &[i32], threshold: i32) -> i32 {+ let mut errors = 0;+ let mut results: Vec<i32> = Vec::new();+ for r in records {+ if *r < 0 {+ errors += 1;+ continue;+ }+ results.push(*r);+ }+ let mut total = 0;+ let mut count = 0;+ for v in &results {+ if *v > threshold {+ total += v;+ count += 1;+ } else {+ total -= v;+ }+ }+ let mut average = 0;+ if count > 0 {+ average = total / count;+ }+ average + errors+ }+ """+ )+ cpp = textwrap.dedent(+ """+ int process(int* records, int n, int threshold) {+ int errors = 0;+ std::vector<int> results;+ for (int i = 0; i < n; i++) {+ if (records[i] < 0) {+ errors++;+ continue;+ }+ results.push_back(records[i]);+ }+ int total = 0;+ int count = 0;+ for (int v : results) {+ if (v > threshold) {+ total += v;+ count++;+ } else {+ total -= v;+ }+ }+ int average = 0;+ if (count > 0) {+ average = total / count;+ }+ return average + errors;+ }+ """+ )+ return {+ "python": ("py", py),+ "java": ("java", java),+ "typescript": ("ts", ts),+ "go": ("go", go),+ "rust": ("rs", rust),+ "cpp": ("cpp", cpp),+ }+++def _detect_in(language: str, ext: str, src: str):+ from repowise.core.ingestion.parser import _get_language++ if _get_language(language) is None:+ pytest.skip(f"tree-sitter language pack missing for {language}")+ res = analyze_file(f"m.{ext}", language, src.encode(), flagged_only=False)+ if res.stats.functions_seen == 0:+ pytest.skip(f"tree-sitter language pack missing for {language}")+ fn = res.functions[0]+ ctx = RefactoringContext(+ file_path=f"m.{ext}",+ language=language,+ nloc=100,+ findings=[_Finding("complex_method", fn.name, fn.start_line, 1.5)],+ function_analyses=res.functions,+ )+ plans = [s for s in ExtractMethodDetector().detect(ctx) if s.refactoring_type == "extract_method"]+ assert plans, f"no extract_method plan for {language}"+ return plans[0]++def test_detector_emits_suggestion_for_flagged_function():findings = [_Finding("complex_method", "process", line_start=2, health_impact=1.5)]suggestions = ExtractMethodDetector().detect(_ctx(_PROCESS, findings))@@ -415,10 +605,20 @@ class _Analysis:def test_suggested_name_unit():name = ExtractMethodDetector._suggested_name- # Exactly one OUT -> name the helper for what it produces.- assert name(_Analysis("process"), _Extraction(["average"])) == "compute_average"+ # Exactly one OUT -> name the helper for what it produces, in the language's+ # own convention.+ assert name(_Analysis("process"), _Extraction(["average"]), "python") == "compute_average"# Non-identifier characters normalised through the shared slug.- assert name(_Analysis("p"), _Extraction(["total-count"])) == "compute_total_count"+ assert name(_Analysis("p"), _Extraction(["total-count"]), "python") == "compute_total_count"+ # A call with no language keeps the snake_case default (the pre-existing+ # behaviour), so an unset ``ctx.language`` degrades rather than changes shape.+ assert name(_Analysis("process"), _Extraction(["average"])) == "compute_average"+++def test_suggested_name_none_cases_are_unchanged_by_the_language():+ """Control: the no-anchor rules this change must not touch. Each of these+ was ``None`` before it and must stay ``None`` after, in every language."""+ name = ExtractMethodDetector._suggested_name# No single OUT -> no anchor, so no name. The enclosing function's name# described the span's context, never the span, and collided with every# sibling plan in the file.@@ -431,6 +631,60 @@ def test_suggested_name_unit():assert name(_Analysis("process"), _Extraction(["result"])) is None# Nothing usable at all -> no name at all.assert name(_Analysis("___"), _Extraction([])) is None+ # And none of the above becomes a name just because the language is camelCase.+ for lang in ("java", "go", "typescript", "cpp", "python"):+ assert name(_Analysis("process"), _Extraction([]), lang) is None+ assert name(_Analysis("process"), _Extraction(["result"]), lang) is None+ assert name(_Analysis("process"), _Extraction(["___"]), lang) is None+++def test_suggested_name_follows_the_language_convention():+ name = ExtractMethodDetector._suggested_name+ # camelCase languages.+ for lang in ("java", "go", "typescript", "tsx", "javascript", "jsx", "svelte", "vue"):+ assert name(_Analysis("process"), _Extraction(["average"]), lang) == "computeAverage", lang+ # snake_case languages, and C++ which has no single convention.+ for lang in ("python", "rust", "cpp"):+ assert name(_Analysis("process"), _Extraction(["average"]), lang) == "compute_average", lang+ # An unknown language falls back rather than raising.+ assert name(_Analysis("process"), _Extraction(["average"]), "cobol") == "compute_average"+ # A language whose file has several words keeps all of them.+ assert (+ name(_Analysis("process"), _Extraction(["total_count"]), "java") == "computeTotalCount"+ )+++def test_suggested_name_keeps_the_out_values_own_casing():+ name = ExtractMethodDetector._suggested_name+ # ``meanValue`` must survive as word boundaries. Lowercasing the whole label+ # first (the old slug) gave ``compute_meanvalue``, neither camelCase nor+ # snake_case.+ assert name(_Analysis("process"), _Extraction(["meanValue"]), "java") == "computeMeanValue"+ assert name(_Analysis("process"), _Extraction(["mean_value"]), "java") == "computeMeanValue"+ assert name(_Analysis("process"), _Extraction(["MeanValue"]), "java") == "computeMeanValue"+ assert name(_Analysis("process"), _Extraction(["meanValue"]), "python") == "compute_mean_value"+ # A run of capitals stays one word: splitting it needs a dictionary.+ assert name(_Analysis("process"), _Extraction(["HTTPStatus"]), "java") == "computeHttpstatus"+ # The role-word guard still sees a multi-word out value's joined slug.+ assert name(_Analysis("process"), _Extraction(["item_value"]), "java") == "computeItemValue"+++ ("language", "expected"),+ [+ ("python", "compute_average"),+ ("rust", "compute_average"),+ ("cpp", "compute_average"),+ ("java", "computeAverage"),+ ("typescript", "computeAverage"),+ ("go", "computeAverage"),+ ],+)+def test_detector_names_the_helper_in_the_files_language(language: str, expected: str):+ ext, src = _process_sources()[language]+ plan = _detect_in(language, ext, src)+ assert plan.plan["returns"] == ["average"], language+ assert plan.plan["suggested_name"] == expected, languagedef test_detector_silent_without_matching_finding():diff --git a/tests/unit/health/test_refactoring_naming.py b/tests/unit/health/test_refactoring_naming.pyindex f69ee131a..98ea68bb7 100644--- a/tests/unit/health/test_refactoring_naming.py+++ b/tests/unit/health/test_refactoring_naming.py@@ -6,7 +6,11 @@ one thing per detector.from __future__ import annotations-from repowise.core.analysis.health.refactoring.naming import identifier_slug+from repowise.core.analysis.health.refactoring.naming import (+ identifier_slug,+ join_identifier,+ split_words,+)def test_lowercases_and_keeps_alphanumerics():@@ -36,3 +40,66 @@ def test_unusable_input_returns_empty_so_callers_can_pick_a_fallback():assert identifier_slug(None) == ""assert identifier_slug("---") == ""assert identifier_slug("...") == ""+++# -- word splitter --------------------------------------------------------------+++def test_split_words_separates_every_casing_of_one_name():+ # The three spellings a reader uses for the same idea must give one word+ # list, or a name's shape would depend on how its author capitalised it.+ assert split_words("meanValue") == ["mean", "value"]+ assert split_words("mean_value") == ["mean", "value"]+ assert split_words("MeanValue") == ["mean", "value"]+ assert split_words("mean-value") == ["mean", "value"]+ assert split_words("mean.value") == ["mean", "value"]+++def test_split_words_keeps_a_capital_run_as_one_word():+ # Splitting a run of capitals needs a dictionary (is ``HTTPServer``+ # ``HTTP``+``Server`` or ``HTTP``+``server``?), which this module does not+ # have; one word is the honest answer.+ assert split_words("HTTPStatus") == ["httpstatus"]+ assert split_words("IDs") == ["ids"]+++def test_split_words_drops_separators_and_digit_boundaries():+ assert split_words("__a__b__") == ["a", "b"]+ assert split_words("api2client") == ["api2client"]+ assert split_words("2fa") == ["2fa"]+ # A digit-to-capital boundary splits, the same as lower-to-capital.+ assert split_words("file2Name") == ["file2", "name"]+++def test_split_words_keeps_non_ascii_letters():+ # An ASCII-only character class would drop the accented letters and the+ # remaining word would silently lose its meaning.+ assert split_words("café") == ["café"]+ assert split_words("naïveValue") == ["naïve", "value"]+ assert split_words("método") == ["método"]+++def test_split_words_of_unusable_input_is_empty():+ assert split_words("") == []+ assert split_words(None) == []+ assert split_words("___") == []+++# -- joiner ---------------------------------------------------------------------+++def test_join_identifier_renders_each_convention():+ words = ["compute", "mean", "value"]+ assert join_identifier(words, "snake_case") == "compute_mean_value"+ assert join_identifier(words, "camelCase") == "computeMeanValue"+++def test_join_identifier_uses_the_first_word_as_lowercase_head():+ # The head is not capitalised: a lifted helper is a local, and a leading+ # capital would make it a type name in Go and Java.+ assert join_identifier(["compute", "average"], "camelCase") == "computeAverage"+++def test_join_identifier_of_no_words_is_empty():+ assert join_identifier([], "camelCase") == ""+ assert join_identifier([], "snake_case") == ""