Skip to content

Commit 726de3d

Browse files
authored
Make metric traits public (#1046)
* parser: Use metric traits instead of CodeMetricsT trait This removes a superfluous trait aggregator * parser: Remove a useless crate * parser: Reorder crates imports * Remove CodeMetricsT trait * metrics: Visualize metric traits documentation * macros: Add a macro to implement empty metric traits for each language * metrics: Use new macro
1 parent 92f00bc commit 726de3d

15 files changed

+213
-132
lines changed

src/macros.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,56 @@ macro_rules! get_language {
1313
};
1414
}
1515

16+
macro_rules! implement_metric_trait {
17+
(Abc, $($code:ident),+) => (
18+
$(
19+
impl Abc for $code {
20+
fn compute(_node: &Node, _stats: &mut Stats) {}
21+
}
22+
)+
23+
);
24+
(Cognitive, $($code:ident),+) => (
25+
$(
26+
impl Cognitive for $code {
27+
fn compute(_node: &Node, _stats: &mut Stats, _nesting_map: &mut FxHashMap<usize, (usize, usize, usize)>,) {}
28+
}
29+
)+
30+
);
31+
(Halstead, $($code:ident),+) => (
32+
$(
33+
impl Halstead for $code {
34+
fn compute<'a>(_node: &Node<'a>, _code: &'a [u8], _halstead_maps: &mut HalsteadMaps<'a>) {}
35+
}
36+
)+
37+
);
38+
(Loc, $($code:ident),+) => (
39+
$(
40+
impl Loc for $code {
41+
fn compute(_node: &Node, _stats: &mut Stats, _is_func_space: bool, _is_unit: bool) {}
42+
}
43+
)+
44+
);
45+
(Wmc, $($code:ident),+) => (
46+
$(
47+
impl Wmc for $code {
48+
fn compute(_space_kind: SpaceKind, _cyclomatic: &cyclomatic::Stats, _stats: &mut Stats) {}
49+
}
50+
)+
51+
);
52+
([$trait:ident], $($code:ident),+) => (
53+
$(
54+
impl $trait for $code {}
55+
)+
56+
);
57+
($trait:ident, $($code:ident),+) => (
58+
$(
59+
impl $trait for $code {
60+
fn compute(_node: &Node, _stats: &mut Stats) {}
61+
}
62+
)+
63+
)
64+
}
65+
1666
macro_rules! mk_enum {
1767
( $( $camel:ident, $description:expr ),* ) => {
1868
/// The list of supported languages.
@@ -222,7 +272,6 @@ macro_rules! mk_code {
222272
( $( ($camel:ident, $code:ident, $parser:ident, $name:ident, $docname:expr) ),* ) => {
223273
$(
224274
pub struct $code { _guard: (), }
225-
impl _private::CodeMetricsT for $code { }
226275

227276
impl _private::TSLanguage for $code {
228277
type BaseLang = $camel;

src/metrics/abc.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,11 @@ impl Stats {
245245
}
246246
}
247247

248-
#[doc(hidden)]
249248
pub trait Abc
250249
where
251250
Self: Checker,
252251
{
253-
fn compute(_node: &Node, _stats: &mut Stats) {}
252+
fn compute(node: &Node, stats: &mut Stats);
254253
}
255254

256255
// Inspects the content of Java parenthesized expressions
@@ -342,16 +341,19 @@ fn java_count_unary_conditions(list_node: &Node, conditions: &mut f64) {
342341
}
343342
}
344343

345-
impl Abc for PythonCode {}
346-
impl Abc for MozjsCode {}
347-
impl Abc for JavascriptCode {}
348-
impl Abc for TypescriptCode {}
349-
impl Abc for TsxCode {}
350-
impl Abc for RustCode {}
351-
impl Abc for CppCode {}
352-
impl Abc for PreprocCode {}
353-
impl Abc for CcommentCode {}
354-
impl Abc for KotlinCode {}
344+
implement_metric_trait!(
345+
Abc,
346+
PythonCode,
347+
MozjsCode,
348+
JavascriptCode,
349+
TypescriptCode,
350+
TsxCode,
351+
RustCode,
352+
CppCode,
353+
PreprocCode,
354+
CcommentCode,
355+
KotlinCode
356+
);
355357

356358
// Fitzpatrick, Jerry (1997). "Applying the ABC metric to C, C++ and Java". C++ Report.
357359
// Source: https://www.softwarerenovation.com/Articles.aspx

src/metrics/cognitive.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,15 @@ impl Stats {
119119
}
120120
}
121121

122-
#[doc(hidden)]
123122
pub trait Cognitive
124123
where
125124
Self: Checker,
126125
{
127126
fn compute(
128-
_node: &Node,
129-
_stats: &mut Stats,
130-
_nesting_map: &mut FxHashMap<usize, (usize, usize, usize)>,
131-
) {
132-
}
127+
node: &Node,
128+
stats: &mut Stats,
129+
nesting_map: &mut FxHashMap<usize, (usize, usize, usize)>,
130+
);
133131
}
134132

135133
fn compute_booleans<T: std::cmp::PartialEq + std::convert::From<u16>>(
@@ -446,10 +444,7 @@ impl Cognitive for TsxCode {
446444
js_cognitive!(Tsx);
447445
}
448446

449-
impl Cognitive for PreprocCode {}
450-
impl Cognitive for CcommentCode {}
451-
impl Cognitive for JavaCode {}
452-
impl Cognitive for KotlinCode {}
447+
implement_metric_trait!(Cognitive, PreprocCode, CcommentCode, JavaCode, KotlinCode);
453448

454449
#[cfg(test)]
455450
mod tests {

src/metrics/cyclomatic.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,11 @@ impl Stats {
101101
}
102102
}
103103

104-
#[doc(hidden)]
105104
pub trait Cyclomatic
106105
where
107106
Self: Checker,
108107
{
109-
fn compute(_node: &Node, _stats: &mut Stats) {}
108+
fn compute(node: &Node, stats: &mut Stats);
110109
}
111110

112111
impl Cyclomatic for PythonCode {
@@ -205,9 +204,6 @@ impl Cyclomatic for CppCode {
205204
}
206205
}
207206

208-
impl Cyclomatic for PreprocCode {}
209-
impl Cyclomatic for CcommentCode {}
210-
211207
impl Cyclomatic for JavaCode {
212208
fn compute(node: &Node, stats: &mut Stats) {
213209
use Java::*;
@@ -221,7 +217,7 @@ impl Cyclomatic for JavaCode {
221217
}
222218
}
223219

224-
impl Cyclomatic for KotlinCode {}
220+
implement_metric_trait!(Cyclomatic, KotlinCode, PreprocCode, CcommentCode);
225221

226222
#[cfg(test)]
227223
mod tests {

src/metrics/exit.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,11 @@ impl Stats {
106106
}
107107
}
108108

109-
#[doc(hidden)]
110109
pub trait Exit
111110
where
112111
Self: Checker,
113112
{
114-
fn compute(_node: &Node, _stats: &mut Stats) {}
113+
fn compute(node: &Node, stats: &mut Stats);
115114
}
116115

117116
impl Exit for PythonCode {
@@ -180,10 +179,7 @@ impl Exit for JavaCode {
180179
}
181180
}
182181

183-
impl Exit for KotlinCode {}
184-
185-
impl Exit for PreprocCode {}
186-
impl Exit for CcommentCode {}
182+
implement_metric_trait!(Exit, KotlinCode, PreprocCode, CcommentCode);
187183

188184
#[cfg(test)]
189185
mod tests {

src/metrics/halstead.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub enum HalsteadType {
2727
Unknown,
2828
}
2929

30-
#[doc(hidden)]
3130
#[derive(Debug, Default, Clone)]
3231
pub struct HalsteadMaps<'a> {
3332
pub(crate) operators: FxHashMap<u16, u64>,
@@ -247,12 +246,11 @@ impl Stats {
247246
}
248247
}
249248

250-
#[doc(hidden)]
251249
pub trait Halstead
252250
where
253251
Self: Checker,
254252
{
255-
fn compute<'a>(_node: &Node<'a>, _code: &'a [u8], _halstead_maps: &mut HalsteadMaps<'a>) {}
253+
fn compute<'a>(node: &Node<'a>, code: &'a [u8], halstead_maps: &mut HalsteadMaps<'a>);
256254
}
257255

258256
#[inline(always)]
@@ -331,10 +329,7 @@ impl Halstead for JavaCode {
331329
}
332330
}
333331

334-
impl Halstead for KotlinCode {}
335-
336-
impl Halstead for PreprocCode {}
337-
impl Halstead for CcommentCode {}
332+
implement_metric_trait!(Halstead, KotlinCode, PreprocCode, CcommentCode);
338333

339334
#[cfg(test)]
340335
mod tests {

src/metrics/loc.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,11 @@ impl Stats {
499499
}
500500
}
501501

502-
#[doc(hidden)]
503502
pub trait Loc
504503
where
505504
Self: Checker,
506505
{
507-
fn compute(_node: &Node, _stats: &mut Stats, _is_func_space: bool, _is_unit: bool) {}
506+
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool);
508507
}
509508

