Skip to content

Commit

Permalink
Fix single word terminated with dot
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Apr 4, 2024
1 parent d02b106 commit 969cd16
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
6 changes: 6 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pydocstyle/D403.py
Expand Up @@ -25,3 +25,9 @@ def non_ascii():

def all_caps():
"""th•s is not capitalized."""

def single_word():
"""singleword."""

def single_word_no_dot():
"""singleword"""
22 changes: 13 additions & 9 deletions crates/ruff_linter/src/rules/pydocstyle/rules/capitalized.rs
Expand Up @@ -59,26 +59,30 @@ pub(crate) fn capitalized(checker: &mut Checker, docstring: &Docstring) {
}

let body = docstring.body();
let Some(first_word) = body.split(' ').next() else {
return;
};
let first_word = body.split_once(' ').map_or_else(|| body.trim_end_matches('.'), |(first_word, _)| first_word);

// Like pydocstyle, we only support ASCII for now.
for char in first_word.chars() {
if !char.is_ascii_alphabetic() && char != '\'' {
return;
}
}

let mut first_word_chars = first_word.chars();
let Some(first_char) = first_word_chars.next() else {
return;
};

if !first_char.is_ascii() {
return;
}

let uppercase_first_char = first_char.to_ascii_uppercase();
if first_char == uppercase_first_char {
return;
}

// Like pydocstyle, we only support ASCII for now.
for char in first_word.chars().skip(1) {
if !char.is_ascii_alphabetic() && char != '\'' {
return;
}
}

let capitalized_word = uppercase_first_char.to_string() + first_word_chars.as_str();

let mut diagnostic = Diagnostic::new(
Expand Down
Expand Up @@ -19,4 +19,37 @@ D403.py:2:5: D403 [*] First word of the first line should be capitalized: `this`
4 4 | def good_function():
5 5 | """This docstring is capitalized."""

D403.py:30:5: D403 [*] First word of the first line should be capitalized: `singleword` -> `Singleword`
|
29 | def single_word():
30 | """singleword."""
| ^^^^^^^^^^^^^^^^^ D403
31 |
32 | def single_word_no_dot():
|
= help: Capitalize `singleword` to `Singleword`

Safe fix
27 27 | """th•s is not capitalized."""
28 28 |
29 29 | def single_word():
30 |- """singleword."""
30 |+ """Singleword."""
31 31 |
32 32 | def single_word_no_dot():
33 33 | """singleword"""

D403.py:33:5: D403 [*] First word of the first line should be capitalized: `singleword` -> `Singleword`
|
32 | def single_word_no_dot():
33 | """singleword"""
| ^^^^^^^^^^^^^^^^ D403
|
= help: Capitalize `singleword` to `Singleword`

Safe fix
30 30 | """singleword."""
31 31 |
32 32 | def single_word_no_dot():
33 |- """singleword"""
33 |+ """Singleword"""

0 comments on commit 969cd16

Please sign in to comment.