Repository navigation
Score if/else chains, nested functions and commented lines consistently - #8
Merged
Merged
Conversation
Three metric bugs, all visible in the UI's stat row and findings: - Tree-sitter hangs `else` and `else if` under the `if` they continue, so every arm counted as a deeper level. A five-arm `else if` chain scored cognitive 36 and nesting 8 in JavaScript and C++ (tripping the deep-nesting and cognitive-load findings) against 5 and 1 for the same Python. Java had no ELSE node at all, so `else` was never counted or explained. Python's `else` took a nesting penalty and an `elif` body was not nested. All four languages now give SonarSource's numbers: `else`/`else if` add one with no nesting penalty, and their bodies are nested one level. - A function's cyclomatic complexity and max nesting included functions nested inside it, despite the comment saying they are scored separately: the walk skipped the nested node but not its body. - A line with code and a trailing comment was counted only as a comment line, so `x = 1 # one` had zero lines of code, which also fed the maintainability index. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UigC7feaY8NkAy9oay3GDW
ethanzhoucool
marked this pull request as ready for review
November 3, 2026 17:18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The README says the metrics run on the IR "so a Java method and a Python function are scored by exactly the same rules". For if/else chains that wasn't true. There were also two smaller counting bugs. All three affect the UI's stat row (cyclomatic / cognitive / max nesting / maintainability) and the findings built from those numbers.
1.
else/else ifchains. Tree-sitter nestselseandelse ifunder theifthey continue, but the metrics treated each arm as one level deeper. Python puts the arms beside theif. Java has noelse_clause, so it had no ELSE node at all:elsewas never counted, and never explained in the walkthrough. Measured onmain, as (cyclomatic, cognitive, max nesting):forcontainingif/elseif/elifwith a nestedifelse ifchainelse { ...; if (c) {...} }On
main, a plain five-arm JS chain trips both thedeep-nestingfinding (threshold 3) and thehigh-cognitive-loadfinding (threshold 15).2. Nested functions leaked into the outer function.
cyclomatic_ofsaid "nested functions are scored separately", but thecontinueonly skipped the nested node itself andwalk()still went through its body. An outer function whose only content is a nested helper with oneifscored 2 instead of 1.max_nesting_ofshared one maximum across the boundary, so a nested helper's deep loops raised adeep-nestingfinding on the outer function too.3. Lines with a trailing comment counted as comment-only.
x = 1 # onewas counted as a comment line and not as code, so"x = 1 # one\ny = 2 # two\n"had 0 lines of code. That number feeds the summary's SLOC and the maintainability index. Blank lines inside a block comment were also subtracted twice.Changes
adapters/treesitter.py_meta_branchnow setsis_elifon theifthat is an else branch, matching what the Python adapter means by it. That's anifinside anelse_clause(JS, C++) or in Java'salternativefield. Before, it was set on the outerif, and only in Java. Nothing besides the metrics readsis_elif. It also sets a newcontinues_parentflag, which tells the metrics the chain is nested rather than laid out side by side.else_clausethat only wraps anifbecomes transparent, soelse ifis one step of the chain in every language.ifalternativeis wrapped in a synthesized ELSE node, spanning theelsekeyword. Java's} else {lines now get the same walkthrough line as JS, C++ and Python ("Runs only when the condition above was false.").analysis/metrics.pyelse/else ifadd +1 with no nesting penalty, and their bodies sit one level inside the chain.else/else ifis at the same depth as theifit continues.cyclomatic_ofandmax_nesting_ofskip nested callables. The file-level totals still count everything, as before.code_linessubtracts only lines that are entirely comments;comment_linesis unchanged.Testing
pyteston Python 3.13, 3.12 and 3.11: 168 passed, 3 skipped.ruffis clean, andmypyreports the same 26 errors as before.main:test_if_else_chains_score_the_same_in_every_language: the 4 shapes above in all 4 languages, each asserting the full triple.test_nested_functions_are_scored_on_their_own: it also checks that the file total still counts every decision.test_lines_with_trailing_comments_are_still_code: Python, JS with a block comment containing a blank line, and C++ with inline/* */.test_java_else_gets_explained.ifs score 6, three sequential score 3".Notes
else:with only anifinside it fromelif, because both produce the same AST. Both are scored aselif.TRYstays inNESTING_KINDS, so atryblock adds a level. That rule is the same in every language, so I left it alone.Generated by Claude Code