Skip to content

Commit

Permalink
fix: fix todo comments identification
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Oct 20, 2024
1 parent 1ec113d commit d8a9cc6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ chrono = { version = "0.4.38", features = ["serde"] }
clap = { version = "4.5.20", features = ["derive"] }
futures = "0.3.31"
indicatif = "0.17.8"
lazy_static = "1.5.0"
open = "5.3.0"
oxc = "0.31.0"
regex = "1.11.0"
Expand Down
9 changes: 7 additions & 2 deletions src/identify_supported_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use lazy_static::lazy_static;
use regex::Regex;

lazy_static! {
static ref SUPPORTED_FILE_REGEX: Regex =
Regex::new(r"\.[cm]?[jt]sx?$").unwrap();
}

pub fn identify_supported_file(file_name: &str) -> bool {
let re = Regex::new(r"\.[cm]?[jt]sx?$").unwrap();
re.is_match(file_name)
SUPPORTED_FILE_REGEX.is_match(file_name)
}
41 changes: 27 additions & 14 deletions src/identify_todo_comment.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
pub const PRIMARY_TODO_KEYWORDS: [&str; 9] = [
"TODO", "FIXME", "CHANGED", "XXX", "HACK", "BUG", "OPTIMIZE", "REFACTOR",
"TEMP",
use lazy_static::lazy_static;
use regex::Regex;

pub const PRIMARY_TODO_KEYWORDS: [&str; 8] = [
"TODO", "FIXME", "XXX", "HACK", "BUG", "OPTIMIZE", "REFACTOR", "TEMP",
];

pub const SECONDARY_TODO_KEYWORDS: [&str; 13] = [
pub const SECONDARY_TODO_KEYWORDS: [&str; 14] = [
"CHANGED",
"IDEA",
"NOTE",
"REVIEW",
Expand All @@ -19,23 +22,33 @@ pub const SECONDARY_TODO_KEYWORDS: [&str; 13] = [
"COMBAK",
];

lazy_static! {
static ref PRIMARY_KEYWORD_REGEXES: Vec<Regex> = PRIMARY_TODO_KEYWORDS
.iter()
.map(|keyword| Regex::new(&format!(
r"(?i)\b{}\b",
regex::escape(keyword)
))
.unwrap())
.collect();
static ref SECONDARY_KEYWORD_REGEX: Regex =
Regex::new(r"(?i)^[^\w]*(\w+)([\s\p{P}]*)(:|[\p{P}\s]|$)").unwrap();
}

pub fn identify_todo_comment(comment_text: &str) -> Option<String> {
let trimmed_text = comment_text.trim();

let words_with_separators: Vec<&str> =
trimmed_text.split_whitespace().collect();

for keyword in PRIMARY_TODO_KEYWORDS.iter() {
for word in &words_with_separators {
if word.to_uppercase().contains(&keyword.to_uppercase()) {
return Some(keyword.to_string());
}
for (i, re) in PRIMARY_KEYWORD_REGEXES.iter().enumerate() {
if re.is_match(trimmed_text) {
return Some(PRIMARY_TODO_KEYWORDS[i].to_string());
}
}

if let Some(first_word) = words_with_separators.first() {
if let Some(captures) = SECONDARY_KEYWORD_REGEX.captures(trimmed_text) {
let first_word = captures.get(1).unwrap().as_str();

for keyword in SECONDARY_TODO_KEYWORDS.iter() {
if first_word.to_uppercase() == keyword.to_uppercase() {
if first_word.eq_ignore_ascii_case(keyword) {
return Some(keyword.to_string());
}
}
Expand Down
24 changes: 21 additions & 3 deletions tests/identify_todo_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ async fn test_primary_keyword_lowercase() {
}

#[tokio::test]
async fn test_secondary_keyword_at_start() {
let comment = "// FIXME: There is a bug.";
assert_eq!(identify_todo_comment(comment), Some("FIXME".to_string()));
async fn test_secondary_keyword_at_start_with_colon() {
let comment = "// NB: There is a nota bene.";
assert_eq!(identify_todo_comment(comment), Some("NB".to_string()));
}

#[tokio::test]
async fn test_secondary_keyword_at_start_with_no_colon() {
let comment = "// DEBUG: There is a debug.";
assert_eq!(identify_todo_comment(comment), Some("DEBUG".to_string()));
}

#[tokio::test]
Expand Down Expand Up @@ -71,3 +77,15 @@ async fn test_no_keyword_found() {
let comment = "// This is just a comment without anything.";
assert_eq!(identify_todo_comment(comment), None);
}

#[tokio::test]
async fn test_no_keyword_found_with_similar_primary_word() {
let comment = "// I love todoctor";
assert_eq!(identify_todo_comment(comment), None);
}

#[tokio::test]
async fn test_no_keyword_found_with_similar_secondary_word() {
let comment = "// Dangerous stuff";
assert_eq!(identify_todo_comment(comment), None);
}

0 comments on commit d8a9cc6

Please sign in to comment.