Skip to content

Commit

Permalink
add auto-fix for E223,224
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa committed Oct 23, 2023
1 parent c704674 commit 7b98b35
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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!("Replaced tab before operator by whitespace")
}
}

/// ## What it does
Expand Down Expand Up @@ -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!("Replaced tab after operator by whitespace")
}
}

/// ## What it does
Expand Down Expand Up @@ -181,10 +189,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(
Expand All @@ -198,7 +211,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));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
---
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
43 | a = 3 # aligned with tab
| ^^^ E223
44 | #:
|
= help: Replaced tab before operator by whitespace

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 |


Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
---
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
| ^^^^ E224
49 | b += 1000
50 | #:
|
= help: Replaced tab after operator by whitespace

Fix
45 45 |
46 46 |
47 47 | #: E224
48 |-a += 1
48 |+a += 1
49 49 | b += 1000
50 50 | #:
51 51 |


0 comments on commit 7b98b35

Please sign in to comment.