Skip to content

Commit

Permalink
Add fix for unexpected-spaces-around-keyword-parameter-equals (#9072)
Browse files Browse the repository at this point in the history
Closes #9066.
  • Loading branch information
charliermarsh committed Dec 9, 2023
1 parent 829a808 commit f69a35a
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 15 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, TextSize};
Expand Down Expand Up @@ -34,11 +34,15 @@ use crate::rules::pycodestyle::rules::logical_lines::{LogicalLine, LogicalLineTo
#[violation]
pub struct UnexpectedSpacesAroundKeywordParameterEquals;

impl Violation for UnexpectedSpacesAroundKeywordParameterEquals {
impl AlwaysFixableViolation for UnexpectedSpacesAroundKeywordParameterEquals {
#[derive_message_formats]
fn message(&self) -> String {
format!("Unexpected spaces around keyword / parameter equals")
}

fn fix_title(&self) -> String {
format!("Remove whitespace")
}
}

/// ## What it does
Expand Down Expand Up @@ -165,22 +169,31 @@ pub(crate) fn whitespace_around_named_parameter_equals(
}
}
} else {
// If there's space between the preceding token and the equals sign, report it.
if token.start() != prev_end {
context.push(
let mut diagnostic = Diagnostic::new(
UnexpectedSpacesAroundKeywordParameterEquals,
TextRange::new(prev_end, token.start()),
);
diagnostic.set_fix(Fix::safe_edit(Edit::deletion(prev_end, token.start())));
context.push_diagnostic(diagnostic);
}

// If there's space between the equals sign and the following token, report it.
while let Some(next) = iter.peek() {
if next.kind() == TokenKind::NonLogicalNewline {
iter.next();
} else {
if next.start() != token.end() {
context.push(
let mut diagnostic = Diagnostic::new(
UnexpectedSpacesAroundKeywordParameterEquals,
TextRange::new(token.end(), next.start()),
);
diagnostic.set_fix(Fix::safe_edit(Edit::deletion(
token.end(),
next.start(),
)));
context.push_diagnostic(diagnostic);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
E25.py:2:12: E251 Unexpected spaces around keyword / parameter equals
E25.py:2:12: E251 [*] Unexpected spaces around keyword / parameter equals
|
1 | #: E251 E251
2 | def foo(bar = False):
| ^ E251
3 | '''Test function with an error in declaration'''
4 | pass
|
= help: Remove whitespace

E25.py:2:14: E251 Unexpected spaces around keyword / parameter equals
Safe fix
1 1 | #: E251 E251
2 |-def foo(bar = False):
2 |+def foo(bar= False):
3 3 | '''Test function with an error in declaration'''
4 4 | pass
5 5 | #: E251

E25.py:2:14: E251 [*] Unexpected spaces around keyword / parameter equals
|
1 | #: E251 E251
2 | def foo(bar = False):
| ^ E251
3 | '''Test function with an error in declaration'''
4 | pass
|
= help: Remove whitespace

Safe fix
1 1 | #: E251 E251
2 |-def foo(bar = False):
2 |+def foo(bar =False):
3 3 | '''Test function with an error in declaration'''
4 4 | pass
5 5 | #: E251

E25.py:6:9: E251 Unexpected spaces around keyword / parameter equals
E25.py:6:9: E251 [*] Unexpected spaces around keyword / parameter equals
|
4 | pass
5 | #: E251
Expand All @@ -28,8 +46,19 @@ E25.py:6:9: E251 Unexpected spaces around keyword / parameter equals
7 | #: E251
8 | foo(bar =True)
|
= help: Remove whitespace

Safe fix
3 3 | '''Test function with an error in declaration'''
4 4 | pass
5 5 | #: E251
6 |-foo(bar= True)
6 |+foo(bar=True)
7 7 | #: E251
8 8 | foo(bar =True)
9 9 | #: E251 E251

E25.py:8:8: E251 Unexpected spaces around keyword / parameter equals
E25.py:8:8: E251 [*] Unexpected spaces around keyword / parameter equals
|
6 | foo(bar= True)
7 | #: E251
Expand All @@ -38,8 +67,19 @@ E25.py:8:8: E251 Unexpected spaces around keyword / parameter equals
9 | #: E251 E251
10 | foo(bar = True)
|
= help: Remove whitespace

E25.py:10:8: E251 Unexpected spaces around keyword / parameter equals
Safe fix
5 5 | #: E251
6 6 | foo(bar= True)
7 7 | #: E251
8 |-foo(bar =True)
8 |+foo(bar=True)
9 9 | #: E251 E251
10 10 | foo(bar = True)
11 11 | #: E251

E25.py:10:8: E251 [*] Unexpected spaces around keyword / parameter equals
|
8 | foo(bar =True)
9 | #: E251 E251
Expand All @@ -48,8 +88,19 @@ E25.py:10:8: E251 Unexpected spaces around keyword / parameter equals
11 | #: E251
12 | y = bar(root= "sdasd")
|
= help: Remove whitespace

Safe fix
7 7 | #: E251
8 8 | foo(bar =True)
9 9 | #: E251 E251
10 |-foo(bar = True)
10 |+foo(bar= True)
11 11 | #: E251
12 12 | y = bar(root= "sdasd")
13 13 | #: E251:2:29

E25.py:10:10: E251 Unexpected spaces around keyword / parameter equals
E25.py:10:10: E251 [*] Unexpected spaces around keyword / parameter equals
|
8 | foo(bar =True)
9 | #: E251 E251
Expand All @@ -58,8 +109,19 @@ E25.py:10:10: E251 Unexpected spaces around keyword / parameter equals
11 | #: E251
12 | y = bar(root= "sdasd")
|
= help: Remove whitespace

Safe fix
7 7 | #: E251
8 8 | foo(bar =True)
9 9 | #: E251 E251
10 |-foo(bar = True)
10 |+foo(bar =True)
11 11 | #: E251
12 12 | y = bar(root= "sdasd")
13 13 | #: E251:2:29

E25.py:12:14: E251 Unexpected spaces around keyword / parameter equals
E25.py:12:14: E251 [*] Unexpected spaces around keyword / parameter equals
|
10 | foo(bar = True)
11 | #: E251
Expand All @@ -68,8 +130,19 @@ E25.py:12:14: E251 Unexpected spaces around keyword / parameter equals
13 | #: E251:2:29
14 | parser.add_argument('--long-option',
|
= help: Remove whitespace

E25.py:15:29: E251 Unexpected spaces around keyword / parameter equals
Safe fix
9 9 | #: E251 E251
10 10 | foo(bar = True)
11 11 | #: E251
12 |-y = bar(root= "sdasd")
12 |+y = bar(root="sdasd")
13 13 | #: E251:2:29
14 14 | parser.add_argument('--long-option',
15 15 | default=

E25.py:15:29: E251 [*] Unexpected spaces around keyword / parameter equals
|
13 | #: E251:2:29
14 | parser.add_argument('--long-option',
Expand All @@ -80,8 +153,20 @@ E25.py:15:29: E251 Unexpected spaces around keyword / parameter equals
17 | #: E251:1:45
18 | parser.add_argument('--long-option', default
|
= help: Remove whitespace

Safe fix
12 12 | y = bar(root= "sdasd")
13 13 | #: E251:2:29
14 14 | parser.add_argument('--long-option',
15 |- default=
16 |- "/rather/long/filesystem/path/here/blah/blah/blah")
15 |+ default="/rather/long/filesystem/path/here/blah/blah/blah")
17 16 | #: E251:1:45
18 17 | parser.add_argument('--long-option', default
19 18 | ="/rather/long/filesystem/path/here/blah/blah/blah")

E25.py:18:45: E251 Unexpected spaces around keyword / parameter equals
E25.py:18:45: E251 [*] Unexpected spaces around keyword / parameter equals
|
16 | "/rather/long/filesystem/path/here/blah/blah/blah")
17 | #: E251:1:45
Expand All @@ -92,8 +177,20 @@ E25.py:18:45: E251 Unexpected spaces around keyword / parameter equals
20 | #: E251:3:8 E251:3:10
21 | foo(True,
|
= help: Remove whitespace

Safe fix
15 15 | default=
16 16 | "/rather/long/filesystem/path/here/blah/blah/blah")
17 17 | #: E251:1:45
18 |-parser.add_argument('--long-option', default
19 |- ="/rather/long/filesystem/path/here/blah/blah/blah")
18 |+parser.add_argument('--long-option', default="/rather/long/filesystem/path/here/blah/blah/blah")
20 19 | #: E251:3:8 E251:3:10
21 20 | foo(True,
22 21 | baz=(1, 2),

E25.py:23:8: E251 Unexpected spaces around keyword / parameter equals
E25.py:23:8: E251 [*] Unexpected spaces around keyword / parameter equals
|
21 | foo(True,
22 | baz=(1, 2),
Expand All @@ -102,8 +199,19 @@ E25.py:23:8: E251 Unexpected spaces around keyword / parameter equals
24 | )
25 | #: Okay
|
= help: Remove whitespace

E25.py:23:10: E251 Unexpected spaces around keyword / parameter equals
Safe fix
20 20 | #: E251:3:8 E251:3:10
21 21 | foo(True,
22 22 | baz=(1, 2),
23 |- biz = 'foo'
23 |+ biz= 'foo'
24 24 | )
25 25 | #: Okay
26 26 | foo(bar=(1 == 1))

E25.py:23:10: E251 [*] Unexpected spaces around keyword / parameter equals
|
21 | foo(True,
22 | baz=(1, 2),
Expand All @@ -112,5 +220,16 @@ E25.py:23:10: E251 Unexpected spaces around keyword / parameter equals
24 | )
25 | #: Okay
|
= help: Remove whitespace

Safe fix
20 20 | #: E251:3:8 E251:3:10
21 21 | foo(True,
22 22 | baz=(1, 2),
23 |- biz = 'foo'
23 |+ biz ='foo'
24 24 | )
25 25 | #: Okay
26 26 | foo(bar=(1 == 1))


0 comments on commit f69a35a

Please sign in to comment.