mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-31 15:48:22 +00:00
Avoid trimming docstring if ends in trailing quote (#2025)
Closes #2017.
This commit is contained in:
parent
6072edf5bf
commit
4af2353ef9
7 changed files with 87 additions and 21 deletions
9
resources/test/fixtures/pydocstyle/D.py
vendored
9
resources/test/fixtures/pydocstyle/D.py
vendored
|
@ -606,3 +606,12 @@ def one_liner():
|
|||
r"""Wrong.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@expect('D200: One-line docstring should fit on one line with quotes '
|
||||
'(found 3)')
|
||||
@expect('D212: Multi-line docstring summary should start at the first line')
|
||||
def one_liner():
|
||||
"""Wrong."
|
||||
|
||||
"""
|
||||
|
|
|
@ -30,22 +30,20 @@ pub fn no_surrounding_whitespace(checker: &mut Checker, docstring: &Docstring) {
|
|||
);
|
||||
if checker.patch(diagnostic.kind.rule()) {
|
||||
if let Some(pattern) = leading_quote(contents) {
|
||||
if let Some(quote) = pattern.chars().last() {
|
||||
// If removing whitespace would lead to an invalid string of quote
|
||||
// characters, avoid applying the fix.
|
||||
if !trimmed.ends_with(quote) {
|
||||
diagnostic.amend(Fix::replacement(
|
||||
trimmed.to_string(),
|
||||
Location::new(
|
||||
docstring.expr.location.row(),
|
||||
docstring.expr.location.column() + pattern.len(),
|
||||
),
|
||||
Location::new(
|
||||
docstring.expr.location.row(),
|
||||
docstring.expr.location.column() + pattern.len() + line.chars().count(),
|
||||
),
|
||||
));
|
||||
}
|
||||
// If removing whitespace would lead to an invalid string of quote
|
||||
// characters, avoid applying the fix.
|
||||
if !trimmed.ends_with(pattern.chars().last().unwrap()) {
|
||||
diagnostic.amend(Fix::replacement(
|
||||
trimmed.to_string(),
|
||||
Location::new(
|
||||
docstring.expr.location.row(),
|
||||
docstring.expr.location.column() + pattern.len(),
|
||||
),
|
||||
Location::new(
|
||||
docstring.expr.location.row(),
|
||||
docstring.expr.location.column() + pattern.len() + line.chars().count(),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,11 +31,16 @@ pub fn one_liner(checker: &mut Checker, docstring: &Docstring) {
|
|||
helpers::leading_quote(docstring.contents),
|
||||
helpers::trailing_quote(docstring.contents),
|
||||
) {
|
||||
diagnostic.amend(Fix::replacement(
|
||||
format!("{leading}{}{trailing}", docstring.body.trim()),
|
||||
docstring.expr.location,
|
||||
docstring.expr.end_location.unwrap(),
|
||||
));
|
||||
// If removing whitespace would lead to an invalid string of quote
|
||||
// characters, avoid applying the fix.
|
||||
let trimmed = docstring.body.trim();
|
||||
if !trimmed.ends_with(trailing.chars().last().unwrap()) {
|
||||
diagnostic.amend(Fix::replacement(
|
||||
format!("{leading}{trimmed}{trailing}"),
|
||||
docstring.expr.location,
|
||||
docstring.expr.end_location.unwrap(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
checker.diagnostics.push(diagnostic);
|
||||
|
|
|
@ -53,4 +53,14 @@ expression: diagnostics
|
|||
row: 608
|
||||
column: 7
|
||||
parent: ~
|
||||
- kind:
|
||||
FitsOnOneLine: ~
|
||||
location:
|
||||
row: 615
|
||||
column: 4
|
||||
end_location:
|
||||
row: 617
|
||||
column: 7
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
||||
|
|
|
@ -212,4 +212,14 @@ expression: diagnostics
|
|||
column: 7
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
MultiLineSummarySecondLine: ~
|
||||
location:
|
||||
row: 615
|
||||
column: 4
|
||||
end_location:
|
||||
row: 617
|
||||
column: 7
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
||||
|
|
|
@ -240,4 +240,21 @@ expression: diagnostics
|
|||
row: 581
|
||||
column: 47
|
||||
parent: ~
|
||||
- kind:
|
||||
EndsInPeriod: ~
|
||||
location:
|
||||
row: 615
|
||||
column: 4
|
||||
end_location:
|
||||
row: 617
|
||||
column: 7
|
||||
fix:
|
||||
content: "."
|
||||
location:
|
||||
row: 615
|
||||
column: 14
|
||||
end_location:
|
||||
row: 615
|
||||
column: 14
|
||||
parent: ~
|
||||
|
||||
|
|
|
@ -223,4 +223,21 @@ expression: diagnostics
|
|||
row: 581
|
||||
column: 47
|
||||
parent: ~
|
||||
- kind:
|
||||
EndsInPunctuation: ~
|
||||
location:
|
||||
row: 615
|
||||
column: 4
|
||||
end_location:
|
||||
row: 617
|
||||
column: 7
|
||||
fix:
|
||||
content: "."
|
||||
location:
|
||||
row: 615
|
||||
column: 14
|
||||
end_location:
|
||||
row: 615
|
||||
column: 14
|
||||
parent: ~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue