add auto-fix for E223,224,242 (#8143)
## Summary Introduce auto fix for `E223`, `E224`, `E242`. This partially address #8120. ## Test Plan Already covered.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use ruff_diagnostics::Violation;
|
||||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, Violation};
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_python_parser::TokenKind;
|
||||
use ruff_text_size::{Ranged, TextRange};
|
||||
@@ -28,11 +28,15 @@ use super::{LogicalLine, Whitespace};
|
||||
#[violation]
|
||||
pub struct TabBeforeOperator;
|
||||
|
||||
impl Violation for TabBeforeOperator {
|
||||
impl AlwaysFixableViolation for TabBeforeOperator {
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
format!("Tab before operator")
|
||||
}
|
||||
|
||||
fn fix_title(&self) -> String {
|
||||
format!("Replace with single space")
|
||||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
@@ -84,11 +88,15 @@ impl Violation for MultipleSpacesBeforeOperator {
|
||||
#[violation]
|
||||
pub struct TabAfterOperator;
|
||||
|
||||
impl Violation for TabAfterOperator {
|
||||
impl AlwaysFixableViolation for TabAfterOperator {
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
format!("Tab after operator")
|
||||
}
|
||||
|
||||
fn fix_title(&self) -> String {
|
||||
format!("Replace with single space")
|
||||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
@@ -138,11 +146,15 @@ impl Violation for MultipleSpacesAfterOperator {
|
||||
#[violation]
|
||||
pub struct TabAfterComma;
|
||||
|
||||
impl Violation for TabAfterComma {
|
||||
impl AlwaysFixableViolation for TabAfterComma {
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
format!("Tab after comma")
|
||||
}
|
||||
|
||||
fn fix_title(&self) -> String {
|
||||
format!("Replace with single space")
|
||||
}
|
||||
}
|
||||
|
||||
/// ## What it does
|
||||
@@ -181,10 +193,15 @@ pub(crate) fn space_around_operator(line: &LogicalLine, context: &mut LogicalLin
|
||||
if !after_operator {
|
||||
match line.leading_whitespace(token) {
|
||||
(Whitespace::Tab, offset) => {
|
||||
context.push(
|
||||
let mut diagnostic = Diagnostic::new(
|
||||
TabBeforeOperator,
|
||||
TextRange::at(token.start() - offset, offset),
|
||||
);
|
||||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
|
||||
" ".to_string(),
|
||||
TextRange::at(token.start() - offset, offset),
|
||||
)));
|
||||
context.push_diagnostic(diagnostic);
|
||||
}
|
||||
(Whitespace::Many, offset) => {
|
||||
context.push(
|
||||
@@ -198,7 +215,13 @@ pub(crate) fn space_around_operator(line: &LogicalLine, context: &mut LogicalLin
|
||||
|
||||
match line.trailing_whitespace(token) {
|
||||
(Whitespace::Tab, len) => {
|
||||
context.push(TabAfterOperator, TextRange::at(token.end(), len));
|
||||
let mut diagnostic =
|
||||
Diagnostic::new(TabAfterOperator, TextRange::at(token.end(), len));
|
||||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
|
||||
" ".to_string(),
|
||||
TextRange::at(token.end(), len),
|
||||
)));
|
||||
context.push_diagnostic(diagnostic);
|
||||
}
|
||||
(Whitespace::Many, len) => {
|
||||
context.push(MultipleSpacesAfterOperator, TextRange::at(token.end(), len));
|
||||
@@ -217,7 +240,13 @@ pub(crate) fn space_after_comma(line: &LogicalLine, context: &mut LogicalLinesCo
|
||||
if matches!(token.kind(), TokenKind::Comma) {
|
||||
match line.trailing_whitespace(token) {
|
||||
(Whitespace::Tab, len) => {
|
||||
context.push(TabAfterComma, TextRange::at(token.end(), len));
|
||||
let mut diagnostic =
|
||||
Diagnostic::new(TabAfterComma, TextRange::at(token.end(), len));
|
||||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
|
||||
" ".to_string(),
|
||||
TextRange::at(token.end(), len),
|
||||
)));
|
||||
context.push_diagnostic(diagnostic);
|
||||
}
|
||||
(Whitespace::Many, len) => {
|
||||
context.push(MultipleSpacesAfterComma, TextRange::at(token.end(), len));
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
|
||||
---
|
||||
E22.py:43:2: E223 Tab before operator
|
||||
E22.py:43:2: E223 [*] Tab before operator
|
||||
|
|
||||
41 | #: E223
|
||||
42 | foobart = 4
|
||||
@@ -9,5 +9,16 @@ E22.py:43:2: E223 Tab before operator
|
||||
| ^^^ E223
|
||||
44 | #:
|
||||
|
|
||||
= help: Replace with single space
|
||||
|
||||
ℹ Fix
|
||||
40 40 |
|
||||
41 41 | #: E223
|
||||
42 42 | foobart = 4
|
||||
43 |-a = 3 # aligned with tab
|
||||
43 |+a = 3 # aligned with tab
|
||||
44 44 | #:
|
||||
45 45 |
|
||||
46 46 |
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
|
||||
---
|
||||
E22.py:48:5: E224 Tab after operator
|
||||
E22.py:48:5: E224 [*] Tab after operator
|
||||
|
|
||||
47 | #: E224
|
||||
48 | a += 1
|
||||
@@ -9,5 +9,16 @@ E22.py:48:5: E224 Tab after operator
|
||||
49 | b += 1000
|
||||
50 | #:
|
||||
|
|
||||
= help: Replace with single space
|
||||
|
||||
ℹ Fix
|
||||
45 45 |
|
||||
46 46 |
|
||||
47 47 | #: E224
|
||||
48 |-a += 1
|
||||
48 |+a += 1
|
||||
49 49 | b += 1000
|
||||
50 50 | #:
|
||||
51 51 |
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
|
||||
---
|
||||
E24.py:6:8: E242 Tab after comma
|
||||
E24.py:6:8: E242 [*] Tab after comma
|
||||
|
|
||||
4 | b = (1, 20)
|
||||
5 | #: E242
|
||||
@@ -10,5 +10,16 @@ E24.py:6:8: E242 Tab after comma
|
||||
7 | #: Okay
|
||||
8 | b = (1, 20) # space before 20
|
||||
|
|
||||
= help: Replace with single space
|
||||
|
||||
ℹ Fix
|
||||
3 3 | #: Okay
|
||||
4 4 | b = (1, 20)
|
||||
5 5 | #: E242
|
||||
6 |-a = (1, 2) # tab before 2
|
||||
6 |+a = (1, 2) # tab before 2
|
||||
7 7 | #: Okay
|
||||
8 8 | b = (1, 20) # space before 20
|
||||
9 9 | #: E241 E241 E241
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user