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): flatten Pascal else-if chains instead of nesting each arm (#1679)
Laentir Valetovae220dab8129
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py b/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.pyindex 346f4f9f7..40ab4cdcd 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py@@ -43,11 +43,19 @@ _BODY_FIELD_NAMES = (# The ``if``-shaped node types that can appear as an ``else if`` / ``elif``# chain continuation. Ternaries / ``if_element`` collection-``if`` are-# deliberately excluded — only statement-level if chains flatten.+# deliberately excluded — only statement-level if chains flatten. Pascal's+# ``if`` (bare then, no else) and ``ifElse`` (then/else) are two distinct+# node types for the same construct -- either can be a chain's last arm+# (bare ``if`` when it has no further ``else``, ``ifElse`` when it does)._ELSE_IF_NODE_KINDS = frozenset(- {"if_statement", "if_expression", "elif_clause", "elsif", "if_let_expression"}+ {"if_statement", "if_expression", "elif_clause", "elsif", "if_let_expression", "if", "ifElse"})+# Token spelling(s) an else-if continuation's previous sibling carries when+# the grammar has no dedicated wrapper node for it (Java / Go / C# / Dart /+# Kotlin spell it "else"; Pascal spells it "kElse").+_ELSE_TOKEN_KINDS = frozenset({"else", "kElse"})+# Branch nodes that are a decision point (count toward CCN) but do NOT open a# nesting level: a comprehension filter (``[x for x in xs if a]``) and a match# ``case ... if guard:`` both parse as ``if_clause`` and sit inline, not as a@@ -81,8 +89,9 @@ def _is_elif_continuation(node: Node) -> bool:- Python: a dedicated ``elif_clause`` node (always a continuation).- Ruby: a dedicated ``elsif`` node (always a continuation).- TypeScript / Rust / C++: the else-if is wrapped in an ``else_clause``.- - Java / Go / C# / Dart / Kotlin: the else-if node is the sibling that- immediately follows the parent ``if``'s ``else`` token.+ - Java / Go / C# / Dart / Kotlin / Pascal: the else-if node is the+ sibling that immediately follows the parent ``if``'s else token+ (spelled ``else`` in those grammars, ``kElse`` in Pascal's)."""if node.type not in _ELSE_IF_NODE_KINDS:return False@@ -94,7 +103,7 @@ def _is_elif_continuation(node: Node) -> bool:if parent.type == "else_clause":return Trueprev = node.prev_sibling- return prev is not None and prev.type == "else"+ return prev is not None and prev.type in _ELSE_TOKEN_KINDSdef _count_boolean_ops_in_condition(node: Node, lmap: LanguageNodeMap) -> int:diff --git a/tests/unit/health/test_complexity_walker.py b/tests/unit/health/test_complexity_walker.pyindex 66250b07f..7207e97ed 100644--- a/tests/unit/health/test_complexity_walker.py+++ b/tests/unit/health/test_complexity_walker.py@@ -131,6 +131,30 @@ def test_pascal_complex_method_ccn():assert many.param_count == 5+def test_pascal_else_if_chain_is_flat_not_nested():+ # Regression: found on a real ~40-arm `else if` VK-code dispatch chain+ # (StringToVK in a real Delphi codebase) — every arm nests the grammar's+ # `ifElse`/`if` one level inside the previous arm's `else` field, so an+ # unflattened walk reported nesting 33 / cognitive 583 for a flat lookup+ # table. Each arm still charges CCN (it's a real decision point) but+ # must not open a fresh nesting level or inflate cognitive complexity.+ src = (+ b"unit U;\ninterface\nimplementation\n"+ b"function F(S: string): Integer;\nbegin\n"+ b" if S = 'a' then Result := 1\n"+ b" else if S = 'b' then Result := 2\n"+ b" else if S = 'c' then Result := 3\n"+ b" else if S = 'd' then Result := 4\n"+ b" else if S = 'e' then Result := 5\n"+ b" else Result := 0;\n"+ b"end;\nend.\n"+ )+ fn = _find(walk_file("u.pas", "pascal", src).functions, "F")+ assert fn is not None+ assert fn.ccn == 6, f"expected CCN 6 (5 arms + base), got {fn.ccn}"+ assert fn.max_nesting == 1, f"expected flat chain (nesting 1), got {fn.max_nesting}"++def test_pascal_no_class_metrics():# Pascal method bodies live in a top-level ``defProc`` outside the# ``declClass`` node (see languages.py's ``_PASCAL`` comment) -- class