mirror of https://github.com/astral-sh/ruff
Implement autofix for `multiple-spaces-after-operator` and `multiple-spaces-before-operator` (#8623)
This commit is contained in:
parent
e0a0ddcf7d
commit
96b265ccec
|
|
@ -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};
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
||||
E22.py:6:2: E221 Multiple spaces before operator
|
||||
ℹ 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
|
||||
|
|
||||
4 | #: E221 E221
|
||||
5 | x = 1
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
||||
E22.py:10:5: E221 Multiple spaces before operator
|
||||
ℹ 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
|
||||
|
|
||||
8 | #: E221 E221
|
||||
9 | x[0] = 1
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
||||
E22.py:15:9: E221 Multiple spaces before operator
|
||||
ℹ 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
|
||||
|
|
||||
13 | x = f(x) + 1
|
||||
14 | y = long_variable + 2
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
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
|
||||
|
|
@ -9,8 +9,19 @@ E22.py:28:8: E222 Multiple spaces after operator
|
|||
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
|
||||
|
|
@ -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
|
||||
|
||||
E22.py:32:4: E222 Multiple spaces after operator
|
||||
ℹ 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
|
||||
|
|
||||
30 | #: E222 E222
|
||||
31 | x = -1
|
||||
|
|
@ -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
|
||||
|
||||
E22.py:35:7: E222 Multiple spaces after operator
|
||||
ℹ 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
|
||||
|
|
||||
33 | long_variable = 3
|
||||
34 | #: E222 E222
|
||||
|
|
@ -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
|
||||
|
|
@ -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 |
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
|
||||
---
|
||||
E24.py:2:8: E241 Multiple spaces after comma
|
||||
E24.py:2:8: E241 [*] Multiple spaces after comma
|
||||
|
|
||||
1 | #: E241
|
||||
2 | a = (1, 2)
|
||||
|
|
@ -9,8 +9,17 @@ E24.py:2:8: E241 Multiple spaces after comma
|
|||
3 | #: Okay
|
||||
4 | b = (1, 20)
|
||||
|
|
||||
= help: Replace with single space
|
||||
|
||||
E24.py:11:18: E241 Multiple spaces after comma
|
||||
ℹ Safe fix
|
||||
1 1 | #: E241
|
||||
2 |-a = (1, 2)
|
||||
2 |+a = (1, 2)
|
||||
3 3 | #: Okay
|
||||
4 4 | b = (1, 20)
|
||||
5 5 | #: E242
|
||||
|
||||
E24.py:11:18: E241 [*] Multiple spaces after comma
|
||||
|
|
||||
9 | #: E241 E241 E241
|
||||
10 | # issue 135
|
||||
|
|
@ -19,8 +28,18 @@ E24.py:11:18: E241 Multiple spaces after comma
|
|||
12 | ef, +h,
|
||||
13 | c, -d]
|
||||
|
|
||||
= help: Replace with single space
|
||||
|
||||
E24.py:12:19: E241 Multiple spaces after comma
|
||||
ℹ Safe fix
|
||||
8 8 | b = (1, 20) # space before 20
|
||||
9 9 | #: E241 E241 E241
|
||||
10 10 | # issue 135
|
||||
11 |-more_spaces = [a, b,
|
||||
11 |+more_spaces = [a, b,
|
||||
12 12 | ef, +h,
|
||||
13 13 | c, -d]
|
||||
|
||||
E24.py:12:19: E241 [*] Multiple spaces after comma
|
||||
|
|
||||
10 | # issue 135
|
||||
11 | more_spaces = [a, b,
|
||||
|
|
@ -28,13 +47,30 @@ E24.py:12:19: E241 Multiple spaces after comma
|
|||
| ^^ E241
|
||||
13 | c, -d]
|
||||
|
|
||||
= help: Replace with single space
|
||||
|
||||
E24.py:13:18: E241 Multiple spaces after comma
|
||||
ℹ Safe fix
|
||||
9 9 | #: E241 E241 E241
|
||||
10 10 | # issue 135
|
||||
11 11 | more_spaces = [a, b,
|
||||
12 |- ef, +h,
|
||||
12 |+ ef, +h,
|
||||
13 13 | c, -d]
|
||||
|
||||
E24.py:13:18: E241 [*] Multiple spaces after comma
|
||||
|
|
||||
11 | more_spaces = [a, b,
|
||||
12 | ef, +h,
|
||||
13 | c, -d]
|
||||
| ^^^ E241
|
||||
|
|
||||
= help: Replace with single space
|
||||
|
||||
ℹ Safe fix
|
||||
10 10 | # issue 135
|
||||
11 11 | more_spaces = [a, b,
|
||||
12 12 | ef, +h,
|
||||
13 |- c, -d]
|
||||
13 |+ c, -d]
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue