Skip to content

Commit

Permalink
Improve nargs metric
Browse files Browse the repository at this point in the history
Counts closures and compute the average both for functions and closures
respectively over the number of functions and closures
  • Loading branch information
Luni-4 committed Sep 16, 2021
1 parent 4e31661 commit 31993b8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
10 changes: 5 additions & 5 deletions rust-code-analysis-web/src/web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ mod tests {
"end_line": 4,
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
Expand All @@ -660,7 +660,7 @@ mod tests {
"end_line": 4,
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
Expand Down Expand Up @@ -713,7 +713,7 @@ mod tests {
"end_line": 2,
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
Expand Down Expand Up @@ -762,7 +762,7 @@ mod tests {
"end_line": 2,
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
Expand All @@ -789,7 +789,7 @@ mod tests {
"end_line": 2,
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
Expand Down
2 changes: 1 addition & 1 deletion src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,5 +351,5 @@ impl Checker for RustCode {
TraitItem,
ClosureExpression
);
mk_checker!(is_non_arg, LPAREN, COMMA, RPAREN, AttributeItem);
mk_checker!(is_non_arg, LPAREN, COMMA, RPAREN, PIPE, AttributeItem);
}
20 changes: 10 additions & 10 deletions src/metrics/nargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ mod tests {
#[test]
fn javascript_single_closure() {
check_metrics!(
"var bar = function (a, b) {return a + b};",
"function (a, b) {return a + b};",
"foo.js",
JavascriptParser,
nargs,
Expand Down Expand Up @@ -713,26 +713,26 @@ mod tests {
fn javascript_nested_functions() {
check_metrics!(
"function f(a, b) {
function foo(a) {
function foo(a, c) {
return a;
}
var bar = function (a, b) {return a + b};
var bar1 = function (a) {return a};
function (a) {return a};
return bar(foo(a), a);
}",
"foo.js",
JavascriptParser,
nargs,
[
(fn_args, 3, usize),
(closure_args, 3, usize),
(nargs_total, 6, usize)
(fn_args, 6, usize),
(closure_args, 1, usize),
(nargs_total, 7, usize)
],
[
(fn_args_average, 1.5),
(closure_args_average, 1.5),
(nargs_average, 1.5)
] // 2 functions + 2 lambdas = 4
(fn_args_average, 2.),
(closure_args_average, 1.),
(nargs_average, 1.75)
] // 3 functions + 1 lambdas = 4
);
}
}
11 changes: 7 additions & 4 deletions src/output/dump_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,19 @@ fn dump_nargs(
last: bool,
stdout: &mut StandardStreamLock,
) -> std::io::Result<()> {
let pref = if last { "`- " } else { "|- " };
let (pref_child, pref) = if last { (" ", "`- ") } else { ("| ", "|- ") };

color!(stdout, Blue);
write!(stdout, "{}{}", prefix, pref)?;

color!(stdout, Green, true);
write!(stdout, "nargs: ")?;
writeln!(stdout, "nargs")?;

color!(stdout, White);
writeln!(stdout, "{}", stats.nargs())
let prefix = format!("{}{}", prefix, pref_child);
dump_value("functions", stats.fn_args(), &prefix, false, stdout)?;
dump_value("closures", stats.closure_args(), &prefix, false, stdout)?;
dump_value("total", stats.nargs_total(), &prefix, false, stdout)?;
dump_value("average", stats.nargs_average(), &prefix, true, stdout)
}

fn dump_nexits(
Expand Down
8 changes: 7 additions & 1 deletion src/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,19 @@ fn compute_halstead_and_mi<T: ParserTrait>(state: &mut State) {

#[inline(always)]
fn compute_averages(state: &mut State) {
let nom_functions = state.space.metrics.nom.functions() as usize;
let nom_closures = state.space.metrics.nom.closures() as usize;
let nom_total = state.space.metrics.nom.total() as usize;
// Cognitive average
state.space.metrics.cognitive.finalize(nom_total);
// Nexit average
state.space.metrics.nexits.finalize(nom_total);
// Nargs average
state.space.metrics.nargs.finalize(nom_total);
state
.space
.metrics
.nargs
.finalize(nom_functions, nom_closures);
}

fn finalize<T: ParserTrait>(state_stack: &mut Vec<State>, diff_level: usize) {
Expand Down

0 comments on commit 31993b8

Please sign in to comment.