Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This project is a Rust crate (library) to analyse source code. This software is
control flow of a program.
- SLOC: it counts the number of lines in a source file.
- LLOC: it counts the number of logical lines (instructions) contained in a source file.
- CLOC: it counts the number of comments in a source file.
- HALSTEAD: it is a suite that provides a series of information, such as the effort required to maintain the analyzed code, the size in bits to store the program, the difficulty to understand the code, an estimate of the number of bugs present in the codebase, and an estimate of the time needed to implement the software.
- MI: it is a suite that allows to evaluate the maintainability of a software.
- NOM: it counts the number of functions and closures in a file/trait/class.
Expand Down
97 changes: 72 additions & 25 deletions src/loc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Stats {
end: usize,
unit: bool,
lines: FxHashSet<usize>,
comment_lines: FxHashSet<usize>,
comment_lines: usize,
}

impl Serialize for Stats {
Expand Down Expand Up @@ -46,10 +46,7 @@ impl Stats {
for l in other.lines.iter() {
self.lines.insert(*l);
}

for c in other.comment_lines.iter() {
self.comment_lines.insert(*c);
}
self.comment_lines += other.comment_lines;
}

#[inline(always)]
Expand All @@ -73,7 +70,7 @@ impl Stats {
pub fn cloc(&self) -> f64 {
// Comments are counted regardless of their placement
// https://en.wikipedia.org/wiki/Source_lines_of_code
self.comment_lines.len() as f64
self.comment_lines as f64
}
}

Expand All @@ -92,29 +89,34 @@ where
}

#[inline(always)]
fn init(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) -> usize {
fn init(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) -> (usize, usize) {
let start = node.start_position().row;
let end = node.end_position().row;

if is_func_space {
let end = node.end_position().row;

stats.start = start;
stats.end = end;
stats.unit = is_unit;
}
start
(start, end)
}

impl Loc for PythonCode {
fn compute(node: &Node, _code: &[u8], stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Python::*;

let start = init(node, stats, is_func_space, is_unit);
let (start, end) = init(node, stats, is_func_space, is_unit);

match node.kind_id().into() {
String | DQUOTE | DQUOTE2 | ExpressionStatement | Block | Module => {}
DQUOTE | DQUOTE2 | ExpressionStatement | Block | Module => {}
Comment => {
stats.comment_lines.insert(start);
stats.comment_lines += (end - start) + 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you probably need to handle the case of multiline comments:
https://twitter.com/gvanrossum/status/112670605505077248

Copy link
Collaborator Author

@Luni-4 Luni-4 Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a FIXME in the Python test because I don't know how to discriminate a string used as comment from a simple string

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An idea could be to look for string where the parent is an ExpressionStatement. I'd say that in the case the string is the statement and so this the only thing we've so a comment. Wdyt ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good idea! I'm going to implement that, thanks @calixteman! :)

}
String => {
let parent = node.parent().unwrap();
if let ExpressionStatement = parent.kind_id().into() {
stats.comment_lines += (end - start) + 1;
}
}
_ => {
stats.lines.insert(start);
Expand All @@ -127,12 +129,12 @@ impl Loc for MozjsCode {
fn compute(node: &Node, _code: &[u8], stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Mozjs::*;

let start = init(node, stats, is_func_space, is_unit);
let (start, end) = init(node, stats, is_func_space, is_unit);

match node.kind_id().into() {
String | DQUOTE | ExpressionStatement | StatementBlock | Program => {}
Comment => {
stats.comment_lines.insert(start);
stats.comment_lines += (end - start) + 1;
}
_ => {
stats.lines.insert(start);
Expand All @@ -145,12 +147,12 @@ impl Loc for JavascriptCode {
fn compute(node: &Node, _code: &[u8], stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Javascript::*;

let start = init(node, stats, is_func_space, is_unit);
let (start, end) = init(node, stats, is_func_space, is_unit);

match node.kind_id().into() {
String | DQUOTE | ExpressionStatement | StatementBlock | Program => {}
Comment => {
stats.comment_lines.insert(start);
stats.comment_lines += (end - start) + 1;
}
_ => {
stats.lines.insert(start);
Expand All @@ -163,12 +165,12 @@ impl Loc for TypescriptCode {
fn compute(node: &Node, _code: &[u8], stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Typescript::*;

let start = init(node, stats, is_func_space, is_unit);
let (start, end) = init(node, stats, is_func_space, is_unit);

match node.kind_id().into() {
String | DQUOTE | ExpressionStatement | StatementBlock | Program => {}
Comment => {
stats.comment_lines.insert(start);
stats.comment_lines += (end - start) + 1;
}
_ => {
stats.lines.insert(start);
Expand All @@ -181,12 +183,12 @@ impl Loc for TsxCode {
fn compute(node: &Node, _code: &[u8], stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Tsx::*;

let start = init(node, stats, is_func_space, is_unit);
let (start, end) = init(node, stats, is_func_space, is_unit);

match node.kind_id().into() {
String | DQUOTE | ExpressionStatement | StatementBlock | Program => {}
Comment => {
stats.comment_lines.insert(start);
stats.comment_lines += (end - start) + 1;
}
_ => {
stats.lines.insert(start);
Expand All @@ -199,12 +201,12 @@ impl Loc for RustCode {
fn compute(node: &Node, _code: &[u8], stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Rust::*;

let start = init(node, stats, is_func_space, is_unit);
let (start, end) = init(node, stats, is_func_space, is_unit);

match node.kind_id().into() {
StringLiteral | RawStringLiteral | ExpressionStatement | Block | SourceFile => {}
LineComment | BlockComment => {
stats.comment_lines.insert(start);
stats.comment_lines += (end - start) + 1;
}
_ => {
stats.lines.insert(start);
Expand All @@ -217,13 +219,13 @@ impl Loc for CppCode {
fn compute(node: &Node, _code: &[u8], stats: &mut Stats, is_func_space: bool, is_unit: bool) {
use Cpp::*;

let start = init(node, stats, is_func_space, is_unit);
let (start, end) = init(node, stats, is_func_space, is_unit);

match node.kind_id().into() {
RawStringLiteral | StringLiteral | ExpressionStatement | CompoundStatement
| LabeledStatement | DeclarationList | FieldDeclarationList | TranslationUnit => {}
Comment => {
stats.comment_lines.insert(start);
stats.comment_lines += (end - start) + 1;
}
_ => {
stats.lines.insert(start);
Expand All @@ -239,3 +241,48 @@ impl Loc for JavaCode {}
impl Loc for GoCode {}
impl Loc for CssCode {}
impl Loc for HtmlCode {}

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use super::*;

#[test]
fn test_cloc_python() {
check_metrics!(
"\"\"\"Block comment\nBlock comment\n\"\"\"\n
# Line Comment\na = 42 # Line Comment\n",
"foo.py",
PythonParser,
loc,
[(cloc, 5, usize)]
);
}

#[test]
fn test_cloc_rust() {
check_metrics!(
"/*Block comment\nBlock Comment*/\n//Line Comment\n/*Block Comment*/ let a = 42; // Line Comment\n",
"foo.rs",
RustParser,
loc,
[
(cloc, 5, usize),
]
);
}

#[test]
fn test_cloc_c() {
check_metrics!(
"/*Block comment\nBlock Comment*/\n//Line Comment\n/*Block Comment*/ int a = 42; // Line Comment\n",
"foo.c",
CppParser,
loc,
[
(cloc, 5, usize),
]
);
}
}