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(refactoring): stop offering an Extract Method span that is nearly the whole body (#2952)

Raghav Chamadiya2c89a75caea4
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
index c027262ce..752684e1c 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
@@ -15,8 +15,9 @@ it cuts at statement boundaries within a single block (so never a partial
branch or a mid-``try`` split), contains no control-flow jump that leaves the
region (``return`` / ``raise`` / ``break`` / ``continue`` -> single clean exit),
removes real complexity (at least one decision point), is substantial enough to
-matter, and has at most one return and a small parameter list. Everything else
-is suppressed -- ten great extractions, not two hundred maybes.
+matter without being nearly the whole body, and has at most one return and a
+small parameter list. Everything else is suppressed -- ten great extractions,
+not two hundred maybes.
Line-based liveness over D2's def/use occurrences realises the IN/OUT inference;
the CFG/jump scan realises the single-exit predicate. The jump and nested-scope
@@ -50,6 +51,13 @@ _MAX_PARAMS = 5 # too many ins => the span is not cohesive
_MAX_RETURNS = 1 # a single clean return (v1); multi-output is future work
# Backstop against a pathological function producing too many sub-ranges.
_MAX_CANDIDATES = 4000
+# A span holding this share of the body's code lines or more is the whole
+# function under another name, not a split: what stays behind is a guard, a
+# wrapper (``try`` / ``with``) or the final ``return``. Measured on 301 plans
+# from seven repos (Python, TS, Go, Java, Rust): 52 sat at or above 0.75, and
+# nearly all of those at 0.8 or more left only such a shell. The span offered
+# instead may be the same block minus a statement, just under the cut.
+_MAX_BODY_SHARE = 0.75
@dataclass(frozen=True)
@@ -97,6 +105,8 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[
# The statement container of the body (Go nests it in a ``statement_list``
# inside the ``block``); spans covering it whole are not extractions.
body_container = _unwrap_container(body, lmap.block_kinds)
+ lines = _function_lines(fn_node)
+ max_slice_nloc = _MAX_BODY_SHARE * _stmts_nloc(body_container.named_children, lines)
def_lines, use_lines = _var_lines(analysis.def_use)
declared_first = _declared_before_read(analysis.def_use)
@@ -120,7 +130,6 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[
else None
)
- lines = _function_lines(fn_node)
out: list[Extraction] = []
evaluated = 0
for block, loop in _all_blocks(fn_node, lmap.block_kinds, scope_kinds, lmap.loop_kinds):
@@ -147,9 +156,7 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[
nested_prefix.append(
nested_prefix[-1] + (1 if _holds_a_named_nested_function([st], lmap) else 0)
)
- code_prefix.append(
- code_prefix[-1] + len(_code_line_numbers(st, lines, drop_docstrings=True))
- )
+ code_prefix.append(code_prefix[-1] + _stmts_nloc([st], lines))
for i in range(n):
for j in range(i, n):
evaluated += 1
@@ -172,7 +179,7 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[
if has_jump or decisions < _MIN_CCN_REMOVED:
continue
slice_nloc = code_prefix[j + 1] - code_prefix[i]
- if slice_nloc < _MIN_SLICE_NLOC:
+ if not _MIN_SLICE_NLOC <= slice_nloc < max_slice_nloc:
continue
span = stmts[i : j + 1]
s = span[0].start_point[0] + 1
@@ -213,6 +220,11 @@ def _function_lines(fn_node: Node) -> list[str]:
return [""] * fn_node.start_point[0] + text.splitlines()
+def _stmts_nloc(stmts: list[Node], lines: list[str]) -> int:
+ """Code lines of *stmts* by the walker's NLOC rule, summed per statement."""
+ return sum(len(_code_line_numbers(st, lines, drop_docstrings=True)) for st in stmts)
+
+
def _sorted(candidates: list[Extraction]) -> list[Extraction]:
return sorted(
candidates,
diff --git a/tests/unit/health/refactoring_corpus_fixture.py b/tests/unit/health/refactoring_corpus_fixture.py
index a366334ef..81953e6ae 100644
--- a/tests/unit/health/refactoring_corpus_fixture.py
+++ b/tests/unit/health/refactoring_corpus_fixture.py
@@ -52,26 +52,34 @@ def extractions_for(language: str, filename: str) -> dict[str, list[dict[str, An
"""Every span the slicer offers, per function, for one corpus file.
Keyed by function name so a golden diff names the archetype that moved.
+
+ The archetypes are small on purpose, so most of their spans hold nearly the
+ whole body. The body-share gate is a worth rule, not a soundness one, and
+ left on it would hide the soundness gates this corpus exists to pin; it is
+ lifted here and tested on its own.
"""
+ from unittest import mock
+
from repowise.core.analysis.health.complexity.languages import get_language_map
+ from repowise.core.analysis.health.dataflow import slice as slicer
from repowise.core.analysis.health.dataflow.analyze import analyze_file
- from repowise.core.analysis.health.dataflow.slice import find_extractions
lmap = get_language_map(language)
result = analyze_file(filename, language, source_for(filename), flagged_only=False)
out: dict[str, list[dict[str, Any]]] = {}
- for analysis in result.functions:
- out[analysis.name] = [
- {
- "start_line": item.start_line,
- "end_line": item.end_line,
- "params": list(item.params),
- "returns": list(item.returns),
- "slice_nloc": item.slice_nloc,
- "ccn_removed": item.ccn_removed,
- }
- for item in find_extractions(analysis, lmap)
- ]
+ with mock.patch.object(slicer, "_MAX_BODY_SHARE", float("inf")):
+ for analysis in result.functions:
+ out[analysis.name] = [
+ {
+ "start_line": item.start_line,
+ "end_line": item.end_line,
+ "params": list(item.params),
+ "returns": list(item.returns),
+ "slice_nloc": item.slice_nloc,
+ "ccn_removed": item.ccn_removed,
+ }
+ for item in slicer.find_extractions(analysis, lmap)
+ ]
return out
diff --git a/tests/unit/health/test_refactoring_extract_method.py b/tests/unit/health/test_refactoring_extract_method.py
index 4908e65d6..33de182eb 100644
--- a/tests/unit/health/test_refactoring_extract_method.py
+++ b/tests/unit/health/test_refactoring_extract_method.py
@@ -150,6 +150,7 @@ def test_extractions_are_deterministic():
def _find_extractions_reference(analysis, lmap):
from repowise.core.analysis.health.dataflow.slice import (
+ _MAX_BODY_SHARE,
_MAX_CANDIDATES,
_MAX_PARAMS,
_MAX_RETURNS,
@@ -159,11 +160,13 @@ def _find_extractions_reference(analysis, lmap):
Extraction,
_all_blocks,
_declared_before_read,
+ _function_lines,
_infer_in_out,
_loop_carry_free,
_outs_definitely_assigned,
_sorted,
_span_metrics,
+ _stmts_nloc,
_unwrap_container,
_var_lines,
)
@@ -175,6 +178,8 @@ def _find_extractions_reference(analysis, lmap):
if body is None:
return []
body_container = _unwrap_container(body, lmap.block_kinds)
+ lines = _function_lines(fn_node)
+ body_nloc = _stmts_nloc(body_container.named_children, lines)
def_lines, use_lines = _var_lines(analysis.def_use)
declared_first = _declared_before_read(analysis.def_use)
decision_kinds = (
@@ -218,8 +223,8 @@ def _find_extractions_reference(analysis, lmap):
decisions, has_jump = _span_metrics(span, decision_kinds, jump_kinds, scope_kinds)
if has_jump or decisions < _MIN_CCN_REMOVED:
continue
- slice_nloc = sum(st.end_point[0] - st.start_point[0] + 1 for st in span)
- if slice_nloc < _MIN_SLICE_NLOC:
+ slice_nloc = _stmts_nloc(span, lines)
+ if slice_nloc < _MIN_SLICE_NLOC or slice_nloc >= _MAX_BODY_SHARE * body_nloc:
continue
s = span[0].start_point[0] + 1
e = span[-1].end_point[0] + 1
@@ -524,11 +529,17 @@ def test_slice_nloc_counts_code_lines_not_comments():
total += limit
else:
total += x
+ total = min(total, limit * 10)
+ total = max(total, 0)
+ print(total)
return total
"""
fn = _first(src)
lmap = get_language_map("python")
- loop = [c for c in find_extractions(fn, lmap) if c.end_line - c.start_line >= 7]
+ # Spans ending with the loop (line 11), the ones holding the comments.
+ loop = [
+ c for c in find_extractions(fn, lmap) if c.end_line == 11 and c.end_line - c.start_line >= 5
+ ]
assert loop
assert all(c.slice_nloc <= c.end_line - c.start_line + 1 - 2 for c in loop)
@@ -695,3 +706,30 @@ def test_plan_params_leave_out_a_loop_counter_the_span_declares():
plan = suggestions[0].plan
assert plan["params"] == ["a", "n"]
assert plan["returns"] == ["total"]
+
+
+def test_a_span_holding_nearly_the_whole_body_is_not_a_split():
+ # Everything but the closing return: lifting it would leave a function that
+ # only calls the helper, the smell moved under a new name.
+ lmap = get_language_map("python")
+ src = """
+ def tally(items, limit):
+ total = 0
+ seen = 0
+ for x in items:
+ if x > limit:
+ total += limit
+ else:
+ total += x
+ seen += 1
+ return total
+ """
+ assert find_extractions(_first(src), lmap) == []
+
+
+def test_a_span_leaving_real_work_behind_is_still_offered():
+ lmap = get_language_map("python")
+ fn = _first(_PROCESS)
+ (best, *_) = find_extractions(fn, lmap)
+ assert "average" in best.returns
+ assert best.end_line < fn.end_line