Skip to content

Commit

Permalink
Change prefix from TDO to TD
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed May 12, 2023
1 parent 0525c4a commit 0704bf2
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@

# T001 - errors
# XXX (evanrittenhouse): this is not fine
# BUG (evanrittenhouse): this is not fine
# FIXME (evanrittenhouse): this is not fine
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# TODO(evanrittenhouse): this has a colon
# T004 - errors
# TODO this has no colon
# TODO(evanrittenhouse) this has no colon
# TODO(evanrittenhouse 😀) this has no colon
# FIXME add a colon
2 changes: 1 addition & 1 deletion crates/ruff/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ pub enum Linter {
#[prefix = "PTH"]
Flake8UsePathlib,
/// [flake8-todos](https://github.com/orsinium-labs/flake8-todos/)
#[prefix = "TDO"]
#[prefix = "TD"]
Flake8Todo,
/// [eradicate](https://pypi.org/project/eradicate/)
#[prefix = "ERA"]
Expand Down
14 changes: 7 additions & 7 deletions crates/ruff/src/rules/flake8_todo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ mod tests {
use crate::test::test_path;
use crate::{assert_messages, settings};

#[test_case(Rule::InvalidTodoTag, Path::new("TDO001.py"); "TDO001")]
#[test_case(Rule::MissingAuthorInTodo, Path::new("TDO002.py"); "TDO002")]
#[test_case(Rule::MissingLinkInTodo, Path::new("TDO003.py"); "TDO003")]
#[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")]
#[test_case(Rule::InvalidTodoTag, Path::new("TD001.py"); "TD001")]
#[test_case(Rule::MissingAuthorInTodo, Path::new("TD002.py"); "TD002")]
#[test_case(Rule::MissingLinkInTodo, Path::new("TD003.py"); "TD003")]
#[test_case(Rule::MissingColonInTodo, Path::new("TD004.py"); "TD004")]
#[test_case(Rule::MissingTextInTodo, Path::new("TD005.py"); "TD005")]
#[test_case(Rule::InvalidCapitalizationInTodo, Path::new("TD006.py"); "TD006")]
#[test_case(Rule::MissingSpaceAfterColonInTodo, Path::new("TD007.py"); "TD007")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy());
let diagnostics = test_path(
Expand Down
54 changes: 29 additions & 25 deletions crates/ruff/src/rules/flake8_todo/rules.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashMap;


use once_cell::sync::Lazy;

use regex::RegexSet;
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_parser::lexer::LexResult;
use rustpython_parser::Tok;

Expand Down Expand Up @@ -220,13 +220,13 @@ static TODO_REGEX_SET: Lazy<RegexSet> = Lazy::new(|| {

// Maps the index of a particular Regex (specified by its index in the above PATTERNS slice) to the length of the
// tag that we're trying to capture.
static PATTERN_TAG_LENGTH: &'static [usize; 3] = &["TODO".len(), "FIXME".len(), "XXX".len()];
static PATTERN_TAG_LENGTH: &[usize; 3] = &["TODO".len(), "FIXME".len(), "XXX".len()];

static ISSUE_LINK_REGEX_SET: Lazy<RegexSet> = Lazy::new(|| {
RegexSet::new([
r#"^#\s*(http|https)://.*"#, // issue link
r#"^#\s*\d+$"#, // issue code - like "003"
r#"^#\s*[A-Z]{1,6}\-?\d+$"#, // issue code - like "TDO-003"
r#"^#\s*[A-Z]{1,6}\-?\d+$"#, // issue code - like "TD-003"
])
.unwrap()
});
Expand Down Expand Up @@ -261,7 +261,7 @@ pub fn check_todos(
check_for_tag_errors(&tag, &mut diagnostics, autofix, settings);
check_for_static_errors(comment, *token_range, &tag, &mut diagnostics);

// TDO-003
// TD-003
if let Some((next_token, _next_range)) = iter.peek() {
if let Tok::Comment(next_comment) = next_token {
if ISSUE_LINK_REGEX_SET.is_match(next_comment) {
Expand Down Expand Up @@ -312,30 +312,29 @@ fn check_for_tag_errors(
autofix: flags::Autofix,
settings: &Settings,
) {
if tag.content != "TODO" {
if tag.content.to_uppercase() == "TODO" {
// TDO-006
let mut invalid_capitalization = Diagnostic::new(
InvalidCapitalizationInTodo {
tag: tag.content.to_string(),
},
tag.range,
);

if autofix.into() && settings.rules.should_fix(Rule::InvalidCapitalizationInTodo) {
invalid_capitalization.set_fix(Fix::unspecified(Edit::range_replacement(
"TODO".to_string(),
tag.range,
)));
}
if tag.content == "TODO" {
return;
}

diagnostics.push(invalid_capitalization);
if tag.content.to_uppercase() == "TODO" {
// TD-006
let mut invalid_capitalization = Diagnostic::new(
InvalidCapitalizationInTodo {
tag: tag.content.to_string(),
},
tag.range,
);

// Avoid pushing multiple diagnostics for the same range.
return;
if autofix.into() && settings.rules.should_fix(Rule::InvalidCapitalizationInTodo) {
invalid_capitalization.set_fix(Fix::unspecified(Edit::range_replacement(
"TODO".to_string(),
tag.range,
)));
}

// TDO-001
diagnostics.push(invalid_capitalization);
} else {
// TD-001
diagnostics.push(Diagnostic::new(
InvalidTodoTag {
tag: tag.content.to_string(),
Expand All @@ -361,6 +360,11 @@ fn check_for_static_errors(
// Absolute offset of the comment's colon from the start of the file.
let mut colon_offset: Option<TextSize> = None;

let comment_rest = &comment[usize::from(relative_offset)..];
let trimmed_start = comment_rest.trim_start();
// Relative offset from the end of the tag to the start of the rest of the comment
let whitespace_offset = comment_rest.text_len();

// An "author block" must be contained in parentheses, like "(ruff)". To check if it exists,
// we can check the first non-whitespace character after the tag. If that first character is a
// left parenthesis, we can say that we have an author's block.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
source: crates/ruff/src/rules/flake8_todo/mod.rs
---
TD006.py:4:3: TD006 [*] Invalid TODO capitalization: `ToDo` should be `TODO`
|
4 | # TODO (evanrittenhouse): this is a valid TODO
5 | # TDO006 - error
6 | # ToDo (evanrittenhouse): invalid capitalization
| ^^^^ TD006
7 | # todo (evanrittenhouse): another invalid capitalization
|
= help: Replace `ToDo` with `TODO`

Suggested fix
1 1 | # TDO006 - accepted
2 2 | # TODO (evanrittenhouse): this is a valid TODO
3 3 | # TDO006 - error
4 |-# ToDo (evanrittenhouse): invalid capitalization
4 |+# TODO (evanrittenhouse): invalid capitalization
5 5 | # todo (evanrittenhouse): another invalid capitalization

TD006.py:5:3: TD006 [*] Invalid TODO capitalization: `todo` should be `TODO`
|
5 | # TDO006 - error
6 | # ToDo (evanrittenhouse): invalid capitalization
7 | # todo (evanrittenhouse): another invalid capitalization
| ^^^^ TD006
|
= help: Replace `todo` with `TODO`

Suggested fix
2 2 | # TODO (evanrittenhouse): this is a valid TODO
3 3 | # TDO006 - error
4 4 | # ToDo (evanrittenhouse): invalid capitalization
5 |-# todo (evanrittenhouse): another invalid capitalization
5 |+# TODO (evanrittenhouse): another invalid capitalization


Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: crates/ruff/src/rules/flake8_todo/mod.rs
---
TD001.py:7:3: TD001 Invalid TODO tag: `XXX` should be `TODO`
|
7 | # T001 - errors
8 | # XXX (evanrittenhouse): this is not fine
| ^^^ TD001
9 | # FIXME (evanrittenhouse): this is not fine
|

TD001.py:8:3: TD001 Invalid TODO tag: `FIXME` should be `TODO`
|
8 | # T001 - errors
9 | # XXX (evanrittenhouse): this is not fine
10 | # FIXME (evanrittenhouse): this is not fine
| ^^^^^ TD001
|


Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: crates/ruff/src/rules/flake8_todo/mod.rs
---
TD002.py:5:7: TD002 Missing author in TODO. Try: # TODO (<author_name>): ...
|
5 | # TODO(evanrittenhouse): this also has an author
6 | # T002 - errors
7 | # TODO: this has no author
| ^ TD002
8 | # FIXME: neither does this
9 | # TODO : and neither does this
|

TD002.py:6:8: TD002 Missing author in TODO. Try: # TODO (<author_name>): ...
|
6 | # T002 - errors
7 | # TODO: this has no author
8 | # FIXME: neither does this
| ^ TD002
9 | # TODO : and neither does this
|

TD002.py:7:7: TD002 Missing author in TODO. Try: # TODO (<author_name>): ...
|
7 | # TODO: this has no author
8 | # FIXME: neither does this
9 | # TODO : and neither does this
| ^ TD002
|


Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: crates/ruff/src/rules/flake8_todo/mod.rs
---
TD004.py:4:7: TD004 Missing colon in TODO. Try: # TODO: ...
|
4 | # TODO(evanrittenhouse): this has a colon
5 | # T004 - errors
6 | # TODO this has no colon
| ^ TD004
7 | # TODO(evanrittenhouse) this has no colon
8 | # FIXME add a colon
|

TD004.py:5:24: TD004 Missing colon in TODO. Try: # TODO: ...
|
5 | # T004 - errors
6 | # TODO this has no colon
7 | # TODO(evanrittenhouse) this has no colon
| ^ TD004
8 | # FIXME add a colon
|

TD004.py:6:8: TD004 Missing colon in TODO. Try: # TODO: ...
|
6 | # TODO this has no colon
7 | # TODO(evanrittenhouse) this has no colon
8 | # FIXME add a colon
| ^ TD004
|


Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/ruff/src/rules/flake8_todo/mod.rs
---
TD003.py:9:3: TD003 Missing issue link on the line following this TODO
|
9 | # TDO003 - errors
10 | # TODO: this comment has no
| ^^^^ TD003
11 | # link after it
|

TD003.py:12:3: TD003 Missing issue link on the line following this TODO
|
12 | # link after it
13 |
14 | # TODO: here's a TODO with no link after it
| ^^^^ TD003
15 | def foo(x):
16 | return x
|

TD003.py:16:3: TD003 Missing issue link on the line following this TODO
|
16 | return x
17 |
18 | # TODO: here's a TODO on the last line with no link
| ^^^^ TD003
|


Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: crates/ruff/src/rules/flake8_todo/mod.rs
---
TD007.py:5:25: TD007 Missing space after colon in TODO
|
5 | # TODO: so does this
6 | # T007 - errors
7 | # TODO(evanrittenhouse):this has no space after a colon
| ^ TD007
8 | # TODO (evanrittenhouse):this doesn't either
9 | # TODO:neither does this
|

TD007.py:6:26: TD007 Missing space after colon in TODO
|
6 | # T007 - errors
7 | # TODO(evanrittenhouse):this has no space after a colon
8 | # TODO (evanrittenhouse):this doesn't either
| ^ TD007
9 | # TODO:neither does this
10 | # FIXME:and lastly neither does this
|

TD007.py:7:8: TD007 Missing space after colon in TODO
|
7 | # TODO(evanrittenhouse):this has no space after a colon
8 | # TODO (evanrittenhouse):this doesn't either
9 | # TODO:neither does this
| ^ TD007
10 | # FIXME:and lastly neither does this
|

TD007.py:8:9: TD007 Missing space after colon in TODO
|
8 | # TODO (evanrittenhouse):this doesn't either
9 | # TODO:neither does this
10 | # FIXME:and lastly neither does this
| ^ TD007
|


Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: crates/ruff/src/rules/flake8_todo/mod.rs
---
TD005.py:4:25: TD005 Missing text after 'TODO'
|
4 | # TODO(evanrittenhouse): this has text, while the errors do not
5 | # T005 - errors
6 | # TODO(evanrittenhouse):
| ^ TD005
7 | # TODO(evanrittenhouse)
8 | # FIXME
|

TD005.py:5:24: TD005 Missing text after 'TODO'
|
5 | # T005 - errors
6 | # TODO(evanrittenhouse):
7 | # TODO(evanrittenhouse)
| ^ TD005
8 | # FIXME
|

TD005.py:6:8: TD005 Missing text after 'TODO'
|
6 | # TODO(evanrittenhouse):
7 | # TODO(evanrittenhouse)
8 | # FIXME
| ^ TD005
|


0 comments on commit 0704bf2

Please sign in to comment.