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): ignore comments in switch blocks (#4527)

Parth-ui-commits7efd374b8748
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py b/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py
index e07cefa6d..8832dcc28 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py
@@ -346,7 +346,11 @@ def _subtree_contains_complex(arm_node: Node, complex_types: frozenset[str]) ->
return True
# A block with multiple named children (statements) is complex.
if cur.type == "block":
- named_children = [c for c in cur.children if c.is_named]
+ named_children = [
+ c
+ for c in cur.children
+ if c.is_named and "comment" not in c.type
+ ]
if len(named_children) > 1:
return True
for child in cur.children:
diff --git a/tests/unit/health/test_complexity_walker.py b/tests/unit/health/test_complexity_walker.py
index f51d39d35..43654d975 100644
--- a/tests/unit/health/test_complexity_walker.py
+++ b/tests/unit/health/test_complexity_walker.py
@@ -1133,3 +1133,37 @@ def test_apex_assertions_and_test_cases():
assert by_name[case].is_test_case, case
for helper in ("makeData", "compute"):
assert not by_name[helper].is_test_case, helper
+
+
+
+def test_java_switch_arm_comment_does_not_increase_ccn():
+ _require_language("java")
+
+ def get_ccn(arm: str) -> int:
+ source = f"""
+ class Example {{
+ String f(int x) {{
+ switch (x) {{
+ case 0 -> {{ return "a"; }}
+ case 1 -> {{ {arm} }}
+ default -> {{ return "z"; }}
+ }}
+ return "";
+ }}
+ }}
+ """
+ results = walk_file_complexity(
+ "/tmp/example.java", "java", source.encode()
+ )
+ result = _find(results, "f")
+ assert result is not None
+ return result.ccn
+
+ plain = get_ccn('return "b";')
+ commented = get_ccn('// explanatory comment\nreturn "b";')
+ multiple_statements = get_ccn(
+ 'String value = "b"; return value;'
+ )
+
+ assert commented == plain
+ assert multiple_statements > plain