Skip to content

Commit

Permalink
Add TDO006
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed Apr 15, 2023
1 parent d2d036c commit 856be86
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 8 deletions.
5 changes: 5 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_todo/TDO006.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TDO006 - accepted
# TODO(evanrittenhouse): this is a valid TODO
# TDO006 - error
# ToDo(evanrittenhouse): invalid capitalization
# todo(evanrittenhouse): another invalid capitalization
2 changes: 1 addition & 1 deletion crates/ruff/src/checkers/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn check_tokens(
Rule::MissingColonInTodo,
Rule::MissingTextInTodo,
Rule::MissingSpaceAfterColonInTodo,
// Rule::InvalidTodoCapitalization,
Rule::InvalidCapitalizationInTodo,
// Rule::TodoMissingLink,
]);

Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<Rule> {
(Flake8Todo, "002") => Rule::MissingAuthorInTodo,
(Flake8Todo, "004") => Rule::MissingColonInTodo,
(Flake8Todo, "005") => Rule::MissingTextInTodo,
(Flake8Todo, "006") => Rule::InvalidCapitalizationInTodo,
(Flake8Todo, "007") => Rule::MissingSpaceAfterColonInTodo,
_ => return None,
})
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ ruff_macros::register_rules!(
rules::flake8_todo::rules::MissingColonInTodo,
rules::flake8_todo::rules::MissingSpaceAfterColonInTodo,
rules::flake8_todo::rules::MissingTextInTodo,
rules::flake8_todo::rules::InvalidCapitalizationInTodo,
);

pub trait AsRule {
Expand Down Expand Up @@ -938,6 +939,7 @@ impl Rule {
| Rule::MissingAuthorInTodo
| Rule::MissingSpaceAfterColonInTodo
| Rule::MissingTextInTodo
| Rule::InvalidCapitalizationInTodo
| Rule::MissingColonInTodo => LintSource::Tokens,
Rule::IOError => LintSource::Io,
Rule::UnsortedImports | Rule::MissingRequiredImport => LintSource::Imports,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/rules/flake8_todo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod tests {
#[test_case(Rule::MissingAuthorInTodo, Path::new("TDO002.py"); "TDO002")]
#[test_case(Rule::MissingColonInTodo, Path::new("TDO004.py"); "TDO004")]
#[test_case(Rule::MissingTextInTodo, Path::new("TDO005.py"); "TDO005")]
#[test_case(Rule::InvalidCapitalizationInTodo, Path::new("TDO006.py"); "TDO006")]
#[test_case(Rule::MissingSpaceAfterColonInTodo, Path::new("TDO007.py"); "TDO007")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy());
Expand Down
27 changes: 20 additions & 7 deletions crates/ruff/src/rules/flake8_todo/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ impl Violation for MissingTextInTodo {
}
}

// TODO
// #[violation]
// pub struct InvalidTODOCapitalization {
// pub tag: String,
// }
#[violation]
pub struct InvalidCapitalizationInTodo {
pub tag: String,
}
impl Violation for InvalidCapitalizationInTodo {
#[derive_message_formats]
fn message(&self) -> String {
let InvalidCapitalizationInTodo { tag } = self;
format!("Invalid TODO capitalization: `{tag}` should be `TODO`")
}
}

#[violation]
pub struct MissingSpaceAfterColonInTodo;
Expand Down Expand Up @@ -100,7 +106,7 @@ impl Violation for MissingSpaceAfterColonInTodo {
// Note: Tags taken from https://github.com/orsinium-labs/flake8-todos/blob/master/flake8_todos/_rules.py#L12.
static TODO_REGEX: Lazy<Regex> = Lazy::new(|| {
// TODO BEFORE COMMITTING - <space> should be a nested group inside of <colon>
Regex::new(r"^#\s*(TODO|BUG|FIXME|XXX)(\(.*\))?(:)?( )?(.+)?$").unwrap()
Regex::new(r"^#\s*([tT][oO][dD][oO]|BUG|FIXME|XXX)(\(.*\))?(:)?( )?(.+)?$").unwrap()
});
static NUM_CAPTURE_GROUPS: usize = 5usize;

Expand Down Expand Up @@ -138,7 +144,14 @@ fn get_tag_regex_errors(text: &str, start: Location, end: Location) -> Vec<Diagn
Range::new(start, end),
));

// TODO: T006 check can go here
if tag.to_uppercase() == "TODO" {
diagnostics.push(Diagnostic::new(
InvalidCapitalizationInTodo {
tag: String::from(tag),
},
Range::new(start, end),
));
}
}

// Note: This initially looks bad from a speed perspective, but is O(1) given that we
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/ruff/src/rules/flake8_todo/mod.rs
---
TDO006.py:4:1: TDO006 Invalid TODO capitalization: `ToDo` should be `TODO`
|
4 | # TODO(evanrittenhouse): this is a valid TODO
5 | # TDO006 - error
6 | # ToDo(evanrittenhouse): invalid capitalization
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TDO006
7 | # todo(evanrittenhouse): another invalid capitalization
|

TDO006.py:5:1: TDO006 Invalid TODO capitalization: `todo` should be `TODO`
|
5 | # TDO006 - error
6 | # ToDo(evanrittenhouse): invalid capitalization
7 | # todo(evanrittenhouse): another invalid capitalization
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TDO006
|


1 change: 1 addition & 0 deletions ruff.schema.json

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

0 comments on commit 856be86

Please sign in to comment.