Skip to content

Commit

Permalink
Avoid adding dashed line outside of docstring (#3581)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Mar 17, 2023
1 parent 87fab4a commit babd0a0
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 210 deletions.
9 changes: 9 additions & 0 deletions crates/ruff/resources/test/fixtures/pydocstyle/sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def no_underline_and_no_description(): # noqa: D416
"""


@expect(_D213)
@expect("D407: Missing dashed underline after section ('Returns')")
@expect("D414: Section has no content ('Returns')")
def no_underline_and_no_newline(): # noqa: D416
"""Toggle the gizmo.
Returns"""


@expect(_D213)
@expect("D410: Missing blank line after section ('Returns')")
@expect("D414: Section has no content ('Returns')")
Expand Down
22 changes: 11 additions & 11 deletions crates/ruff/src/rules/pydocstyle/rules/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,15 @@ fn blanks_and_section_underline(
// Add a dashed line (of the appropriate length) under the section header.
let content = format!(
"{}{}{}",
checker.stylist.line_ending().as_str(),
whitespace::clean(docstring.indentation),
"-".repeat(context.section_name.len()),
checker.stylist.line_ending().as_str()
);
diagnostic.amend(Fix::insertion(
content,
Location::new(
docstring.expr.location.row() + context.original_index + 1,
0,
docstring.expr.location.row() + context.original_index,
context.line.trim_end().chars().count(),
),
));
}
Expand Down Expand Up @@ -595,15 +595,15 @@ fn blanks_and_section_underline(
// Add a dashed line (of the appropriate length) under the section header.
let content = format!(
"{}{}{}",
checker.stylist.line_ending().as_str(),
whitespace::clean(docstring.indentation),
"-".repeat(context.section_name.len()),
checker.stylist.line_ending().as_str()
);
diagnostic.amend(Fix::insertion(
content,
Location::new(
docstring.expr.location.row() + context.original_index + 1,
0,
docstring.expr.location.row() + context.original_index,
context.line.trim_end().chars().count(),
),
));
}
Expand Down Expand Up @@ -721,14 +721,14 @@ fn common_section(checker: &mut Checker, docstring: &Docstring, context: &Sectio
);
if checker.patch(diagnostic.kind.rule()) {
// Add a newline after the section.
let line = context.following_lines.last().unwrap_or(&context.line);
diagnostic.amend(Fix::insertion(
line_end.to_string(),
format!("{}{}", line_end, docstring.indentation),
Location::new(
docstring.expr.location.row()
+ context.original_index
+ 1
+ context.following_lines.len(),
0,
line.trim_end().chars().count(),
),
));
}
Expand All @@ -744,14 +744,14 @@ fn common_section(checker: &mut Checker, docstring: &Docstring, context: &Sectio
);
if checker.patch(diagnostic.kind.rule()) {
// Add a newline after the section.
let line = context.following_lines.last().unwrap_or(&context.line);
diagnostic.amend(Fix::insertion(
line_end.to_string(),
Location::new(
docstring.expr.location.row()
+ context.original_index
+ 1
+ context.following_lines.len(),
0,
line.trim_end().chars().count(),
),
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ expression: diagnostics
suggestion: "Remove over-indentation from \"Returns\""
fixable: true
location:
row: 135
row: 144
column: 4
end_location:
row: 141
row: 150
column: 7
fix:
content: " "
location:
row: 137
row: 146
column: 0
end_location:
row: 137
row: 146
column: 8
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ expression: diagnostics
suggestion: "Remove over-indentation from \"Returns\" underline"
fixable: true
location:
row: 147
row: 156
column: 4
end_location:
row: 153
row: 162
column: 7
fix:
content: " "
location:
row: 150
row: 159
column: 0
end_location:
row: 150
row: 159
column: 9
parent: ~
- kind:
Expand All @@ -28,18 +28,18 @@ expression: diagnostics
suggestion: "Remove over-indentation from \"Returns\" underline"
fixable: true
location:
row: 161
row: 170
column: 4
end_location:
row: 165
row: 174
column: 7
fix:
content: " "
location:
row: 164
row: 173
column: 0
end_location:
row: 164
row: 173
column: 9
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ expression: diagnostics
suggestion: "Capitalize \"Short summary\""
fixable: true
location:
row: 207
row: 216
column: 4
end_location:
row: 221
row: 230
column: 7
fix:
content: Short Summary
location:
row: 209
row: 218
column: 4
end_location:
row: 209
row: 218
column: 17
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ expression: diagnostics
suggestion: "Add newline after \"Raises\""
fixable: true
location:
row: 207
row: 216
column: 4
end_location:
row: 221
row: 230
column: 7
fix:
content: ""
location:
row: 218
row: 227
column: 10
end_location:
row: 218
row: 227
column: 11
parent: ~

Loading

0 comments on commit babd0a0

Please sign in to comment.