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): refuse Java extraction spans that hold a switch-rule yield (#3415)

ump45noseff4315a361d7
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/languages.py b/packages/core/src/repowise/core/analysis/health/complexity/languages.py
index 104233d86..f845a126c 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/languages.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/languages.py
@@ -506,6 +506,8 @@ _JAVA = LanguageNodeMap(
catch_kinds=frozenset({"catch_clause"}),
switch_kinds=frozenset({"switch_expression", "switch_statement"}),
case_kinds=frozenset({"switch_block_statement_group", "switch_rule"}),
+ # A switch-rule ``yield t;`` parses as ``yield_statement`` (tree-sitter-java).
+ yield_kinds=frozenset({"yield_statement"}),
boolean_operator_kinds=frozenset(),
boolean_operator_text_kinds=frozenset({"binary_expression"}),
class_kinds=frozenset({"class_declaration"}),
diff --git a/tests/unit/health/test_refactoring_extract_method.py b/tests/unit/health/test_refactoring_extract_method.py
index aa40f2b0b..bedae47e8 100644
--- a/tests/unit/health/test_refactoring_extract_method.py
+++ b/tests/unit/health/test_refactoring_extract_method.py
@@ -20,6 +20,7 @@ from repowise.core.analysis.health.refactoring.extract_method import (
recovered_share,
)
from repowise.core.analysis.health.refactoring.models import RefactoringContext
+from repowise.core.ingestion.parser import _get_language
# A long function whose tail (compute-average loop) is a clean extraction.
_PROCESS = """
@@ -130,6 +131,54 @@ def test_a_comment_opening_the_else_arm_still_yields_the_same_extraction():
assert best.slice_nloc >= 5
+_JAVA_SWITCH_RULE_YIELD = """
+class A {
+ int pick(int[] xs, int k) {
+ int total = 0;
+ for (int x : xs) {
+ total += x;
+ }
+ int r = switch (k) {
+ case 1 -> {
+ int t = 0;
+ for (int x : xs) {
+ if (x > 0) {
+ t += x;
+ }
+ if (x > 10) {
+ t -= 1;
+ }
+ }
+ yield t;
+ }
+ default -> 0;
+ };
+ return r + total;
+ }
+}
+"""
+
+
+def test_a_java_switch_rule_yield_refuses_spans_that_hold_it():
+ # Lifting a span that contains a switch-rule `yield` into a helper would
+ # move the yield out of the switch rule it belongs to, so the plan cannot
+ # compile. Spans that stop before the yield stay offered.
+ if _get_language("java") is None:
+ pytest.skip("tree-sitter language pack missing for java")
+ res = analyze_file("m.java", "java", _JAVA_SWITCH_RULE_YIELD.encode(), flagged_only=False)
+ if res.stats.functions_seen == 0:
+ pytest.skip("tree-sitter language pack missing for java")
+ fn = res.functions[0]
+ lmap = get_language_map("java")
+ extractions = find_extractions(fn, lmap)
+ assert extractions, "expected at least one extraction"
+ # The yield is on line 19; no offered span may end on it, and the span
+ # that stops just before it stays the best one.
+ assert all(extraction.end_line != 19 for extraction in extractions)
+ best = extractions[0]
+ assert (best.start_line, best.end_line) == (10, 18)
+
+
def test_no_extraction_when_every_span_has_a_jump():
# A guard-clause cascade: every span contains a return, so nothing is a
# single-exit slice -> no candidate.