-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve todo comment identification
- Loading branch information
Showing
4 changed files
with
112 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |