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): flatten ternary dispatch chains across seven languages (#4709)

crown7c5d6f5b5d11
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 afe41cd60..103b9e328 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/languages.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/languages.py
@@ -577,6 +577,7 @@ _JAVA = LanguageNodeMap(
function_kinds=frozenset({"method_declaration", "constructor_declaration"}),
lambda_kinds=frozenset({"lambda_expression"}),
branch_kinds=frozenset({"if_statement", "ternary_expression"}),
+ flat_ternary_kinds=frozenset({"ternary_expression"}),
loop_kinds=frozenset(
{
"for_statement",
@@ -895,6 +896,7 @@ _DART = LanguageNodeMap(
# ``if_element`` is the collection-literal ``if`` (``[if (x) y]``);
# ``conditional_expression`` is the ternary.
branch_kinds=frozenset({"if_statement", "conditional_expression", "if_element"}),
+ flat_ternary_kinds=frozenset({"conditional_expression"}),
loop_kinds=frozenset({"for_statement", "while_statement", "do_statement", "for_element"}),
try_kinds=frozenset({"try_statement"}),
# A bare ``on FormatException {}`` arm without ``catch`` has no
@@ -934,6 +936,7 @@ _CPP = LanguageNodeMap(
function_kinds=frozenset({"function_definition"}),
lambda_kinds=frozenset({"lambda_expression"}),
branch_kinds=frozenset({"if_statement", "conditional_expression"}),
+ flat_ternary_kinds=frozenset({"conditional_expression"}),
loop_kinds=frozenset({"for_statement", "while_statement", "do_statement", "for_range_loop"}),
foreach_kinds=frozenset({"for_range_loop"}),
try_kinds=frozenset({"try_statement"}),
@@ -1001,6 +1004,7 @@ _C = LanguageNodeMap(
function_kinds=frozenset({"function_definition"}),
lambda_kinds=frozenset(),
branch_kinds=frozenset({"if_statement", "conditional_expression"}),
+ flat_ternary_kinds=frozenset({"conditional_expression"}),
loop_kinds=frozenset({"for_statement", "while_statement", "do_statement"}),
try_kinds=frozenset(),
catch_kinds=frozenset(),
@@ -1056,6 +1060,7 @@ _CSHARP = LanguageNodeMap(
),
lambda_kinds=frozenset({"lambda_expression"}),
branch_kinds=frozenset({"if_statement", "conditional_expression"}),
+ flat_ternary_kinds=frozenset({"conditional_expression"}),
loop_kinds=frozenset({"for_statement", "while_statement", "foreach_statement", "do_statement"}),
foreach_kinds=frozenset({"foreach_statement"}),
try_kinds=frozenset({"try_statement"}),
@@ -1453,6 +1458,7 @@ _OBJC = LanguageNodeMap(
# its complexity now that ``declaration`` is not a function kind.
lambda_kinds=frozenset({"block_literal"}),
branch_kinds=frozenset({"if_statement", "conditional_expression"}),
+ flat_ternary_kinds=frozenset({"conditional_expression"}),
# Fast enumeration parses as ``for_statement``; there is no
# ``for_in_statement`` node in this grammar.
loop_kinds=frozenset({"for_statement", "while_statement", "do_statement"}),
@@ -1484,6 +1490,7 @@ _PHP = LanguageNodeMap(
function_kinds=frozenset({"function_definition", "method_declaration"}),
lambda_kinds=frozenset({"anonymous_function", "arrow_function"}),
branch_kinds=frozenset({"if_statement", "else_if_clause", "conditional_expression"}),
+ flat_ternary_kinds=frozenset({"conditional_expression"}),
loop_kinds=frozenset({"for_statement", "foreach_statement", "while_statement", "do_statement"}),
foreach_kinds=frozenset({"foreach_statement"}),
try_kinds=frozenset({"try_statement"}),
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.py
index f6bd9f4b2..085728313 100644
--- a/packages/core/src/repowise/core/analysis/health/engine.py
+++ b/packages/core/src/repowise/core/analysis/health/engine.py
@@ -106,16 +106,14 @@ log = structlog.get_logger(__name__)
# Not a licence to move a calibrated scoring weight — those are frozen
# independently of this stamp.
#
-# v43: the code-origin classifier moved for several path shapes: a nested
-# ``tools/`` folder is production (only a repo-root or engineering ``tools/``
-# stays tooling), Python ``_vendor/`` folders and Salesforce static-resource
-# libraries read as vendored, and prefixed migration folders under ``db/`` read
-# as migrations. Stored origins decide health and dead-code scope, so those
-# files re-score.
+# v44: ternary dispatch chains in Java, C, C++, C#, Dart, Objective-C and PHP
+# keep each decision but do not add nesting for their else-arms, matching
+# Python and JS/TS. Re-score stored metrics and invalidate cached walks so
+# old false nested-complexity findings do not survive the new language maps.
#
# Earlier bumps and their reasons: ``git log -S"HEALTH_ANALYZER_VERSION =" -p``
# on this file.
-HEALTH_ANALYZER_VERSION = 43
+HEALTH_ANALYZER_VERSION = 44
def _log_duplication_diagnostics(report: DuplicationReport) -> None:
diff --git a/tests/unit/health/test_cross_language_ternary_chains.py b/tests/unit/health/test_cross_language_ternary_chains.py
new file mode 100644
index 000000000..7467b4828
--- /dev/null
+++ b/tests/unit/health/test_cross_language_ternary_chains.py
@@ -0,0 +1,111 @@
+"""A ternary's else-arm dispatch stays flat; decisions inside an arm still nest."""
+
+from __future__ import annotations
+
+import re
+
+import pytest
+
+from repowise.core.analysis.health.biomarkers.base import FileContext
+from repowise.core.analysis.health.biomarkers.nested_complexity import BIOMARKER
+from repowise.core.analysis.health.complexity import walk_file
+from repowise.core.ingestion.parser import _get_language
+
+_LANGUAGES = [
+ ("java", "F.java", "class F { int f(boolean a, boolean b, boolean c) {", "} }"),
+ ("c", "f.c", "int f(int a, int b, int c) {", "}"),
+ ("cpp", "f.cpp", "int f(bool a, bool b, bool c) {", "}"),
+ ("csharp", "F.cs", "class F { int f(bool a, bool b, bool c) {", "} }"),
+ ("dart", "f.dart", "int f(bool a, bool b, bool c) {", "}"),
+ ("objectivec", "f.m", "int f(int a, int b, int c) {", "}"),
+ ("php", "f.php", "<?php function f($a, $b, $c) {", "}"),
+]
+
+
[email protected]("language,path,prefix,suffix", _LANGUAGES, ids=[v[0] for v in _LANGUAGES])
+ "body,expected",
+ [
+ pytest.param("return a ? 1 : (b ? 2 : (c ? 3 : 4));", (4, 1, 3), id="dispatch"),
+ pytest.param("return a ? (b ? 1 : 2) : 3;", (3, 2, 3), id="nested-consequence"),
+ pytest.param("return (a ? b : c) ? 1 : 2;", (3, 2, 3), id="nested-condition"),
+ pytest.param(
+ "if (a) { return b ? 1 : (c ? 2 : 3); } return 0;",
+ (4, 2, 4),
+ id="dispatch-inside-if",
+ ),
+ pytest.param(
+ "if (a ? b : (c ? a : b)) { return 1; } return 0;",
+ (4, 2, 4),
+ id="dispatch-in-if-condition",
+ ),
+ ],
+)
+def test_ternary_dispatch_and_real_nesting(language, path, prefix, suffix, body, expected):
+ if _get_language(language) is None:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ if language == "php":
+ body = re.sub(r"\b([abc])\b", r"$\1", body)
+ [fn] = walk_file(path, language, f"{prefix}\n{body}\n{suffix}".encode()).functions
+ assert (fn.ccn, fn.max_nesting, fn.cognitive) == expected
+
+
[email protected]("language,path,prefix,suffix", _LANGUAGES, ids=[v[0] for v in _LANGUAGES])
+def test_dispatch_does_not_report_a_deeply_nested_function(language, path, prefix, suffix):
+ if _get_language(language) is None:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ body = "return a ? 1 : (b ? 2 : (c ? 3 : (a ? 4 : (b ? 5 : 6))));"
+ if language == "php":
+ body = re.sub(r"\b([abc])\b", r"$\1", body)
+ [fn] = walk_file(path, language, f"{prefix}\n{body}\n{suffix}".encode()).functions
+ assert fn.ccn == 6
+ context = FileContext(
+ file_path=path, language=language, nloc=3, has_test_file=False, module=None,
+ all_functions=(fn,),
+ )
+ assert BIOMARKER.detect(context) == []
+
+
+ "language,path,prefix,suffix", [v for v in _LANGUAGES if v[0] != "php"],
+ ids=[v[0] for v in _LANGUAGES if v[0] != "php"],
+)
+def test_unparenthesized_dispatch(language, path, prefix, suffix):
+ # PHP 8 requires the explicit grouping covered above; these grammars are
+ # right-associative without it.
+ if _get_language(language) is None:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ body = "return a ? 1 : b ? 2 : c ? 3 : 4;"
+ [fn] = walk_file(path, language, f"{prefix}\n{body}\n{suffix}".encode()).functions
+ assert (fn.ccn, fn.max_nesting, fn.cognitive) == (4, 1, 3)
+
+
+_LAMBDAS = {
+ "java": "IntSupplier pick = () -> %s; return pick.getAsInt();",
+ "cpp": "auto pick = [&]() { return %s; }; return pick();",
+ "csharp": "System.Func<int> pick = () => %s; return pick();",
+ "dart": "final pick = () { return %s; }; return pick();",
+ "objectivec": "int (^pick)(void) = ^{ return %s; }; return pick();",
+ "php": "$pick = function () use ($a, $b, $c) { return %s; }; return $pick();",
+}
+
+
+ "language,path,prefix,suffix", [v for v in _LANGUAGES if v[0] in _LAMBDAS],
+ ids=[v[0] for v in _LANGUAGES if v[0] in _LAMBDAS],
+)
+ "expression,expected",
+ [
+ pytest.param("a ? 1 : (b ? 2 : 3)", (3, 1, 2), id="lambda-dispatch"),
+ pytest.param("a ? (b ? 1 : 2) : 3", (3, 2, 3), id="lambda-nested-consequence"),
+ ],
+)
+def test_lambda_keeps_dispatch_distinct_from_nesting(language, path, prefix, suffix, expression, expected):
+ if _get_language(language) is None:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ if language == "php":
+ expression = re.sub(r"\b([abc])\b", r"$\1", expression)
+ body = _LAMBDAS[language] % expression
+ [fn] = walk_file(path, language, f"{prefix}\n{body}\n{suffix}".encode()).functions
+ assert (fn.ccn, fn.max_nesting, fn.cognitive) == expected
diff --git a/tests/unit/health/test_model_version_coupling.py b/tests/unit/health/test_model_version_coupling.py
index 5648e7522..f890ec93a 100644
--- a/tests/unit/health/test_model_version_coupling.py
+++ b/tests/unit/health/test_model_version_coupling.py
@@ -22,7 +22,7 @@ from repowise.core.analysis.health.refactoring.identity import (
# The three stamps as they stand together. Moving either identity model means
# moving the analyzer with it, and updating this tuple in the same commit.
-_STAMPS = (43, 3, 4)
+_STAMPS = (44, 3, 4)
def test_the_identity_models_and_the_analyzer_stamp_are_pinned_together() -> None: