1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! `Cyclomatic` implementation for Ruby.
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
use super::*;
impl Cyclomatic for RubyCode {
fn compute<'a>(node: &Node<'a>, code: &'a [u8], stats: &mut Stats) {
use Ruby as R;
match node.kind_id().into() {
// Standard-only: each `when` arm of an ordinary `case … when`.
R::When => {
stats.cyclomatic += 1.;
}
// Standard-only: a `case … in` pattern-match arm, but only when
// it is a real decision — the bare wildcard `in _` (no guard)
// is the default arm and adds nothing, matching Rust's `_`
// `MatchArm` and Python's `case _:` filters (#977).
R::InClause if crate::metrics::npa::ruby_in_clause_counts(node, code) => {
stats.cyclomatic += 1.;
}
// Modified-only: each case container collapses its arms.
R::Case | R::CaseMatch => {
stats.cyclomatic_modified += 1.;
}
// Both standard and modified.
R::If
| R::Unless
| R::Elsif
| R::IfModifier
| R::UnlessModifier
| R::While
| R::Until
| R::For
| R::WhileModifier
| R::UntilModifier
| R::Rescue
| R::RescueModifier
| R::RescueModifier2
| R::RescueModifier3
| R::Conditional
| R::AMPAMP
| R::PIPEPIPE
| R::And
| R::Or
// Safe-navigation `&.` (`AMPDOT`) is short-circuit — it
// skips the method call when the receiver is nil — so each
// occurrence is one decision point, mirroring the
// Kotlin/PHP/JS/C# treatment of `?.` (issues #281, #452).
// The grammar emits the `&.` token once per operator inside
// a `call` node, so matching the token counts each textual
// `&.` exactly once, including in chains (`a&.b&.c` is +2),
// paralleling Kotlin's `QMARKDOT` token approach.
| R::AMPDOT => {
stats.cyclomatic += 1.;
stats.cyclomatic_modified += 1.;
}
_ => {}
}
}
}