Skip to content

Commit

Permalink
Add include_comments option.
Browse files Browse the repository at this point in the history
  • Loading branch information
bencollins54 committed Oct 18, 2023
1 parent d468cb9 commit 0561f44
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ target
debug.log
release
node_modules
tarpaulin-report.html
build_rs_cov.profraw

tarpaulin-report.html
build_rs_cov.profraw
.idea/
4 changes: 4 additions & 0 deletions crates/fta/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn read_config(config_path: &str) -> FtaConfig {
]),
output_limit: Some(5000),
score_cap: Some(1000),
include_comments: Some(false),
};

if Path::new(config_path).exists() {
Expand Down Expand Up @@ -57,6 +58,9 @@ pub fn read_config(config_path: &str) -> FtaConfig {
},
output_limit: provided_config.output_limit.or(default_config.output_limit),
score_cap: provided_config.score_cap.or(default_config.score_cap),
include_comments: provided_config
.include_comments
.or(default_config.include_comments),
}
} else {
default_config
Expand Down
4 changes: 3 additions & 1 deletion crates/fta/src/config/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ mod tests {
"exclude_filenames": [".tmp.go"],
"exclude_directories": ["/test"],
"output_limit": 2500,
"score_cap": 500
"score_cap": 500,
"include_comments": true
}
"#;

Expand Down Expand Up @@ -57,6 +58,7 @@ mod tests {
);
assert_eq!(config.output_limit, Some(2500));
assert_eq!(config.score_cap, Some(500));
assert_eq!(config.include_comments, Some(true));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/fta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn do_analysis(
source_code: &str,
use_tsx: bool,
) -> Result<FileData, Error> {
let (result, line_count) = parse::parse_module(source_code, use_tsx);
let (result, line_count) = parse::parse_module(source_code, use_tsx, config.include_comments);

match result {
Ok(module) => Ok(collect_results(
Expand Down
21 changes: 17 additions & 4 deletions crates/fta/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ use swc_ecma_parser::{error::Error, lexer::Lexer, Parser, Syntax, TsConfig};

mod tests;

pub fn parse_module(source: &str, use_tsx: bool) -> (Result<Module, Error>, usize) {
let line_count = source.lines().count();
pub fn parse_module(
source: &str,
use_tsx: bool,
include_comments: bool,
) -> (Result<Module, Error>, usize) {
let cm: Lrc<SourceMap> = Default::default();
let comments = CountingComments::new();

Expand All @@ -37,9 +40,19 @@ pub fn parse_module(source: &str, use_tsx: bool) -> (Result<Module, Error>, usiz
let mut parser = Parser::new_from(lexer);
let parsed = parser.parse_module();

println!("Lines: {:?}, Comments: {:?}", line_count, comments.count());
println!(
"Lines: {:?}, Comments: {:?}",
source.lines().count(),
comments.count()
);

let line_count = if include_comments == true {
source.lines().count()
} else {
source.lines().count() - comments.count()
};

(parsed, line_count - comments.count())
(parsed, line_count)
}

struct CountingComments {
Expand Down
1 change: 1 addition & 0 deletions crates/fta/src/structs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct FtaConfig {
pub exclude_directories: Option<Vec<String>>,
pub output_limit: Option<usize>,
pub score_cap: Option<usize>,
pub include_comments: Option<bool>,
}

#[derive(Debug, Serialize, PartialEq)]
Expand Down

0 comments on commit 0561f44

Please sign in to comment.