Skip to content

Commit 39ff9f9

Browse files
committed
Add nexits average
1 parent 7f33ab1 commit 39ff9f9

3 files changed

Lines changed: 63 additions & 9 deletions

File tree

‎rust-code-analysis-cli/src/web/server.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ mod tests {
634634
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
635635
"cognitive": 0.0,
636636
"nargs": 0.,
637-
"nexits": 0.,
637+
"nexits": {"sum": 0.0, "average": 0.0},
638638
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
639639
"difficulty": 1.0,
640640
"effort": 4.754_887_502_163_468,
@@ -661,7 +661,7 @@ mod tests {
661661
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
662662
"cognitive": 0.0,
663663
"nargs": 0.,
664-
"nexits": 0.,
664+
"nexits": {"sum": 0.0, "average": 0.0},
665665
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
666666
"difficulty": 1.0,
667667
"effort": 4.754_887_502_163_468,
@@ -714,7 +714,7 @@ mod tests {
714714
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
715715
"cognitive": 0.0,
716716
"nargs": 0.,
717-
"nexits": 0.,
717+
"nexits": {"sum": 0.0, "average": 0.0},
718718
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
719719
"difficulty": 1.0,
720720
"effort": 4.754_887_502_163_468,
@@ -763,7 +763,7 @@ mod tests {
763763
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
764764
"cognitive": 0.0,
765765
"nargs": 0.,
766-
"nexits": 0.,
766+
"nexits": {"sum": 0.0, "average": 0.0},
767767
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
768768
"difficulty": 1.0,
769769
"effort": 4.754_887_502_163_468,
@@ -790,7 +790,7 @@ mod tests {
790790
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
791791
"cognitive": 0.0,
792792
"nargs": 0.,
793-
"nexits": 0.,
793+
"nexits": {"sum": 0.0, "average": 0.0},
794794
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
795795
"difficulty": 1.0,
796796
"effort": 4.754_887_502_163_468,

‎src/metrics/exit.rs‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use serde::ser::Serializer;
1+
use serde::ser::{SerializeStruct, Serializer};
22
use serde::Serialize;
33
use std::fmt;
44

@@ -12,11 +12,15 @@ use crate::*;
1212
#[derive(Debug, Clone)]
1313
pub struct Stats {
1414
exit: usize,
15+
total_space_functions: usize,
1516
}
1617

1718
impl Default for Stats {
1819
fn default() -> Self {
19-
Self { exit: 0 }
20+
Self {
21+
exit: 0,
22+
total_space_functions: 1,
23+
}
2024
}
2125
}
2226

@@ -25,13 +29,16 @@ impl Serialize for Stats {
2529
where
2630
S: Serializer,
2731
{
28-
serializer.serialize_f64(self.exit())
32+
let mut st = serializer.serialize_struct("nexits", 2)?;
33+
st.serialize_field("sum", &self.exit())?;
34+
st.serialize_field("average", &self.exit_average())?;
35+
st.end()
2936
}
3037
}
3138

3239
impl fmt::Display for Stats {
3340
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34-
write!(f, "{}", self.exit)
41+
write!(f, "sum: {}, average: {}", self.exit(), self.exit_average())
3542
}
3643
}
3744

@@ -45,6 +52,18 @@ impl Stats {
4552
pub fn exit(&self) -> f64 {
4653
self.exit as f64
4754
}
55+
56+
/// Returns the `NExit` metric average value
57+
///
58+
/// This value is computed dividing the `NExit` value
59+
/// for the total number of functions/closures in a space.
60+
pub fn exit_average(&self) -> f64 {
61+
self.exit() / self.total_space_functions as f64
62+
}
63+
64+
pub(crate) fn finalize(&mut self, total_space_functions: usize) {
65+
self.total_space_functions = total_space_functions;
66+
}
4867
}
4968

5069
#[doc(hidden)]
@@ -121,3 +140,27 @@ impl Exit for JavaCode {}
121140
impl Exit for GoCode {}
122141
impl Exit for CssCode {}
123142
impl Exit for HtmlCode {}
143+
144+
#[cfg(test)]
145+
mod tests {
146+
use std::path::PathBuf;
147+
148+
use super::*;
149+
150+
#[test]
151+
fn test_nested_functions_exit() {
152+
check_metrics!(
153+
"def f(a, b):
154+
def foo(a):
155+
if a:
156+
return 1
157+
bar = lambda a: lambda b: b or True or True
158+
return bar(foo(a))(a)",
159+
"foo.py",
160+
PythonParser,
161+
nexits,
162+
[(exit, 2, usize)],
163+
[(exit_average, 0.5)]
164+
);
165+
}
166+
}

‎src/spaces.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ fn compute_halstead_and_mi<'a, T: ParserTrait>(state: &mut State<'a>) {
179179
);
180180
}
181181

182+
#[inline(always)]
183+
fn compute_average<'a>(state: &mut State<'a>) {
184+
state
185+
.space
186+
.metrics
187+
.nexits
188+
.finalize(state.space.metrics.nom.total() as usize);
189+
}
190+
182191
fn finalize<'a, T: ParserTrait>(state_stack: &mut Vec<State<'a>>, diff_level: usize) {
183192
if state_stack.is_empty() {
184193
return;
@@ -187,10 +196,12 @@ fn finalize<'a, T: ParserTrait>(state_stack: &mut Vec<State<'a>>, diff_level: us
187196
if state_stack.len() == 1 {
188197
let mut last_state = state_stack.last_mut().unwrap();
189198
compute_halstead_and_mi::<T>(&mut last_state);
199+
compute_average(&mut last_state);
190200
break;
191201
} else {
192202
let mut state = state_stack.pop().unwrap();
193203
compute_halstead_and_mi::<T>(&mut state);
204+
compute_average(&mut state);
194205

195206
let mut last_state = state_stack.last_mut().unwrap();
196207
last_state.halstead_maps.merge(&state.halstead_maps);

0 commit comments

Comments
 (0)