Skip to content

Commit

Permalink
Implement autofix for multiple-spaces-after-operator and `multiple-…
Browse files Browse the repository at this point in the history
…spaces-before-operator` (#8623)
  • Loading branch information
charliermarsh authored Nov 11, 2023
1 parent e0a0ddc commit 96b265c
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, Violation};
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_parser::TokenKind;
use ruff_text_size::{Ranged, TextRange};
Expand Down Expand Up @@ -60,11 +60,15 @@ impl AlwaysFixableViolation for TabBeforeOperator {
#[violation]
pub struct MultipleSpacesBeforeOperator;

impl Violation for MultipleSpacesBeforeOperator {
impl AlwaysFixableViolation for MultipleSpacesBeforeOperator {
#[derive_message_formats]
fn message(&self) -> String {
format!("Multiple spaces before operator")
}

fn fix_title(&self) -> String {
format!("Replace with single space")
}
}

/// ## What it does
Expand Down Expand Up @@ -120,11 +124,15 @@ impl AlwaysFixableViolation for TabAfterOperator {
#[violation]
pub struct MultipleSpacesAfterOperator;

impl Violation for MultipleSpacesAfterOperator {
impl AlwaysFixableViolation for MultipleSpacesAfterOperator {
#[derive_message_formats]
fn message(&self) -> String {
format!("Multiple spaces after operator")
}

fn fix_title(&self) -> String {
format!("Replace with single space")
}
}

/// ## What it does
Expand Down Expand Up @@ -175,11 +183,15 @@ impl AlwaysFixableViolation for TabAfterComma {
#[violation]
pub struct MultipleSpacesAfterComma;

impl Violation for MultipleSpacesAfterComma {
impl AlwaysFixableViolation for MultipleSpacesAfterComma {
#[derive_message_formats]
fn message(&self) -> String {
format!("Multiple spaces after comma")
}

fn fix_title(&self) -> String {
format!("Replace with single space")
}
}

/// E221, E222, E223, E224
Expand All @@ -204,10 +216,15 @@ pub(crate) fn space_around_operator(line: &LogicalLine, context: &mut LogicalLin
context.push_diagnostic(diagnostic);
}
(Whitespace::Many, offset) => {
context.push(
let mut diagnostic = Diagnostic::new(
MultipleSpacesBeforeOperator,
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);
}
_ => {}
}
Expand All @@ -224,7 +241,15 @@ pub(crate) fn space_around_operator(line: &LogicalLine, context: &mut LogicalLin
context.push_diagnostic(diagnostic);
}
(Whitespace::Many, len) => {
context.push(MultipleSpacesAfterOperator, TextRange::at(token.end(), len));
let mut diagnostic = Diagnostic::new(
MultipleSpacesAfterOperator,
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);
}
_ => {}
}
Expand All @@ -249,7 +274,13 @@ pub(crate) fn space_after_comma(line: &LogicalLine, context: &mut LogicalLinesCo
context.push_diagnostic(diagnostic);
}
(Whitespace::Many, len) => {
context.push(MultipleSpacesAfterComma, TextRange::at(token.end(), len));
let mut diagnostic =
Diagnostic::new(MultipleSpacesAfterComma, 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);
}
_ => {}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
E22.py:3:6: E221 Multiple spaces before operator
E22.py:3:6: E221 [*] Multiple spaces before operator
|
1 | #: E221
2 | a = 12 + 3
Expand All @@ -10,8 +10,18 @@ E22.py:3:6: E221 Multiple spaces before operator
4 | #: E221 E221
5 | x = 1
|
= help: Replace with single space

E22.py:5:2: E221 Multiple spaces before operator
Safe fix
1 1 | #: E221
2 2 | a = 12 + 3
3 |-b = 4 + 5
3 |+b = 4 + 5
4 4 | #: E221 E221
5 5 | x = 1
6 6 | y = 2

E22.py:5:2: E221 [*] Multiple spaces before operator
|
3 | b = 4 + 5
4 | #: E221 E221
Expand All @@ -20,8 +30,19 @@ E22.py:5:2: E221 Multiple spaces before operator
6 | y = 2
7 | long_variable = 3
|
= help: Replace with single space

Safe fix
2 2 | a = 12 + 3
3 3 | b = 4 + 5
4 4 | #: E221 E221
5 |-x = 1
5 |+x = 1
6 6 | y = 2
7 7 | long_variable = 3
8 8 | #: E221 E221

E22.py:6:2: E221 Multiple spaces before operator
E22.py:6:2: E221 [*] Multiple spaces before operator
|
4 | #: E221 E221
5 | x = 1
Expand All @@ -30,8 +51,19 @@ E22.py:6:2: E221 Multiple spaces before operator
7 | long_variable = 3
8 | #: E221 E221
|
= help: Replace with single space

E22.py:9:5: E221 Multiple spaces before operator
Safe fix
3 3 | b = 4 + 5
4 4 | #: E221 E221
5 5 | x = 1
6 |-y = 2
6 |+y = 2
7 7 | long_variable = 3
8 8 | #: E221 E221
9 9 | x[0] = 1

E22.py:9:5: E221 [*] Multiple spaces before operator
|
7 | long_variable = 3
8 | #: E221 E221
Expand All @@ -40,8 +72,19 @@ E22.py:9:5: E221 Multiple spaces before operator
10 | x[1] = 2
11 | long_variable = 3
|
= help: Replace with single space

Safe fix
6 6 | y = 2
7 7 | long_variable = 3
8 8 | #: E221 E221
9 |-x[0] = 1
9 |+x[0] = 1
10 10 | x[1] = 2
11 11 | long_variable = 3
12 12 | #: E221 E221

E22.py:10:5: E221 Multiple spaces before operator
E22.py:10:5: E221 [*] Multiple spaces before operator
|
8 | #: E221 E221
9 | x[0] = 1
Expand All @@ -50,8 +93,19 @@ E22.py:10:5: E221 Multiple spaces before operator
11 | long_variable = 3
12 | #: E221 E221
|
= help: Replace with single space

E22.py:13:9: E221 Multiple spaces before operator
Safe fix
7 7 | long_variable = 3
8 8 | #: E221 E221
9 9 | x[0] = 1
10 |-x[1] = 2
10 |+x[1] = 2
11 11 | long_variable = 3
12 12 | #: E221 E221
13 13 | x = f(x) + 1

E22.py:13:9: E221 [*] Multiple spaces before operator
|
11 | long_variable = 3
12 | #: E221 E221
Expand All @@ -60,8 +114,19 @@ E22.py:13:9: E221 Multiple spaces before operator
14 | y = long_variable + 2
15 | z = x[0] + 3
|
= help: Replace with single space

Safe fix
10 10 | x[1] = 2
11 11 | long_variable = 3
12 12 | #: E221 E221
13 |-x = f(x) + 1
13 |+x = f(x) + 1
14 14 | y = long_variable + 2
15 15 | z = x[0] + 3
16 16 | #: E221:3:14

E22.py:15:9: E221 Multiple spaces before operator
E22.py:15:9: E221 [*] Multiple spaces before operator
|
13 | x = f(x) + 1
14 | y = long_variable + 2
Expand All @@ -70,8 +135,19 @@ E22.py:15:9: E221 Multiple spaces before operator
16 | #: E221:3:14
17 | text = """
|
= help: Replace with single space

E22.py:19:14: E221 Multiple spaces before operator
Safe fix
12 12 | #: E221 E221
13 13 | x = f(x) + 1
14 14 | y = long_variable + 2
15 |-z = x[0] + 3
15 |+z = x[0] + 3
16 16 | #: E221:3:14
17 17 | text = """
18 18 | bar

E22.py:19:14: E221 [*] Multiple spaces before operator
|
17 | text = """
18 | bar
Expand All @@ -80,5 +156,16 @@ E22.py:19:14: E221 Multiple spaces before operator
20 | #: Okay
21 | x = 1
|
= help: Replace with single space

Safe fix
16 16 | #: E221:3:14
17 17 | text = """
18 18 | bar
19 |- foo %s""" % rofl
19 |+ foo %s""" % rofl
20 20 | #: Okay
21 21 | x = 1
22 22 | y = 2


Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
E22.py:28:8: E222 Multiple spaces after operator
E22.py:28:8: E222 [*] Multiple spaces after operator
|
27 | #: E222
28 | a = a + 1
| ^^ E222
29 | b = b + 10
30 | #: E222 E222
|
= help: Replace with single space

E22.py:31:4: E222 Multiple spaces after operator
Safe fix
25 25 |
26 26 |
27 27 | #: E222
28 |-a = a + 1
28 |+a = a + 1
29 29 | b = b + 10
30 30 | #: E222 E222
31 31 | x = -1

E22.py:31:4: E222 [*] Multiple spaces after operator
|
29 | b = b + 10
30 | #: E222 E222
Expand All @@ -19,8 +30,19 @@ E22.py:31:4: E222 Multiple spaces after operator
32 | y = -2
33 | long_variable = 3
|
= help: Replace with single space

Safe fix
28 28 | a = a + 1
29 29 | b = b + 10
30 30 | #: E222 E222
31 |-x = -1
31 |+x = -1
32 32 | y = -2
33 33 | long_variable = 3
34 34 | #: E222 E222

E22.py:32:4: E222 Multiple spaces after operator
E22.py:32:4: E222 [*] Multiple spaces after operator
|
30 | #: E222 E222
31 | x = -1
Expand All @@ -29,8 +51,19 @@ E22.py:32:4: E222 Multiple spaces after operator
33 | long_variable = 3
34 | #: E222 E222
|
= help: Replace with single space

Safe fix
29 29 | b = b + 10
30 30 | #: E222 E222
31 31 | x = -1
32 |-y = -2
32 |+y = -2
33 33 | long_variable = 3
34 34 | #: E222 E222
35 35 | x[0] = 1

E22.py:35:7: E222 Multiple spaces after operator
E22.py:35:7: E222 [*] Multiple spaces after operator
|
33 | long_variable = 3
34 | #: E222 E222
Expand All @@ -39,8 +72,19 @@ E22.py:35:7: E222 Multiple spaces after operator
36 | x[1] = 2
37 | long_variable = 3
|
= help: Replace with single space

E22.py:36:7: E222 Multiple spaces after operator
Safe fix
32 32 | y = -2
33 33 | long_variable = 3
34 34 | #: E222 E222
35 |-x[0] = 1
35 |+x[0] = 1
36 36 | x[1] = 2
37 37 | long_variable = 3
38 38 | #:

E22.py:36:7: E222 [*] Multiple spaces after operator
|
34 | #: E222 E222
35 | x[0] = 1
Expand All @@ -49,5 +93,16 @@ E22.py:36:7: E222 Multiple spaces after operator
37 | long_variable = 3
38 | #:
|
= help: Replace with single space

Safe fix
33 33 | long_variable = 3
34 34 | #: E222 E222
35 35 | x[0] = 1
36 |-x[1] = 2
36 |+x[1] = 2
37 37 | long_variable = 3
38 38 | #:
39 39 |


Loading

0 comments on commit 96b265c

Please sign in to comment.