Skip to content

Commit

Permalink
Improve comment line counting.
Browse files Browse the repository at this point in the history
  • Loading branch information
bencollins54 committed Oct 18, 2023
1 parent 0561f44 commit 0e8cb31
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions crates/fta-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion crates/fta-wasm/src/lib_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion crates/fta/src/cyclo/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion crates/fta/src/halstead/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion crates/fta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ fn do_analysis(
source_code: &str,
use_tsx: bool,
) -> Result<FileData, Error> {
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(
Expand Down
35 changes: 19 additions & 16 deletions crates/fta/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Comment>) {
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<Comment>) {
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 {
Expand All @@ -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<Vec<swc_common::comments::Comment>> {
fn get_leading(&self, _pos: swc_common::BytePos) -> Option<Vec<swc_common::comments::Comment>> {
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<Vec<swc_common::comments::Comment>> {
fn get_trailing(
&self,
_pos: swc_common::BytePos,
) -> Option<Vec<swc_common::comments::Comment>> {
todo!()
}

fn add_pure_comment(&self, pos: swc_common::BytePos) {
fn add_pure_comment(&self, _pos: swc_common::BytePos) {
todo!()
}
}
Expand Down
11 changes: 5 additions & 6 deletions crates/fta/src/parse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down

0 comments on commit 0e8cb31

Please sign in to comment.