Skip to content

Commit

Permalink
feat: improve todo comment identification
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Oct 19, 2024
1 parent 6e6aac0 commit 72f597a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 39 deletions.
44 changes: 39 additions & 5 deletions src/identify_todo_comment.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
use crate::todo_keywords::TODO_KEYWORDS;
pub const PRIMARY_TODO_KEYWORDS: [&str; 9] = [
"TODO", "FIXME", "CHANGED", "XXX", "HACK", "BUG", "OPTIMIZE", "REFACTOR",
"TEMP",
];

pub const SECONDARY_TODO_KEYWORDS: [&str; 13] = [
"IDEA",
"NOTE",
"REVIEW",
"NB",
"QUESTION",
"DEBUG",
"KLUDGE",
"COMPAT",
"WARNING",
"DANGER",
"INFO",
"DEPRECATED",
"COMBAK",
];

pub fn identify_todo_comment(comment_text: &str) -> Option<String> {
let trimmed_text = comment_text.trim().to_uppercase();
for keyword in TODO_KEYWORDS.iter() {
if trimmed_text.starts_with(keyword) {
return Some(keyword.to_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());
}
}
}

if let Some(first_word) = words_with_separators.first() {
for keyword in SECONDARY_TODO_KEYWORDS.iter() {
if first_word.to_uppercase() == keyword.to_uppercase() {
return Some(keyword.to_string());
}
}
}

None
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ pub mod identify_supported_file;
pub mod identify_todo_comment;
pub mod prepare_blame_data;
pub mod remove_duplicate_dates;
pub mod todo_keywords;
pub mod types;
33 changes: 0 additions & 33 deletions src/todo_keywords.rs

This file was deleted.

73 changes: 73 additions & 0 deletions tests/identify_todo_comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use todoctor::identify_todo_comment::identify_todo_comment;

#[tokio::test]
async fn test_primary_keyword_found() {
let comment = "// TODO: This is a test comment.";
assert_eq!(identify_todo_comment(comment), Some("TODO".to_string()));
}

#[tokio::test]
async fn test_primary_keyword_middle_of_sentence() {
let comment = "// This is a test TODO comment.";
assert_eq!(identify_todo_comment(comment), Some("TODO".to_string()));
}

#[tokio::test]
async fn test_primary_keyword_at_end() {
let comment = "// This is a test TODO";
assert_eq!(identify_todo_comment(comment), Some("TODO".to_string()));
}

#[tokio::test]
async fn test_primary_keyword_lowercase() {
let comment = "// todo: lowercase todo comment.";
assert_eq!(identify_todo_comment(comment), Some("TODO".to_string()));
}

#[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()));
}

#[tokio::test]
async fn test_secondary_keyword_not_found_in_middle() {
let comment = "// This is a REVIEW comment.";
assert_eq!(identify_todo_comment(comment), None);
}

#[tokio::test]
async fn test_comment_without_keywords() {
let comment = "// Just a regular comment.";
assert_eq!(identify_todo_comment(comment), None);
}

#[tokio::test]
async fn test_primary_keyword_with_punctuation() {
let comment = "// bla bla -- TODO: fix this.";
assert_eq!(identify_todo_comment(comment), Some("TODO".to_string()));
}

#[tokio::test]
async fn test_secondary_keyword_case_insensitive() {
let comment = "// fixme: case insensitive.";
assert_eq!(identify_todo_comment(comment), Some("FIXME".to_string()));
}

#[tokio::test]
async fn test_primary_keyword_with_dot() {
let comment = "// This comment has a todo in the middle.";
assert_eq!(identify_todo_comment(comment), Some("TODO".to_string()));
}

#[tokio::test]
async fn test_secondary_keyword_with_case_variation() {
let comment = "// FiXmE: Mixed case keyword at the start.";
assert_eq!(identify_todo_comment(comment), Some("FIXME".to_string()));
}

#[tokio::test]
async fn test_no_keyword_found() {
let comment = "// This is just a comment without anything.";
assert_eq!(identify_todo_comment(comment), None);
}

0 comments on commit 72f597a

Please sign in to comment.