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): stop counting abstract, interface and extern methods as function units (#4772)
Raghav Chamadiya271bb0c2da35
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py b/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.pyindex 8b219f60d..a200adbe4 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py@@ -385,8 +385,13 @@ def is_misread_scope(node: Node, lmap: LanguageNodeMap) -> bool:def is_function_node(node: Node, lmap: LanguageNodeMap) -> bool:- """True for a function definition, False for a scope the grammar misread as one."""- return node.type in lmap.function_kinds and not is_misread_scope(node, lmap)+ """True for a function definition, False for a scope the grammar misread as+ one or a declaration with no body (abstract, interface, extern)."""+ return (+ node.type in lmap.function_kinds+ and not is_misread_scope(node, lmap)+ and not (lmap.bodyless is not None and lmap.bodyless(node))+ )def _is_function_unit(node: Node, lmap: LanguageNodeMap) -> bool:diff --git a/packages/core/src/repowise/core/analysis/health/complexity/languages.py b/packages/core/src/repowise/core/analysis/health/complexity/languages.pyindex d23585ae1..5fac471e3 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/languages.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/languages.py@@ -356,6 +356,10 @@ class LanguageNodeMap:# class bodies, perf naming); passes that only run inside a collected# function's body never meet one.misread_scope: Callable[[Node], object] | None = None+ # * ``bodyless`` -- truthy for a ``function_kinds`` node that declares a+ # signature with no body (abstract, interface, extern). It is no unit+ # and no class method. None means every such node has a body.+ bodyless: Callable[[Node], object] | None = None# -- Local bindings ------------------------------------------------------# * ``local_value`` -- truthy for a ``function_kinds`` node that binds a@@ -548,7 +552,14 @@ _JVM_BOUND_PARAM_MARKERS = frozenset(})+def _no_body_field(node: Node) -> object:+ """Java, C#, PHP and Apex: an abstract, interface or extern member ends in+ ``;`` with no ``body`` field."""+ return node.child_by_field_name("body") is None++_JAVA = LanguageNodeMap(+ bodyless=_no_body_field,ctor_kinds=frozenset({"constructor_declaration"}),fixed_signature_markers=frozenset({"Override"}),bound_param_markers=_JVM_BOUND_PARAM_MARKERS,@@ -640,6 +651,7 @@ _APEX = LanguageNodeMap({"method_declaration", "constructor_declaration", "trigger_declaration", "accessor_list"}),misread_scope=_apex_auto_property,+ bodyless=lambda node: node.type == "method_declaration" and _no_body_field(node),lambda_kinds=frozenset(),branch_kinds=frozenset({"if_statement", "ternary_expression"}),loop_kinds=frozenset(@@ -756,6 +768,8 @@ _RUST = LanguageNodeMap(_KOTLIN = LanguageNodeMap(+ # ``abstract fun``, an interface ``fun`` and ``external fun`` have no ``function_body``.+ bodyless=lambda node: not any(c.type == "function_body" for c in node.children),ctor_kinds=frozenset({"secondary_constructor"}),fixed_signature_markers=frozenset({"override"}),bound_param_markers=_JVM_BOUND_PARAM_MARKERS,@@ -991,6 +1005,7 @@ _C = LanguageNodeMap()_CSHARP = LanguageNodeMap(+ bodyless=_no_body_field,ctor_kinds=frozenset({"constructor_declaration"}),# ``override`` and ``extern`` modifiers; P/Invoke and source-generated# bindings, whose parameters a native ABI or a message template fixes; and@@ -1441,6 +1456,7 @@ _OBJC = LanguageNodeMap(_PHP = LanguageNodeMap(+ bodyless=_no_body_field,function_kinds=frozenset({"function_definition", "method_declaration"}),lambda_kinds=frozenset({"anonymous_function", "arrow_function"}),branch_kinds=frozenset({"if_statement", "else_if_clause", "conditional_expression"}),diff --git a/tests/unit/health/test_complexity_walker.py b/tests/unit/health/test_complexity_walker.pyindex 96c8fd510..6632412de 100644--- a/tests/unit/health/test_complexity_walker.py+++ b/tests/unit/health/test_complexity_walker.py@@ -1243,3 +1243,55 @@ def test_java_switch_arm_comment_does_not_increase_ccn():assert commented == plainassert multiple_statements > plain+++_BODYLESS_SOURCES = {+ "java": (+ "A.java",+ b"abstract class A {\n A() { }\n abstract void f(int x);\n native void n();\n void g() { int y = 1; }\n}\n"+ b"interface I {\n void h();\n default int d() { return 1; }\n}\n",+ {"A", "g", "d"},+ 2,+ ),+ "csharp": (+ "a.cs",+ b"abstract class A {\n public A() { }\n public abstract void F(int x);\n static extern int E(int a);\n"+ b" partial void P();\n public void G() { var y = 1; }\n int M() => 1;\n}\n"+ b"interface I {\n void H();\n int D() { return 1; }\n}\n",+ {"A", "G", "M", "D"},+ 3,+ ),+ "php": (+ "a.php",+ b"<?php\nabstract class A {\n abstract public function f($x);\n public function g() { return 1; }\n}\n"+ b"interface I {\n public function h();\n}\n",+ {"g"},+ 1,+ ),+ "kotlin": (+ "a.kt",+ b"abstract class A {\n abstract fun f(x: Int)\n fun g(): Int { return 1 }\n fun e() = 2\n}\n"+ b"interface I {\n fun h()\n fun d(): Int { return 1 }\n}\n",+ {"g", "e", "d"},+ 2,+ ),+ "apex": (+ "A.cls",+ b"public abstract class A {\n public abstract void f(Integer x);\n public void g() { Integer y = 1; }\n}\n"+ b"public interface I {\n void h();\n}\n",+ {"g"},+ 1,+ ),+}++[email protected]("language", sorted(_BODYLESS_SOURCES))+def test_bodyless_methods_are_not_units(language):+ """Abstract, interface and extern declarations have no code: no function+ entry and no class method. Members with a body stay units."""+ _require_language(language)+ path, source, units, a_methods = _BODYLESS_SOURCES[language]+ fcx = walk_file(path, language, source)+ assert {f.name for f in fcx.functions} == units+ classes = {c.name: c for c in fcx.classes}+ assert classes["A"].method_count == a_methodsdiff --git a/tests/unit/health/test_signature_facts.py b/tests/unit/health/test_signature_facts.pyindex 8edb70166..b890550e2 100644--- a/tests/unit/health/test_signature_facts.py+++ b/tests/unit/health/test_signature_facts.py@@ -46,10 +46,8 @@ def test_java_constructor_override_and_scalar_count():("member", "fixed"),[("public override void M(int a, int b, int c, int d, int e) {}", True),- ('[DllImport("x")] static extern int M(int a, int b, int c, int d, int e);', True),("void IBar.M(int a, int b, int c, int d, int e) {}", True),("[DataRow(1, 2, 3, 4, 5)] public void M(int a, int b, int c, int d, int e) {}", True),- ('[LoggerMessage(1, "{a}")] static partial void M(int a, int b, int c, int d, int e);', True),("public void M(int a, int b, int c, int d, int e) {}", True),("protected void M(int a, int b, int c, int d, int e) {}", True),('[Obsolete("override")] void M(int a, int b, int c, int d, int e) {}', False),@@ -63,6 +61,18 @@ def test_csharp_fixed_signatures(member: str, fixed: bool):assert fns["M"].signature_fixed is fixed+def test_csharp_bodyless_bindings_are_no_unit():+ # P/Invoke and source-generated partials declare a signature another+ # declaration fills in, so there is nothing here to score.+ fns = _fns(+ "A.cs",+ "csharp",+ 'public class A { [DllImport("x")] static extern int M(int a, int b, int c, int d, int e);'+ ' [LoggerMessage(1, "{a}")] static partial void N(int a, int b, int c, int d, int e); }',+ )+ assert fns == {}++def test_csharp_public_member_of_internal_type_is_not_api():fns = _fns("A.cs",