Skip to content

Commit 7e8a531

Browse files
committed
Improve nargs metric
Counts closures and compute the average both for functions and closures respectively over the number of functions and closures
1 parent 1800948 commit 7e8a531

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ mod tests {
633633
"end_line": 4,
634634
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
635635
"cognitive": {"sum": 0.0, "average": 0.0},
636-
"nargs": {"sum": 0.0, "average": 0.0},
636+
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
637637
"nexits": {"sum": 0.0, "average": 0.0},
638638
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
639639
"difficulty": 1.0,
@@ -660,7 +660,7 @@ mod tests {
660660
"end_line": 4,
661661
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
662662
"cognitive": {"sum": 0.0, "average": 0.0},
663-
"nargs": {"sum": 0.0, "average": 0.0},
663+
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
664664
"nexits": {"sum": 0.0, "average": 0.0},
665665
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
666666
"difficulty": 1.0,
@@ -713,7 +713,7 @@ mod tests {
713713
"end_line": 2,
714714
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
715715
"cognitive": {"sum": 0.0, "average": 0.0},
716-
"nargs": {"sum": 0.0, "average": 0.0},
716+
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
717717
"nexits": {"sum": 0.0, "average": 0.0},
718718
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
719719
"difficulty": 1.0,
@@ -762,7 +762,7 @@ mod tests {
762762
"end_line": 2,
763763
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
764764
"cognitive": {"sum": 0.0, "average": 0.0},
765-
"nargs": {"sum": 0.0, "average": 0.0},
765+
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
766766
"nexits": {"sum": 0.0, "average": 0.0},
767767
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
768768
"difficulty": 1.0,
@@ -789,7 +789,7 @@ mod tests {
789789
"end_line": 2,
790790
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
791791
"cognitive": {"sum": 0.0, "average": 0.0},
792-
"nargs": {"sum": 0.0, "average": 0.0},
792+
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
793793
"nexits": {"sum": 0.0, "average": 0.0},
794794
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
795795
"difficulty": 1.0,

src/checker.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub trait Checker {
1919
fn is_string(node: &Node) -> bool;
2020
fn is_call(node: &Node) -> bool;
2121
fn is_func(node: &Node) -> bool;
22+
fn is_closure(node: &Node) -> bool;
2223
fn is_func_space(node: &Node) -> bool;
2324
fn is_non_arg(node: &Node) -> bool;
2425

@@ -32,6 +33,7 @@ impl Checker for PreprocCode {
3233
mk_checker!(is_string, StringLiteral, RawStringLiteral);
3334
mk_checker!(is_call,);
3435
mk_checker!(is_func,);
36+
mk_checker!(is_closure,);
3537
mk_checker!(is_func_space,);
3638
mk_checker!(is_non_arg,);
3739
}
@@ -41,6 +43,7 @@ impl Checker for CcommentCode {
4143
mk_checker!(is_string, StringLiteral, RawStringLiteral);
4244
mk_checker!(is_call,);
4345
mk_checker!(is_func,);
46+
mk_checker!(is_closure,);
4447
mk_checker!(is_func_space,);
4548
mk_checker!(is_non_arg,);
4649

@@ -68,6 +71,7 @@ impl Checker for CppCode {
6871
FunctionDefinition2,
6972
FunctionDefinition3
7073
);
74+
mk_checker!(is_closure, LambdaExpression);
7175
mk_checker!(
7276
is_func_space,
7377
TranslationUnit,
@@ -106,6 +110,7 @@ impl Checker for PythonCode {
106110
mk_checker!(is_string, String, ConcatenatedString);
107111
mk_checker!(is_call, Call);
108112
mk_checker!(is_func, FunctionDefinition);
113+
mk_checker!(is_closure, Lambda);
109114
mk_checker!(is_func_space, Module, FunctionDefinition, ClassDefinition);
110115
mk_checker!(is_non_arg, LPAREN, COMMA, RPAREN);
111116
}
@@ -115,6 +120,7 @@ impl Checker for JavaCode {
115120
mk_checker!(is_string, StringLiteral);
116121
mk_checker!(is_call, MethodInvocation);
117122
mk_checker!(is_func, MethodDeclaration);
123+
mk_checker!(is_closure,);
118124
mk_checker!(is_func_space, Program, ClassDeclaration);
119125
mk_checker!(is_non_arg,);
120126
}
@@ -123,13 +129,12 @@ impl Checker for MozjsCode {
123129
mk_checker!(is_comment, Comment);
124130
mk_checker!(is_string, String, TemplateString);
125131
mk_checker!(is_call, CallExpression);
132+
mk_checker!(is_func, FunctionDeclaration, MethodDefinition);
126133
mk_checker!(
127-
is_func,
134+
is_closure,
128135
Function,
129136
GeneratorFunction,
130-
FunctionDeclaration,
131137
GeneratorFunctionDeclaration,
132-
MethodDefinition,
133138
ArrowFunction
134139
);
135140
mk_checker!(
@@ -153,13 +158,12 @@ impl Checker for JavascriptCode {
153158
mk_checker!(is_comment, Comment);
154159
mk_checker!(is_string, String, TemplateString);
155160
mk_checker!(is_call, CallExpression);
161+
mk_checker!(is_func, FunctionDeclaration, MethodDefinition);
156162
mk_checker!(
157-
is_func,
163+
is_closure,
158164
Function,
159165
GeneratorFunction,
160-
FunctionDeclaration,
161166
GeneratorFunctionDeclaration,
162-
MethodDefinition,
163167
ArrowFunction
164168
);
165169
mk_checker!(
@@ -182,13 +186,12 @@ impl Checker for TypescriptCode {
182186
mk_checker!(is_comment, Comment);
183187
mk_checker!(is_string, String, TemplateString);
184188
mk_checker!(is_call, CallExpression);
189+
mk_checker!(is_func, FunctionDeclaration, MethodDefinition);
185190
mk_checker!(
186-
is_func,
191+
is_closure,
187192
Function,
188193
GeneratorFunction,
189-
FunctionDeclaration,
190194
GeneratorFunctionDeclaration,
191-
MethodDefinition,
192195
ArrowFunction
193196
);
194197
mk_checker!(
@@ -211,13 +214,12 @@ impl Checker for TsxCode {
211214
mk_checker!(is_comment, Comment);
212215
mk_checker!(is_string, String, TemplateString);
213216
mk_checker!(is_call, CallExpression);
217+
mk_checker!(is_func, FunctionDeclaration, MethodDefinition);
214218
mk_checker!(
215-
is_func,
219+
is_closure,
216220
Function,
217221
GeneratorFunction,
218-
FunctionDeclaration,
219222
GeneratorFunctionDeclaration,
220-
MethodDefinition,
221223
ArrowFunction
222224
);
223225
mk_checker!(
@@ -254,7 +256,8 @@ impl Checker for RustCode {
254256
mk_else_if!(IfExpression);
255257
mk_checker!(is_string, StringLiteral, RawStringLiteral);
256258
mk_checker!(is_call, CallExpression);
257-
mk_checker!(is_func, FunctionItem, ClosureExpression);
259+
mk_checker!(is_func, FunctionItem);
260+
mk_checker!(is_closure, ClosureExpression);
258261
mk_checker!(
259262
is_func_space,
260263
SourceFile,
@@ -263,5 +266,5 @@ impl Checker for RustCode {
263266
TraitItem,
264267
ClosureExpression
265268
);
266-
mk_checker!(is_non_arg, LPAREN, COMMA, RPAREN, AttributeItem);
269+
mk_checker!(is_non_arg, LPAREN, COMMA, RPAREN, PIPE, AttributeItem);
267270
}

src/output/dump_metrics.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,19 @@ fn dump_nargs(
256256
last: bool,
257257
stdout: &mut StandardStreamLock,
258258
) -> std::io::Result<()> {
259-
let pref = if last { "`- " } else { "|- " };
259+
let (pref_child, pref) = if last { (" ", "`- ") } else { ("| ", "|- ") };
260260

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

264264
color!(stdout, Green, true);
265-
write!(stdout, "nargs: ")?;
265+
writeln!(stdout, "nargs")?;
266266

267-
color!(stdout, White);
268-
writeln!(stdout, "{}", stats.nargs())
267+
let prefix = format!("{}{}", prefix, pref_child);
268+
dump_value("functions", stats.fn_args(), &prefix, false, stdout)?;
269+
dump_value("closures", stats.closure_args(), &prefix, false, stdout)?;
270+
dump_value("total", stats.nargs_total(), &prefix, false, stdout)?;
271+
dump_value("average", stats.nargs_average(), &prefix, true, stdout)
269272
}
270273

271274
fn dump_nexits(

src/spaces.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,19 @@ fn compute_halstead_and_mi<'a, T: ParserTrait>(state: &mut State<'a>) {
181181

182182
#[inline(always)]
183183
fn compute_averages<'a>(state: &mut State<'a>) {
184+
let nom_functions = state.space.metrics.nom.functions() as usize;
185+
let nom_closures = state.space.metrics.nom.closures() as usize;
184186
let nom_total = state.space.metrics.nom.total() as usize;
185187
// Cognitive average
186188
state.space.metrics.cognitive.finalize(nom_total);
187189
// Nexit average
188190
state.space.metrics.nexits.finalize(nom_total);
189191
// Nargs average
190-
state.space.metrics.nargs.finalize(nom_total);
192+
state
193+
.space
194+
.metrics
195+
.nargs
196+
.finalize(nom_functions, nom_closures);
191197
}
192198

193199
fn finalize<'a, T: ParserTrait>(state_stack: &mut Vec<State<'a>>, diff_level: usize) {

0 commit comments

Comments
 (0)