-
Notifications
You must be signed in to change notification settings - Fork 49
/
loc.rs
3460 lines (3280 loc) · 110 KB
/
loc.rs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::collections::HashSet;
use crate::checker::Checker;
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use std::fmt;
use crate::macros::implement_metric_trait;
use crate::*;
/// The `SLoc` metric suite.
#[derive(Debug, Clone)]
pub struct Sloc {
start: usize,
end: usize,
unit: bool,
sloc_min: usize,
sloc_max: usize,
}
impl Default for Sloc {
fn default() -> Self {
Self {
start: 0,
end: 0,
unit: false,
sloc_min: usize::MAX,
sloc_max: 0,
}
}
}
impl Sloc {
#[inline(always)]
pub fn sloc(&self) -> f64 {
// This metric counts the number of lines in a file
// The if construct is needed to count the line of code that represents
// the function signature in a function space
let sloc = if self.unit {
self.end - self.start
} else {
(self.end - self.start) + 1
};
sloc as f64
}
/// The `Sloc` metric minimum value.
#[inline(always)]
pub fn sloc_min(&self) -> f64 {
self.sloc_min as f64
}
/// The `Sloc` metric maximum value.
#[inline(always)]
pub fn sloc_max(&self) -> f64 {
self.sloc_max as f64
}
#[inline(always)]
pub fn merge(&mut self, other: &Sloc) {
self.sloc_min = self.sloc_min.min(other.sloc() as usize);
self.sloc_max = self.sloc_max.max(other.sloc() as usize);
}
#[inline(always)]
pub(crate) fn compute_minmax(&mut self) {
if self.sloc_min == usize::MAX {
self.sloc_min = self.sloc_min.min(self.sloc() as usize);
self.sloc_max = self.sloc_max.max(self.sloc() as usize);
}
}
}
/// The `PLoc` metric suite.
#[derive(Debug, Clone)]
pub struct Ploc {
lines: HashSet<usize>,
ploc_min: usize,
ploc_max: usize,
}
impl Default for Ploc {
fn default() -> Self {
Self {
lines: HashSet::default(),
ploc_min: usize::MAX,
ploc_max: 0,
}
}
}
impl Ploc {
#[inline(always)]
pub fn ploc(&self) -> f64 {
// This metric counts the number of instruction lines in a code
// https://en.wikipedia.org/wiki/Source_lines_of_code
self.lines.len() as f64
}
/// The `Ploc` metric minimum value.
#[inline(always)]
pub fn ploc_min(&self) -> f64 {
self.ploc_min as f64
}
/// The `Ploc` metric maximum value.
#[inline(always)]
pub fn ploc_max(&self) -> f64 {
self.ploc_max as f64
}
#[inline(always)]
pub fn merge(&mut self, other: &Ploc) {
// Merge ploc lines
for l in other.lines.iter() {
self.lines.insert(*l);
}
self.ploc_min = self.ploc_min.min(other.ploc() as usize);
self.ploc_max = self.ploc_max.max(other.ploc() as usize);
}
#[inline(always)]
pub(crate) fn compute_minmax(&mut self) {
if self.ploc_min == usize::MAX {
self.ploc_min = self.ploc_min.min(self.ploc() as usize);
self.ploc_max = self.ploc_max.max(self.ploc() as usize);
}
}
}
/// The `CLoc` metric suite.
#[derive(Debug, Clone)]
pub struct Cloc {
only_comment_lines: usize,
code_comment_lines: usize,
comment_line_end: Option<usize>,
cloc_min: usize,
cloc_max: usize,
}
impl Default for Cloc {
fn default() -> Self {
Self {
only_comment_lines: 0,
code_comment_lines: 0,
comment_line_end: Option::default(),
cloc_min: usize::MAX,
cloc_max: 0,
}
}
}
impl Cloc {
#[inline(always)]
pub fn cloc(&self) -> f64 {
// Comments are counted regardless of their placement
// https://en.wikipedia.org/wiki/Source_lines_of_code
(self.only_comment_lines + self.code_comment_lines) as f64
}
/// The `Ploc` metric minimum value.
#[inline(always)]
pub fn cloc_min(&self) -> f64 {
self.cloc_min as f64
}
/// The `Ploc` metric maximum value.
#[inline(always)]
pub fn cloc_max(&self) -> f64 {
self.cloc_max as f64
}
#[inline(always)]
pub fn merge(&mut self, other: &Cloc) {
// Merge cloc lines
self.only_comment_lines += other.only_comment_lines;
self.code_comment_lines += other.code_comment_lines;
self.cloc_min = self.cloc_min.min(other.cloc() as usize);
self.cloc_max = self.cloc_max.max(other.cloc() as usize);
}
#[inline(always)]
pub(crate) fn compute_minmax(&mut self) {
if self.cloc_min == usize::MAX {
self.cloc_min = self.cloc_min.min(self.cloc() as usize);
self.cloc_max = self.cloc_max.max(self.cloc() as usize);
}
}
}
/// The `LLoc` metric suite.
#[derive(Debug, Clone)]
pub struct Lloc {
logical_lines: usize,
lloc_min: usize,
lloc_max: usize,
}
impl Default for Lloc {
fn default() -> Self {
Self {
logical_lines: 0,
lloc_min: usize::MAX,
lloc_max: 0,
}
}
}
impl Lloc {
#[inline(always)]
pub fn lloc(&self) -> f64 {
// This metric counts the number of statements in a code
// https://en.wikipedia.org/wiki/Source_lines_of_code
self.logical_lines as f64
}
/// The `Lloc` metric minimum value.
#[inline(always)]
pub fn lloc_min(&self) -> f64 {
self.lloc_min as f64
}
/// The `Lloc` metric maximum value.
#[inline(always)]
pub fn lloc_max(&self) -> f64 {
self.lloc_max as f64
}
#[inline(always)]
pub fn merge(&mut self, other: &Lloc) {
// Merge lloc lines
self.logical_lines += other.logical_lines;
self.lloc_min = self.lloc_min.min(other.lloc() as usize);
self.lloc_max = self.lloc_max.max(other.lloc() as usize);
}
#[inline(always)]
pub(crate) fn compute_minmax(&mut self) {
if self.lloc_min == usize::MAX {
self.lloc_min = self.lloc_min.min(self.lloc() as usize);
self.lloc_max = self.lloc_max.max(self.lloc() as usize);
}
}
}
/// The `Loc` metric suite.
#[derive(Debug, Clone)]
pub struct Stats {
sloc: Sloc,
ploc: Ploc,
cloc: Cloc,
lloc: Lloc,
space_count: usize,
blank_min: usize,
blank_max: usize,
}
impl Default for Stats {
fn default() -> Self {
Self {
sloc: Sloc::default(),
ploc: Ploc::default(),
cloc: Cloc::default(),
lloc: Lloc::default(),
space_count: 1,
blank_min: usize::MAX,
blank_max: 0,
}
}
}
impl Serialize for Stats {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut st = serializer.serialize_struct("loc", 20)?;
st.serialize_field("sloc", &self.sloc())?;
st.serialize_field("ploc", &self.ploc())?;
st.serialize_field("lloc", &self.lloc())?;
st.serialize_field("cloc", &self.cloc())?;
st.serialize_field("blank", &self.blank())?;
st.serialize_field("sloc_average", &self.sloc_average())?;
st.serialize_field("ploc_average", &self.ploc_average())?;
st.serialize_field("lloc_average", &self.lloc_average())?;
st.serialize_field("cloc_average", &self.cloc_average())?;
st.serialize_field("blank_average", &self.blank_average())?;
st.serialize_field("sloc_min", &self.sloc_min())?;
st.serialize_field("sloc_max", &self.sloc_max())?;
st.serialize_field("cloc_min", &self.cloc_min())?;
st.serialize_field("cloc_max", &self.cloc_max())?;
st.serialize_field("ploc_min", &self.ploc_min())?;
st.serialize_field("ploc_max", &self.ploc_max())?;
st.serialize_field("lloc_min", &self.lloc_min())?;
st.serialize_field("lloc_max", &self.lloc_max())?;
st.serialize_field("blank_min", &self.blank_min())?;
st.serialize_field("blank_max", &self.blank_max())?;
st.end()
}
}
impl fmt::Display for Stats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"sloc: {}, ploc: {}, lloc: {}, cloc: {}, blank: {}, sloc_average: {}, ploc_average: {}, lloc_average: {}, cloc_average: {}, blank_average: {}, sloc_min: {}, sloc_max: {}, cloc_min: {}, cloc_max: {}, ploc_min: {}, ploc_max: {}, lloc_min: {}, lloc_max: {}, blank_min: {}, blank_max: {}",
self.sloc(),
self.ploc(),
self.lloc(),
self.cloc(),
self.blank(),
self.sloc_average(),
self.ploc_average(),
self.lloc_average(),
self.cloc_average(),
self.blank_average(),
self.sloc_min(),
self.sloc_max(),
self.cloc_min(),
self.cloc_max(),
self.ploc_min(),
self.ploc_max(),
self.lloc_min(),
self.lloc_max(),
self.blank_min(),
self.blank_max(),
)
}
}
impl Stats {
/// Merges a second `Loc` metric suite into the first one
pub fn merge(&mut self, other: &Stats) {
self.sloc.merge(&other.sloc);
self.ploc.merge(&other.ploc);
self.cloc.merge(&other.cloc);
self.lloc.merge(&other.lloc);
// Count spaces
self.space_count += other.space_count;
// min and max
self.blank_min = self.blank_min.min(other.blank() as usize);
self.blank_max = self.blank_max.max(other.blank() as usize);
}
/// The `Sloc` metric.
///
/// Counts the number of lines in a scope
#[inline(always)]
pub fn sloc(&self) -> f64 {
self.sloc.sloc()
}
/// The `Ploc` metric.
///
/// Counts the number of instruction lines in a scope
#[inline(always)]
pub fn ploc(&self) -> f64 {
self.ploc.ploc()
}
/// The `Lloc` metric.
///
/// Counts the number of statements in a scope
#[inline(always)]
pub fn lloc(&self) -> f64 {
self.lloc.lloc()
}
/// The `Cloc` metric.
///
/// Counts the number of comments in a scope
#[inline(always)]
pub fn cloc(&self) -> f64 {
self.cloc.cloc()
}
/// The `Blank` metric.
///
/// Counts the number of blank lines in a scope
#[inline(always)]
pub fn blank(&self) -> f64 {
self.sloc() - self.ploc() - self.cloc.only_comment_lines as f64
}
/// The `Sloc` metric average value.
///
/// This value is computed dividing the `Sloc` value for the number of spaces
#[inline(always)]
pub fn sloc_average(&self) -> f64 {
self.sloc() / self.space_count as f64
}
/// The `Ploc` metric average value.
///
/// This value is computed dividing the `Ploc` value for the number of spaces
#[inline(always)]
pub fn ploc_average(&self) -> f64 {
self.ploc() / self.space_count as f64
}
/// The `Lloc` metric average value.
///
/// This value is computed dividing the `Lloc` value for the number of spaces
#[inline(always)]
pub fn lloc_average(&self) -> f64 {
self.lloc() / self.space_count as f64
}
/// The `Cloc` metric average value.
///
/// This value is computed dividing the `Cloc` value for the number of spaces
#[inline(always)]
pub fn cloc_average(&self) -> f64 {
self.cloc() / self.space_count as f64
}
/// The `Blank` metric average value.
///
/// This value is computed dividing the `Blank` value for the number of spaces
#[inline(always)]
pub fn blank_average(&self) -> f64 {
self.blank() / self.space_count as f64
}
/// The `Sloc` metric minimum value.
#[inline(always)]
pub fn sloc_min(&self) -> f64 {
self.sloc.sloc_min()
}
/// The `Sloc` metric maximum value.
#[inline(always)]
pub fn sloc_max(&self) -> f64 {
self.sloc.sloc_max()
}
/// The `Cloc` metric minimum value.
#[inline(always)]
pub fn cloc_min(&self) -> f64 {
self.cloc.cloc_min()
}
/// The `Cloc` metric maximum value.
#[inline(always)]
pub fn cloc_max(&self) -> f64 {
self.cloc.cloc_max()
}
/// The `Ploc` metric minimum value.
#[inline(always)]
pub fn ploc_min(&self) -> f64 {
self.ploc.ploc_min()
}
/// The `Ploc` metric maximum value.
#[inline(always)]
pub fn ploc_max(&self) -> f64 {
self.ploc.ploc_max()
}
/// The `Lloc` metric minimum value.
#[inline(always)]
pub fn lloc_min(&self) -> f64 {
self.lloc.lloc_min()
}
/// The `Lloc` metric maximum value.
#[inline(always)]
pub fn lloc_max(&self) -> f64 {
self.lloc.lloc_max()
}
/// The `Blank` metric minimum value.
#[inline(always)]
pub fn blank_min(&self) -> f64 {
self.blank_min as f64
}
/// The `Blank` metric maximum value.
#[inline(always)]
pub fn blank_max(&self) -> f64 {
self.blank_max as f64
}
#[inline(always)]
pub(crate) fn compute_minmax(&mut self) {
self.sloc.compute_minmax();
self.ploc.compute_minmax();
self.cloc.compute_minmax();
self.lloc.compute_minmax();
if self.blank_min == usize::MAX {
self.blank_min = self.blank_min.min(self.blank() as usize);
self.blank_max = self.blank_max.max(self.blank() as usize);
}
}
}
pub trait Loc
where
Self: Checker,
{
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool);
}
#[inline(always)]
fn init(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) -> (usize, usize) {
let start = node.start_row();
let end = node.end_row();
if is_func_space {
stats.sloc.start = start;
stats.sloc.end = end;
stats.sloc.unit = is_unit;
}
(start, end)
}
#[inline(always)]
// Discriminates among the comments that are *after* a code line and
// the ones that are on an independent line.
// This difference is necessary in order to avoid having
// a wrong count for the blank metric.
fn add_cloc_lines(stats: &mut Stats, start: usize, end: usize) {
let comment_diff = end - start;
let is_comment_after_code_line = stats.ploc.lines.contains(&start);
if is_comment_after_code_line && comment_diff == 0 {
// A comment is *entirely* next to a code line
stats.cloc.code_comment_lines += 1;
} else if is_comment_after_code_line && comment_diff > 0 {
// A block comment that starts next to a code line and ends on
// independent lines.
stats.cloc.code_comment_lines += 1;
stats.cloc.only_comment_lines += comment_diff;
} else {
// A comment on an independent line AND
// a block comment on independent lines OR
// a comment *before* a code line
stats.cloc.only_comment_lines += (end - start) + 1;
// Save line end of a comment to check whether
// a comment *before* a code line is considered
stats.cloc.comment_line_end = Some(end);
}
}
#[inline(always)]
// Detects the comments that are on a code line but *before* the code part.
// This difference is necessary in order to avoid having
// a wrong count for the blank metric.
fn check_comment_ends_on_code_line(stats: &mut Stats, start_code_line: usize) {
if let Some(end) = stats.cloc.comment_line_end {
if end == start_code_line && !stats.ploc.lines.contains(&start_code_line) {
// Comment entirely *before* a code line
stats.cloc.only_comment_lines -= 1;
stats.cloc.code_comment_lines += 1;
}
}
}
impl Loc for PythonCode {
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Python::*;
let (start, end) = init(node, stats, is_func_space, is_unit);
match node.kind_id().into() {
StringStart | StringEnd | StringContent | Block | Module => {}
Comment => {
add_cloc_lines(stats, start, end);
}
String => {
let parent = node.parent().unwrap();
if let ExpressionStatement = parent.kind_id().into() {
add_cloc_lines(stats, start, end);
} else if parent.start_row() != start {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
}
}
Statement
| SimpleStatements
| ImportStatement
| FutureImportStatement
| ImportFromStatement
| PrintStatement
| AssertStatement
| ReturnStatement
| DeleteStatement
| RaiseStatement
| PassStatement
| BreakStatement
| ContinueStatement
| IfStatement
| ForStatement
| WhileStatement
| TryStatement
| WithStatement
| GlobalStatement
| NonlocalStatement
| ExecStatement
| ExpressionStatement => {
stats.lloc.logical_lines += 1;
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
}
}
}
}
impl Loc for MozjsCode {
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Mozjs::*;
let (start, end) = init(node, stats, is_func_space, is_unit);
match node.kind_id().into() {
String | DQUOTE | Program => {}
Comment => {
add_cloc_lines(stats, start, end);
}
ExpressionStatement | ExportStatement | ImportStatement | StatementBlock
| IfStatement | SwitchStatement | ForStatement | ForInStatement | WhileStatement
| DoStatement | TryStatement | WithStatement | BreakStatement | ContinueStatement
| DebuggerStatement | ReturnStatement | ThrowStatement | EmptyStatement
| StatementIdentifier => {
stats.lloc.logical_lines += 1;
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
}
}
}
}
impl Loc for JavascriptCode {
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Javascript::*;
let (start, end) = init(node, stats, is_func_space, is_unit);
match node.kind_id().into() {
String | DQUOTE | Program => {}
Comment => {
add_cloc_lines(stats, start, end);
}
ExpressionStatement | ExportStatement | ImportStatement | StatementBlock
| IfStatement | SwitchStatement | ForStatement | ForInStatement | WhileStatement
| DoStatement | TryStatement | WithStatement | BreakStatement | ContinueStatement
| DebuggerStatement | ReturnStatement | ThrowStatement | EmptyStatement
| StatementIdentifier => {
stats.lloc.logical_lines += 1;
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
}
}
}
}
impl Loc for TypescriptCode {
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Typescript::*;
let (start, end) = init(node, stats, is_func_space, is_unit);
match node.kind_id().into() {
String | DQUOTE | Program => {}
Comment => {
add_cloc_lines(stats, start, end);
}
ExpressionStatement | ExportStatement | ImportStatement | StatementBlock
| IfStatement | SwitchStatement | ForStatement | ForInStatement | WhileStatement
| DoStatement | TryStatement | WithStatement | BreakStatement | ContinueStatement
| DebuggerStatement | ReturnStatement | ThrowStatement | EmptyStatement
| StatementIdentifier => {
stats.lloc.logical_lines += 1;
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
}
}
}
}
impl Loc for TsxCode {
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Tsx::*;
let (start, end) = init(node, stats, is_func_space, is_unit);
match node.kind_id().into() {
String | DQUOTE | Program => {}
Comment => {
add_cloc_lines(stats, start, end);
}
ExpressionStatement | ExportStatement | ImportStatement | StatementBlock
| IfStatement | SwitchStatement | ForStatement | ForInStatement | WhileStatement
| DoStatement | TryStatement | WithStatement | BreakStatement | ContinueStatement
| DebuggerStatement | ReturnStatement | ThrowStatement | EmptyStatement
| StatementIdentifier => {
stats.lloc.logical_lines += 1;
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
}
}
}
}
impl Loc for RustCode {
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Rust::*;
let (start, end) = init(node, stats, is_func_space, is_unit);
match node.kind_id().into() {
StringLiteral
| RawStringLiteral
| Block
| SourceFile
| SLASHSLASH
| SLASHSTAR
| STARSLASH
| OuterDocCommentMarker2
| DocComment => {}
LineComment | BlockComment => {
add_cloc_lines(stats, start, end);
}
Statement
| EmptyStatement
| ExpressionStatement
| LetDeclaration
| AssignmentExpression
| CompoundAssignmentExpr => {
stats.lloc.logical_lines += 1;
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
}
}
}
}
impl Loc for CppCode {
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Cpp::*;
let (start, end) = init(node, stats, is_func_space, is_unit);
match node.kind_id().into() {
RawStringLiteral | StringLiteral | DeclarationList | FieldDeclarationList
| TranslationUnit => {}
Comment => {
add_cloc_lines(stats, start, end);
}
WhileStatement | SwitchStatement | CaseStatement | IfStatement | ForStatement
| ReturnStatement | BreakStatement | ContinueStatement | GotoStatement
| ThrowStatement | TryStatement | ExpressionStatement | LabeledStatement
| StatementIdentifier => {
stats.lloc.logical_lines += 1;
}
Declaration => {
if node.count_specific_ancestors::<CppParser>(
|node| {
matches!(
node.kind_id().into(),
WhileStatement | ForStatement | IfStatement
)
},
|node| node.kind_id() == CompoundStatement,
) == 0
{
stats.lloc.logical_lines += 1;
}
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
}
}
}
}
impl Loc for JavaCode {
fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Java::*;
let (start, end) = init(node, stats, is_func_space, is_unit);
let kind_id: Java = node.kind_id().into();
// LLOC in Java is counted for statements only
// https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html
match kind_id {
Program => {}
LineComment | BlockComment => {
add_cloc_lines(stats, start, end);
}
AssertStatement | BreakStatement | ContinueStatement | DoStatement
| EnhancedForStatement | ExpressionStatement | ForStatement | IfStatement
| ReturnStatement | SwitchExpression | ThrowStatement | TryStatement
| WhileStatement => {
stats.lloc.logical_lines += 1;
}
LocalVariableDeclaration => {
if node.count_specific_ancestors::<JavaParser>(
|node| node.kind_id() == ForStatement,
|node| node.kind_id() == Block,
) == 0
{
// The initializer, condition, and increment in a for loop are expressions.
// Don't count the variable declaration if in a ForStatement.
// https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
stats.lloc.logical_lines += 1;
}
}
_ => {
check_comment_ends_on_code_line(stats, start);
stats.ploc.lines.insert(start);
}
}
}
}
implement_metric_trait!(Loc, PreprocCode, CcommentCode, KotlinCode);
#[cfg(test)]
mod tests {
use crate::tools::check_metrics;
use super::*;
#[test]
fn python_sloc() {
check_metrics::<PythonParser>(
"
a = 42
",
"foo.py",
|metric| {
// Spaces: 1
insta::assert_json_snapshot!(
metric.loc,
@r###"
{
"sloc": 1.0,
"ploc": 1.0,
"lloc": 1.0,
"cloc": 0.0,
"blank": 0.0,
"sloc_average": 1.0,
"ploc_average": 1.0,
"lloc_average": 1.0,
"cloc_average": 0.0,
"blank_average": 0.0,
"sloc_min": 1.0,
"sloc_max": 1.0,
"cloc_min": 0.0,
"cloc_max": 0.0,
"ploc_min": 1.0,
"ploc_max": 1.0,
"lloc_min": 1.0,
"lloc_max": 1.0,
"blank_min": 0.0,
"blank_max": 0.0
}"###
);
},
);
}
#[test]
fn python_blank() {
check_metrics::<PythonParser>(
"
a = 42
b = 43
",
"foo.py",
|metric| {
// Spaces: 1
insta::assert_json_snapshot!(
metric.loc,
@r###"
{
"sloc": 3.0,
"ploc": 2.0,
"lloc": 2.0,
"cloc": 0.0,
"blank": 1.0,
"sloc_average": 3.0,
"ploc_average": 2.0,
"lloc_average": 2.0,
"cloc_average": 0.0,
"blank_average": 1.0,
"sloc_min": 3.0,
"sloc_max": 3.0,
"cloc_min": 0.0,
"cloc_max": 0.0,
"ploc_min": 2.0,
"ploc_max": 2.0,
"lloc_min": 2.0,
"lloc_max": 2.0,
"blank_min": 1.0,
"blank_max": 1.0
}"###
);
},
);
}
#[test]
fn rust_blank() {
check_metrics::<RustParser>(
"
let a = 42;
let b = 43;
",
"foo.rs",
|metric| {
// Spaces: 1
insta::assert_json_snapshot!(
metric.loc,
@r###"
{
"sloc": 3.0,
"ploc": 2.0,
"lloc": 2.0,
"cloc": 0.0,
"blank": 1.0,
"sloc_average": 3.0,
"ploc_average": 2.0,
"lloc_average": 2.0,
"cloc_average": 0.0,
"blank_average": 1.0,
"sloc_min": 3.0,
"sloc_max": 3.0,
"cloc_min": 0.0,
"cloc_max": 0.0,
"ploc_min": 2.0,
"ploc_max": 2.0,
"lloc_min": 2.0,
"lloc_max": 2.0,
"blank_min": 1.0,
"blank_max": 1.0
}"###
);
},
);
check_metrics::<RustParser>("fn func() { /* comment */ }", "foo.rs", |metric| {
// Spaces: 2
insta::assert_json_snapshot!(
metric.loc,
@r###"
{
"sloc": 1.0,
"ploc": 1.0,
"lloc": 0.0,
"cloc": 1.0,
"blank": 0.0,
"sloc_average": 0.5,
"ploc_average": 0.5,
"lloc_average": 0.0,
"cloc_average": 0.5,
"blank_average": 0.0,
"sloc_min": 1.0,
"sloc_max": 1.0,
"cloc_min": 1.0,
"cloc_max": 1.0,
"ploc_min": 1.0,
"ploc_max": 1.0,
"lloc_min": 0.0,
"lloc_max": 0.0,
"blank_min": 0.0,
"blank_max": 0.0
}"###
);
});
}
#[test]
fn c_blank() {