diff --git a/src/identify_todo_comment.rs b/src/identify_todo_comment.rs index 167d2db..dce6011 100644 --- a/src/identify_todo_comment.rs +++ b/src/identify_todo_comment.rs @@ -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 { - 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 } diff --git a/src/lib.rs b/src/lib.rs index eb4121a..f2e1f7f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/todo_keywords.rs b/src/todo_keywords.rs deleted file mode 100644 index 2102435..0000000 --- a/src/todo_keywords.rs +++ /dev/null @@ -1,33 +0,0 @@ -pub const TODO_KEYWORDS: [&str; 31] = [ - "TODO", - "FIXME", - "CHANGED", - "XXX", - "IDEA", - "HACK", - "NOTE", - "REVIEW", - "NB", - "BUG", - "QUESTION", - "COMBAK", - "TEMP", - "DEBUG", - "OPTIMIZE", - "HACK", - "KLUDGE", - "COMPAT", - "WARNING", - "DANGER", - "INFO", - "IDEA", - "REFACTOR", - "DEPRECATED", - "DOC", - "DOCME", - "DOCMEUP", - "DOCYOU", - "DOCU", - "DOCUP", - "DOCUS", -]; diff --git a/tests/identify_todo_comment.rs b/tests/identify_todo_comment.rs new file mode 100644 index 0000000..ad72dae --- /dev/null +++ b/tests/identify_todo_comment.rs @@ -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); +}