Skip to content

Commit

Permalink
Fix autofix conflict between D209 and D400 (#3564)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse committed Mar 17, 2023
1 parent d9ed0aa commit f5e5caa
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
3 changes: 3 additions & 0 deletions crates/ruff/resources/test/fixtures/pydocstyle/D209_D400.py
Original file line number Diff line number Diff line change
@@ -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"""
16 changes: 14 additions & 2 deletions crates/ruff/src/autofix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;

use crate::linter::FixTable;
use crate::registry::AsRule;
use crate::registry::{AsRule, Rule};

pub mod helpers;

Expand Down Expand Up @@ -39,7 +39,7 @@ fn apply_fixes<'a>(
.as_ref()
.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
// any re-application.
Expand Down Expand Up @@ -92,6 +92,18 @@ pub(crate) fn apply_fix(fix: &Fix, locator: &Locator) -> String {
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)]
mod tests {
use rustpython_parser::ast::Location;
Expand Down
10 changes: 10 additions & 0 deletions crates/ruff/src/rules/pydocstyle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,14 @@ mod tests {
assert_yaml_snapshot!(diagnostics);
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(())
}
}
Original file line number Diff line number Diff line change
@@ -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: ~

0 comments on commit f5e5caa

Please sign in to comment.