510509
#[inline(always)]
@@ -815,10 +814,7 @@ impl Loc for JavaCode {
815814
}
816815
}
817816

818-
impl Loc for KotlinCode {}
819-
820-
impl Loc for PreprocCode {}
821-
impl Loc for CcommentCode {}
817+
implement_metric_trait!(Loc, PreprocCode, CcommentCode, KotlinCode);
822818

823819
#[cfg(test)]
824820
mod tests {

src/metrics/mi.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ impl Stats {
8282
}
8383
}
8484

85-
#[doc(hidden)]
8685
pub trait Mi
8786
where
8887
Self: Checker,
@@ -102,17 +101,20 @@ where
102101
}
103102
}
104103

105-
impl Mi for RustCode {}
106-
impl Mi for CppCode {}
107-
impl Mi for PythonCode {}
108-
impl Mi for MozjsCode {}
109-
impl Mi for JavascriptCode {}
110-
impl Mi for TypescriptCode {}
111-
impl Mi for TsxCode {}
112-
impl Mi for PreprocCode {}
113-
impl Mi for CcommentCode {}
114-
impl Mi for JavaCode {}
115-
impl Mi for KotlinCode {}
104+
implement_metric_trait!(
105+
[Mi],
106+
PythonCode,
107+
MozjsCode,
108+
JavascriptCode,
109+
TypescriptCode,
110+
TsxCode,
111+
RustCode,
112+
CppCode,
113+
PreprocCode,
114+
CcommentCode,
115+
JavaCode,
116+
KotlinCode
117+
);
116118

117119
#[cfg(test)]
118120
mod tests {

src/metrics/nargs.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ fn compute_args<T: Checker>(node: &Node, nargs: &mut usize) {
192192
}
193193
}
194194

195-
#[doc(hidden)]
196195
pub trait NArgs
197196
where
198197
Self: Checker,
@@ -229,16 +228,19 @@ impl NArgs for CppCode {
229228
}
230229
}
231230

232-
impl NArgs for MozjsCode {}
233-
impl NArgs for JavascriptCode {}
234-
impl NArgs for TypescriptCode {}
235-
impl NArgs for TsxCode {}
236-
impl NArgs for PreprocCode {}
237-
impl NArgs for CcommentCode {}
238-
impl NArgs for RustCode {}
239-
impl NArgs for PythonCode {}
240-
impl NArgs for JavaCode {}
241-
impl NArgs for KotlinCode {}
231+
implement_metric_trait!(
232+
[NArgs],
233+
PythonCode,
234+
MozjsCode,
235+
JavascriptCode,
236+
TypescriptCode,
237+
TsxCode,
238+
RustCode,
239+
PreprocCode,
240+
CcommentCode,
241+
JavaCode,
242+
KotlinCode
243+
);
242244

243245
#[cfg(test)]
244246
mod tests {

src/metrics/nom.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ impl Stats {
185185
}
186186
}
187187

188-
#[doc(hidden)]
189188
pub trait Nom
190189
where
191190
Self: Checker,
@@ -201,17 +200,20 @@ where
201200
}
202201
}
203202

204-
impl Nom for PythonCode {}
205-
impl Nom for MozjsCode {}
206-
impl Nom for JavascriptCode {}
207-
impl Nom for TypescriptCode {}
208-
impl Nom for TsxCode {}
209-
impl Nom for RustCode {}
210-
impl Nom for CppCode {}
211-
impl Nom for PreprocCode {}
212-
impl Nom for CcommentCode {}
213-
impl Nom for JavaCode {}
214-
impl Nom for KotlinCode {}
203+
implement_metric_trait!(
204+
[Nom],
205+
PythonCode,
206+
MozjsCode,
207+
JavascriptCode,
208+
TypescriptCode,
209+
TsxCode,
210+
CppCode,
211+
RustCode,
212+
PreprocCode,
213+
CcommentCode,
214+
JavaCode,
215+
KotlinCode
216+
);
215217

216218
#[cfg(test)]
217219
mod tests {

0 commit comments

Comments
 (0)