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(dataflow): skip comment nodes when picking the else arm (#3349)
SakarPaisen4c9aa84df2a3
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/cfg.py b/packages/core/src/repowise/core/analysis/health/dataflow/cfg.pyindex 3a5b747e6..35689af24 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/cfg.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/cfg.py@@ -399,7 +399,14 @@ class _CFGBuilder:self._edge(cur, join)returnif alt.type == "else_clause": # Python / TS wrap the else arm in a clause- inner = next((c for c in alt.named_children), None)+ # Comment nodes are named siblings here too, and an ``else:`` whose only+ # child is a comment has no statements at all. Picking a comment would+ # make it ``alt``, so the kind is never an ``if_kinds`` one and it reaches+ # ``_process_seq`` as a statement, giving the block the comment's own start+ # line. Skip them the same way ``_body_stmts`` does.+ inner = next(+ (c for c in alt.named_children if "comment" not in c.type), None+ )if inner is None:self._edge(cur, join)returndiff --git a/tests/unit/health/test_dataflow_cfg.py b/tests/unit/health/test_dataflow_cfg.pyindex 3548b6ec5..d6e2ebfef 100644--- a/tests/unit/health/test_dataflow_cfg.py+++ b/tests/unit/health/test_dataflow_cfg.py@@ -107,7 +107,38 @@ def test_if_else_join():assert len(joins) == 1join = joins[0]assert len(join.predecessors) == 2- assert set(join.predecessors) == {s for s in cfg.block(join.id).predecessors}+ # The join flows to the return -> exit.+ assert cfg.exit_id in cfg.reachable_ids()+++def test_else_arm_statement_lines_skip_a_comment_child():+ # An ``else:`` whose first child is a comment: the comment is a named child of+ # the ``else_clause``, so picking the first named child made it the arm and the+ # block was reported at the comment's own line rather than the statement's.+ cfg = _cfg(+ """+ def f(x):+ if x > 0:+ y = 1+ else:+ # the other case+ y = 2+ return y+ """+ )+ branches = _by_kind(cfg, "branch")+ assert len(branches) == 1+ assert len(branches[0].successors) == 2+ assert len(_by_kind(cfg, "join")) == 1++ # Line 1 is ``def f``, the comment is 6, ``y = 2`` is 7. The else arm's block must+ # start at the statement; before the fix it started at the comment (line 0, since+ # tree-sitter does not count it) and no block claimed line 7 at all.+ lines = {b.start_line for b in cfg.blocks if b.statements}+ assert 7 in lines, f"the else statement's line is not claimed by any block: {lines}"+ assert 6 not in lines, f"a block starts at the else comment: {lines}"+ join = _by_kind(cfg, "join")[0]+ assert len(join.predecessors) == 2# The join flows to the return -> exit.assert cfg.exit_id in cfg.reachable_ids()diff --git a/tests/unit/health/test_refactoring_extract_method.py b/tests/unit/health/test_refactoring_extract_method.pyindex 3aba66373..32053d441 100644--- a/tests/unit/health/test_refactoring_extract_method.py+++ b/tests/unit/health/test_refactoring_extract_method.py@@ -92,6 +92,44 @@ def test_finds_clean_extraction_with_inferred_signature():assert best.slice_nloc >= 5+# The same tail as _PROCESS, with a comment opening the else arm. The CFG fix keeps+# the arm's block on the statement's line, so the slice still sees it.+_PROCESS_ELSE_COMMENT = """+def process(records, threshold):+ results = []+ errors = 0+ for r in records:+ if r is None:+ errors += 1+ continue+ results.append(r)+ total = 0+ count = 0+ for v in results:+ if v > threshold:+ total += v+ count += 1+ else:+ # count the misses too+ total -= v+ average = total / count if count else 0+ return average, errors+"""+++def test_a_comment_opening_the_else_arm_still_yields_the_same_extraction():+ # The comment must not change which span is offered or the parameters it takes.+ lmap = get_language_map("python")+ extractions = find_extractions(_first(_PROCESS_ELSE_COMMENT), lmap)+ assert extractions, "expected at least one extraction"+ best = extractions[0]+ assert "average" in best.returns+ assert "results" in best.params and "threshold" in best.params+ assert len(best.returns) <= 1+ assert best.ccn_removed >= 1+ assert best.slice_nloc >= 5++def test_no_extraction_when_every_span_has_a_jump():# A guard-clause cascade: every span contains a return, so nothing is a# single-exit slice -> no candidate.