[flake8-todos] Allow words starting with todo (#13640)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Sid 2024-10-14 12:21:45 +02:00 committed by GitHub
parent 5caabe54b6
commit 9bb4722ebf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 63 deletions

View file

@ -287,19 +287,23 @@ impl<'a> TodoDirective<'a> {
pub(crate) fn from_comment(comment: &'a str, comment_range: TextRange) -> Option<Self> {
// The directive's offset from the start of the comment.
let mut relative_offset = TextSize::new(0);
let mut subset_opt = Some(comment);
let mut subset = comment;
// Loop over `#`-delimited sections of the comment to check for directives. This will
// correctly handle cases like `# foo # TODO`.
while let Some(subset) = subset_opt {
loop {
let trimmed = subset.trim_start_matches('#').trim_start();
let offset = subset.text_len() - trimmed.text_len();
relative_offset += offset;
// Find the first word. Don't use split by whitespace because that would include the `:` character
// in `TODO:`
let first_word = trimmed.split(|c: char| !c.is_alphanumeric()).next()?;
// If we detect a TodoDirectiveKind variant substring in the comment, construct and
// return the appropriate TodoDirective
if let Ok(directive_kind) = trimmed.parse::<TodoDirectiveKind>() {
if let Ok(directive_kind) = first_word.parse::<TodoDirectiveKind>() {
let len = directive_kind.len();
return Some(Self {
@ -310,11 +314,11 @@ impl<'a> TodoDirective<'a> {
}
// Shrink the subset to check for the next phrase starting with "#".
subset_opt = if let Some(new_offset) = trimmed.find('#') {
if let Some(new_offset) = trimmed.find('#') {
relative_offset += TextSize::try_from(new_offset).unwrap();
subset.get(relative_offset.to_usize()..)
subset = &subset[relative_offset.to_usize()..];
} else {
None
break;
};
}
@ -334,30 +338,13 @@ impl FromStr for TodoDirectiveKind {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
// The lengths of the respective variant strings: TODO, FIXME, HACK, XXX
for length in [3, 4, 5] {
let Some(substr) = s.get(..length) else {
break;
};
match substr.to_lowercase().as_str() {
"fixme" => {
return Ok(TodoDirectiveKind::Fixme);
}
"hack" => {
return Ok(TodoDirectiveKind::Hack);
}
"todo" => {
return Ok(TodoDirectiveKind::Todo);
}
"xxx" => {
return Ok(TodoDirectiveKind::Xxx);
}
_ => continue,
}
match s.to_lowercase().as_str() {
"fixme" => Ok(TodoDirectiveKind::Fixme),
"hack" => Ok(TodoDirectiveKind::Hack),
"todo" => Ok(TodoDirectiveKind::Todo),
"xxx" => Ok(TodoDirectiveKind::Xxx),
_ => Err(()),
}
Err(())
}
}