Fix autofix conflict between D209 and D400 (#3564)

This commit is contained in:
Jonathan Plasse 2023-03-17 03:36:25 +01:00 committed by GitHub
parent d9ed0aae69
commit f5e5caaa25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 2 deletions

View file

@ -0,0 +1,3 @@
def lorem():
"""lorem ipsum dolor sit amet consectetur adipiscing elit
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"""

View file

@ -9,7 +9,7 @@ use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range; use ruff_python_ast::types::Range;
use crate::linter::FixTable; use crate::linter::FixTable;
use crate::registry::AsRule; use crate::registry::{AsRule, Rule};
pub mod helpers; pub mod helpers;
@ -39,7 +39,7 @@ fn apply_fixes<'a>(
.as_ref() .as_ref()
.map(|fix| (diagnostic.kind.rule(), fix)) .map(|fix| (diagnostic.kind.rule(), fix))
}) })
.sorted_by_key(|(.., fix)| fix.location) .sorted_by(|(rule1, fix1), (rule2, fix2)| cmp_fix(*rule1, *rule2, fix1, fix2))
{ {
// If we already applied an identical fix as part of another correction, skip // If we already applied an identical fix as part of another correction, skip
// any re-application. // any re-application.
@ -92,6 +92,18 @@ pub(crate) fn apply_fix(fix: &Fix, locator: &Locator) -> String {
output output
} }
/// Compare two fixes.
fn cmp_fix(rule1: Rule, rule2: Rule, fix1: &Fix, fix2: &Fix) -> std::cmp::Ordering {
fix1.location
.cmp(&fix2.location)
.then_with(|| match (&rule1, &rule2) {
// Apply `EndsInPeriod` fixes before `NewLineAfterLastParagraph` fixes.
(Rule::EndsInPeriod, Rule::NewLineAfterLastParagraph) => std::cmp::Ordering::Less,
(Rule::NewLineAfterLastParagraph, Rule::EndsInPeriod) => std::cmp::Ordering::Greater,
_ => std::cmp::Ordering::Equal,
})
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use rustpython_parser::ast::Location; use rustpython_parser::ast::Location;

View file

@ -153,4 +153,14 @@ mod tests {
assert_yaml_snapshot!(diagnostics); assert_yaml_snapshot!(diagnostics);
Ok(()) Ok(())
} }
#[test]
fn d209_d400() -> Result<()> {
let diagnostics = test_path(
Path::new("pydocstyle/D209_D400.py"),
&settings::Settings::for_rules([Rule::NewLineAfterLastParagraph, Rule::EndsInPeriod]),
)?;
assert_yaml_snapshot!(diagnostics);
Ok(())
}
} }

View file

@ -0,0 +1,45 @@
---
source: crates/ruff/src/rules/pydocstyle/mod.rs
expression: diagnostics
---
- kind:
name: NewLineAfterLastParagraph
body: Multi-line docstring closing quotes should be on a separate line
suggestion: Move closing quotes to new line
fixable: true
location:
row: 2
column: 4
end_location:
row: 3
column: 72
fix:
content: "\n "
location:
row: 3
column: 69
end_location:
row: 3
column: 69
parent: ~
- kind:
name: EndsInPeriod
body: First line should end with a period
suggestion: Add period
fixable: true
location:
row: 2
column: 4
end_location:
row: 3
column: 72
fix:
content: "."
location:
row: 3
column: 69
end_location:
row: 3
column: 69
parent: ~