From 0e8cb3117f3bfcde1bbb622283f3036e44ea1cb8 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 19 Oct 2023 00:05:59 +0100 Subject: [PATCH] Improve comment line counting. --- crates/fta-wasm/src/lib.rs | 4 ++-- crates/fta-wasm/src/lib_tests.rs | 2 +- crates/fta/src/cyclo/tests.rs | 2 +- crates/fta/src/halstead/tests.rs | 2 +- crates/fta/src/lib.rs | 6 +++++- crates/fta/src/parse/mod.rs | 35 +++++++++++++++++--------------- crates/fta/src/parse/tests.rs | 11 +++++----- 7 files changed, 34 insertions(+), 28 deletions(-) diff --git a/crates/fta-wasm/src/lib.rs b/crates/fta-wasm/src/lib.rs index fd5d209..157bacd 100644 --- a/crates/fta-wasm/src/lib.rs +++ b/crates/fta-wasm/src/lib.rs @@ -8,10 +8,10 @@ use wasm_bindgen::prelude::*; mod lib_tests; #[wasm_bindgen] -pub fn analyze_file_wasm(source_code: &str, use_tsx: bool) -> String { +pub fn analyze_file_wasm(source_code: &str, use_tsx: bool, include_comments: bool) -> String { let json_string; - match parse::parse_module(source_code, use_tsx) { + match parse::parse_module(source_code, use_tsx, include_comments) { (Ok(module), line_count) => { let (cyclo, halstead_metrics, fta_score) = analyze_file(&module, line_count); let mut analyzed: HashMap<&str, Value> = HashMap::new(); diff --git a/crates/fta-wasm/src/lib_tests.rs b/crates/fta-wasm/src/lib_tests.rs index 2520399..808d44e 100644 --- a/crates/fta-wasm/src/lib_tests.rs +++ b/crates/fta-wasm/src/lib_tests.rs @@ -35,7 +35,7 @@ mod tests { } "#; - let result = analyze_file_wasm(input_code, true); + let result = analyze_file_wasm(input_code, true, false); let expected_json: Value = from_str(expected_output).unwrap(); let actual_json: Value = from_str(&result).unwrap(); diff --git a/crates/fta/src/cyclo/tests.rs b/crates/fta/src/cyclo/tests.rs index aa4ca0f..de5a1ae 100644 --- a/crates/fta/src/cyclo/tests.rs +++ b/crates/fta/src/cyclo/tests.rs @@ -5,7 +5,7 @@ mod tests { use swc_ecma_ast::Module; fn parse(src: &str) -> Module { - match parse_module(src, false) { + match parse_module(src, false, false) { (Ok(module), _line_count) => module, (Err(_err), _) => { panic!("failed"); diff --git a/crates/fta/src/halstead/tests.rs b/crates/fta/src/halstead/tests.rs index 4f05e26..075d245 100644 --- a/crates/fta/src/halstead/tests.rs +++ b/crates/fta/src/halstead/tests.rs @@ -6,7 +6,7 @@ mod tests { use swc_ecma_ast::Module; fn parse(ts_code: &str) -> Module { - let (parsed_module, _line_count) = parse_module(ts_code, true); + let (parsed_module, _line_count) = parse_module(ts_code, true, false); if let Ok(parsed_module) = parsed_module { parsed_module diff --git a/crates/fta/src/lib.rs b/crates/fta/src/lib.rs index d9f9421..64733f0 100644 --- a/crates/fta/src/lib.rs +++ b/crates/fta/src/lib.rs @@ -94,7 +94,11 @@ fn do_analysis( source_code: &str, use_tsx: bool, ) -> Result { - let (result, line_count) = parse::parse_module(source_code, use_tsx, config.include_comments); + let (result, line_count) = parse::parse_module( + source_code, + use_tsx, + config.include_comments.unwrap_or(false), + ); match result { Ok(module) => Ok(collect_results( diff --git a/crates/fta/src/parse/mod.rs b/crates/fta/src/parse/mod.rs index 6a5305a..4901c16 100644 --- a/crates/fta/src/parse/mod.rs +++ b/crates/fta/src/parse/mod.rs @@ -61,28 +61,28 @@ struct CountingComments { impl Comments for CountingComments { fn add_leading(self: &CountingComments, _pos: BytePos, _comment: Comment) { - println!("CommentKind: {:?}", _comment.kind); let current_count = self.count.get(); - self.count.set(current_count + 1); - // match _comment.kind { - // CommentKind::Block => self.count.set(current_count + 3), - // CommentKind::Line => self.count.set(current_count + 1), - // } + self.count + .set(current_count + 1 + _comment.text.matches('\n').count()); } fn add_leading_comments(self: &CountingComments, _pos: BytePos, _comments: Vec) { let current_count = self.count.get(); - self.count.set(current_count + _comments.len()); + let comment_count: usize = _comments + .iter() + .map(|comment| comment.text.matches('\n').count()) + .sum(); + self.count.set(current_count + 1 + comment_count); } fn add_trailing(self: &CountingComments, _pos: BytePos, _comment: Comment) { - let current_count = self.count.get(); - self.count.set(current_count + 1); + // let current_count = self.count.get(); + // self.count.set(current_count + 1); } fn add_trailing_comments(self: &CountingComments, _pos: BytePos, _comments: Vec) { - let current_count = self.count.get(); - self.count.set(current_count + _comments.len()); + // let current_count = self.count.get(); + // self.count.set(current_count + _comments.len()); } fn has_leading(&self, _pos: BytePos) -> bool { @@ -101,23 +101,26 @@ impl Comments for CountingComments { None } - fn move_leading(&self, from: swc_common::BytePos, to: swc_common::BytePos) { + fn move_leading(&self, _from: swc_common::BytePos, _to: swc_common::BytePos) { todo!() } - fn get_leading(&self, pos: swc_common::BytePos) -> Option> { + fn get_leading(&self, _pos: swc_common::BytePos) -> Option> { todo!() } - fn move_trailing(&self, from: swc_common::BytePos, to: swc_common::BytePos) { + fn move_trailing(&self, _from: swc_common::BytePos, _to: swc_common::BytePos) { todo!() } - fn get_trailing(&self, pos: swc_common::BytePos) -> Option> { + fn get_trailing( + &self, + _pos: swc_common::BytePos, + ) -> Option> { todo!() } - fn add_pure_comment(&self, pos: swc_common::BytePos) { + fn add_pure_comment(&self, _pos: swc_common::BytePos) { todo!() } } diff --git a/crates/fta/src/parse/tests.rs b/crates/fta/src/parse/tests.rs index f34c565..2d44988 100644 --- a/crates/fta/src/parse/tests.rs +++ b/crates/fta/src/parse/tests.rs @@ -13,7 +13,7 @@ mod tests { console.log(myResult); // 79 "#; - let (parsed_module, line_count) = parse_module(ts_code, true); + let (parsed_module, line_count) = parse_module(ts_code, true, false); assert!(parsed_module.is_ok(), "Failed to parse TypeScript code"); assert_eq!(line_count, 8, "Incorrect line count"); @@ -23,20 +23,19 @@ mod tests { fn it_ignores_comments() { let ts_code = r#" /* - This is a multi-line comment. - You can write as many lines as you want. - Each line will be part of the comment until the closing tag. + block comment with multiple lines */ function add(a: number, b: number): number { return a + b; } - // This is a single-line comment. + // line comment const myResult = add(23, 56); + /* block comment with single line */ console.log(myResult); // 79 "#; - let (parsed_module, line_count) = parse_module(ts_code, true); + let (parsed_module, line_count) = parse_module(ts_code, true, false); assert!(parsed_module.is_ok(), "Failed to parse TypeScript code"); assert_eq!(line_count, 8, "Incorrect line count");