Skip to content

fix(abc): a comment in a condition slot zeroes the condition #1455

Description

@dekobon

Summary

C#'s if / while / do condition slots read a fixed child index, so
a leading comment inside the parentheses shifts the read and the condition
scores zero.

$ # if (IsEven(x))      -> abc.conditions 1
$ # if (/*c*/ IsEven(x)) -> abc.conditions 0

This is the same class #1181
fixed for the ternary, and the same trap the when-guard slot added in
#1422
deliberately avoids: tree-sitter extras are named nodes, so a
first-named-child read hands the slot a comment. That arm iterates every
named child instead, and csharp_guard_keeps_its_condition_across_a_comment
pins it. The sibling slots were left to their own change rather than
widened into #1422.

Where

src/metrics/abc/csharp.rs, csharp_walk_for_conditions — the
IfStatement | WhileStatement and DoStatement arms, which use
csharp_inspect_child(node, idx, …) with a positional index. The guard arm
immediately below them shows the shape that survives extras.

Fix shape

Prefer child_by_field_name where the grammar exposes a field for the slot
(.claude/rules/grammar-dispatch.md §3). Where it does not, iterate named
children and pick by role, as the WhenClause | CatchFilterClause arm does.

Check the sibling languages in the same pass — java.rs, groovy.rs and
cpp.rs all carry positional inspect_child reads, and Groovy's
if / while (child(2)) and do-while (child(4)) are called out in
src/metrics/abc/groovy.rs as the last positional condition lookups after
#1181/#1276 migrated the rest.

Tests

A commented and an uncommented spelling of the same condition, asserted
against each other rather than against a literal, so the pair cannot
drift apart silently. Both block (/* */) and line (//) comments, since
they are separate extras.

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions