From 214dc4ce16fe13461790c08a999f5bedc3167e4a Mon Sep 17 00:00:00 2001 From: Steve C Date: Sun, 15 Oct 2023 19:10:37 -0400 Subject: [PATCH] add fix for D300 --- .../rules/pydocstyle/rules/triple_quotes.rs | 34 ++- ...les__pydocstyle__tests__D300_D.py.snap.new | 201 ++++++++++++++++++ ...er__rules__pydocstyle__tests__bom.snap.new | 16 ++ 3 files changed, 243 insertions(+), 8 deletions(-) create mode 100644 crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D300_D.py.snap.new create mode 100644 crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__bom.snap.new diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/triple_quotes.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/triple_quotes.rs index c8b7d6208a80c1..277bf72d2dac32 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/triple_quotes.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/triple_quotes.rs @@ -1,4 +1,4 @@ -use ruff_diagnostics::{Diagnostic, Violation}; +use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_codegen::Quote; use ruff_text_size::Ranged; @@ -36,7 +36,7 @@ pub struct TripleSingleQuotes { expected_quote: Quote, } -impl Violation for TripleSingleQuotes { +impl AlwaysFixableViolation for TripleSingleQuotes { #[derive_message_formats] fn message(&self) -> String { let TripleSingleQuotes { expected_quote } = self; @@ -45,6 +45,14 @@ impl Violation for TripleSingleQuotes { Quote::Single => format!(r#"Use triple single quotes `'''`"#), } } + + fn fix_title(&self) -> String { + let TripleSingleQuotes { expected_quote } = self; + match expected_quote { + Quote::Double => format!(r#"Use triple double quotes `"""`"#), + Quote::Single => format!(r#"Use triple single quotes `'''`"#), + } + } } /// D300 @@ -60,18 +68,28 @@ pub(crate) fn triple_quotes(checker: &mut Checker, docstring: &Docstring) { match expected_quote { Quote::Single => { if !leading_quote.ends_with("'''") { - checker.diagnostics.push(Diagnostic::new( - TripleSingleQuotes { expected_quote }, + let mut diagnostic = + Diagnostic::new(TripleSingleQuotes { expected_quote }, docstring.range()); + + diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( + format!("'''{}'''", docstring.body().as_str()), docstring.range(), - )); + ))); + + checker.diagnostics.push(diagnostic); } } Quote::Double => { if !leading_quote.ends_with("\"\"\"") { - checker.diagnostics.push(Diagnostic::new( - TripleSingleQuotes { expected_quote }, + let mut diagnostic = + Diagnostic::new(TripleSingleQuotes { expected_quote }, docstring.range()); + + diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( + format!("\"\"\"{}\"\"\"", docstring.body().as_str()), docstring.range(), - )); + ))); + + checker.diagnostics.push(diagnostic); } } } diff --git a/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D300_D.py.snap.new b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D300_D.py.snap.new new file mode 100644 index 00000000000000..e3e9d8cd392caf --- /dev/null +++ b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D300_D.py.snap.new @@ -0,0 +1,201 @@ +--- +source: crates/ruff_linter/src/rules/pydocstyle/mod.rs +assertion_line: 105 +--- +D.py:307:5: D300 [*] Use triple double quotes `"""` + | +305 | @expect('D300: Use """triple double quotes""" (found \'\'\'-quotes)') +306 | def triple_single_quotes_raw(): +307 | r'''Summary.''' + | ^^^^^^^^^^^^^^^ D300 + | + = help: Use triple double quotes `"""` + +ℹ Fix +304 304 | +305 305 | @expect('D300: Use """triple double quotes""" (found \'\'\'-quotes)') +306 306 | def triple_single_quotes_raw(): +307 |- r'''Summary.''' + 307 |+ """Summary.""" +308 308 | +309 309 | +310 310 | @expect('D300: Use """triple double quotes""" (found \'\'\'-quotes)') + +D.py:312:5: D300 [*] Use triple double quotes `"""` + | +310 | @expect('D300: Use """triple double quotes""" (found \'\'\'-quotes)') +311 | def triple_single_quotes_raw_uppercase(): +312 | R'''Summary.''' + | ^^^^^^^^^^^^^^^ D300 + | + = help: Use triple double quotes `"""` + +ℹ Fix +309 309 | +310 310 | @expect('D300: Use """triple double quotes""" (found \'\'\'-quotes)') +311 311 | def triple_single_quotes_raw_uppercase(): +312 |- R'''Summary.''' + 312 |+ """Summary.""" +313 313 | +314 314 | +315 315 | @expect('D300: Use """triple double quotes""" (found \'-quotes)') + +D.py:317:5: D300 [*] Use triple double quotes `"""` + | +315 | @expect('D300: Use """triple double quotes""" (found \'-quotes)') +316 | def single_quotes_raw(): +317 | r'Summary.' + | ^^^^^^^^^^^ D300 + | + = help: Use triple double quotes `"""` + +ℹ Fix +314 314 | +315 315 | @expect('D300: Use """triple double quotes""" (found \'-quotes)') +316 316 | def single_quotes_raw(): +317 |- r'Summary.' + 317 |+ """Summary.""" +318 318 | +319 319 | +320 320 | @expect('D300: Use """triple double quotes""" (found \'-quotes)') + +D.py:322:5: D300 [*] Use triple double quotes `"""` + | +320 | @expect('D300: Use """triple double quotes""" (found \'-quotes)') +321 | def single_quotes_raw_uppercase(): +322 | R'Summary.' + | ^^^^^^^^^^^ D300 + | + = help: Use triple double quotes `"""` + +ℹ Fix +319 319 | +320 320 | @expect('D300: Use """triple double quotes""" (found \'-quotes)') +321 321 | def single_quotes_raw_uppercase(): +322 |- R'Summary.' + 322 |+ """Summary.""" +323 323 | +324 324 | +325 325 | @expect('D300: Use """triple double quotes""" (found \'-quotes)') + +D.py:328:5: D300 [*] Use triple double quotes `"""` + | +326 | @expect('D301: Use r""" if any backslashes in a docstring') +327 | def single_quotes_raw_uppercase_backslash(): +328 | R'Sum\mary.' + | ^^^^^^^^^^^^ D300 + | + = help: Use triple double quotes `"""` + +ℹ Fix +325 325 | @expect('D300: Use """triple double quotes""" (found \'-quotes)') +326 326 | @expect('D301: Use r""" if any backslashes in a docstring') +327 327 | def single_quotes_raw_uppercase_backslash(): +328 |- R'Sum\mary.' + 328 |+ """Sum\mary.""" +329 329 | +330 330 | +331 331 | @expect('D301: Use r""" if any backslashes in a docstring') + +D.py:645:5: D300 [*] Use triple double quotes `"""` + | +644 | def single_line_docstring_with_an_escaped_backslash(): +645 | "\ + | _____^ +646 | | " + | |_____^ D300 +647 | +648 | class StatementOnSameLineAsDocstring: + | + = help: Use triple double quotes `"""` + +ℹ Fix +642 642 | +643 643 | +644 644 | def single_line_docstring_with_an_escaped_backslash(): +645 |- "\ +646 |- " + 645 |+ """\ + 646 |+ """ +647 647 | +648 648 | class StatementOnSameLineAsDocstring: +649 649 | "After this docstring there's another statement on the same line separated by a semicolon." ; priorities=1 + +D.py:649:5: D300 [*] Use triple double quotes `"""` + | +648 | class StatementOnSameLineAsDocstring: +649 | "After this docstring there's another statement on the same line separated by a semicolon." ; priorities=1 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ D300 +650 | def sort_services(self): +651 | pass + | + = help: Use triple double quotes `"""` + +ℹ Fix +646 646 | " +647 647 | +648 648 | class StatementOnSameLineAsDocstring: +649 |- "After this docstring there's another statement on the same line separated by a semicolon." ; priorities=1 + 649 |+ """After this docstring there's another statement on the same line separated by a semicolon.""" ; priorities=1 +650 650 | def sort_services(self): +651 651 | pass +652 652 | + +D.py:654:5: D300 [*] Use triple double quotes `"""` + | +653 | class StatementOnSameLineAsDocstring: +654 | "After this docstring there's another statement on the same line separated by a semicolon."; priorities=1 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ D300 + | + = help: Use triple double quotes `"""` + +ℹ Fix +651 651 | pass +652 652 | +653 653 | class StatementOnSameLineAsDocstring: +654 |- "After this docstring there's another statement on the same line separated by a semicolon."; priorities=1 + 654 |+ """After this docstring there's another statement on the same line separated by a semicolon."""; priorities=1 +655 655 | +656 656 | +657 657 | class CommentAfterDocstring: + +D.py:658:5: D300 [*] Use triple double quotes `"""` + | +657 | class CommentAfterDocstring: +658 | "After this docstring there's a comment." # priorities=1 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ D300 +659 | def sort_services(self): +660 | pass + | + = help: Use triple double quotes `"""` + +ℹ Fix +655 655 | +656 656 | +657 657 | class CommentAfterDocstring: +658 |- "After this docstring there's a comment." # priorities=1 + 658 |+ """After this docstring there's a comment.""" # priorities=1 +659 659 | def sort_services(self): +660 660 | pass +661 661 | + +D.py:664:5: D300 [*] Use triple double quotes `"""` + | +663 | def newline_after_closing_quote(self): +664 | "We enforce a newline after the closing quote for a multi-line docstring \ + | _____^ +665 | | but continuations shouldn't be considered multi-line" + | |_________________________________________________________^ D300 + | + = help: Use triple double quotes `"""` + +ℹ Fix +661 661 | +662 662 | +663 663 | def newline_after_closing_quote(self): +664 |- "We enforce a newline after the closing quote for a multi-line docstring \ +665 |- but continuations shouldn't be considered multi-line" + 664 |+ """We enforce a newline after the closing quote for a multi-line docstring \ + 665 |+ but continuations shouldn't be considered multi-line""" + + diff --git a/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__bom.snap.new b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__bom.snap.new new file mode 100644 index 00000000000000..29f5cb9fe0cf99 --- /dev/null +++ b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__bom.snap.new @@ -0,0 +1,16 @@ +--- +source: crates/ruff_linter/src/rules/pydocstyle/mod.rs +assertion_line: 115 +--- +bom.py:1:1: D300 [*] Use triple double quotes `"""` + | +1 | ''' SAM macro definitions ''' + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ D300 + | + = help: Use triple double quotes `"""` + +ℹ Fix +1 |-''' SAM macro definitions ''' + 1 |+""" SAM macro definitions """ + +