Skip to content

Commit

Permalink
add auto-fix for E252 (#8142)
Browse files Browse the repository at this point in the history
## Summary

Introduce auto fix for `E252`. This partially address #8121.

## Test Plan

Already covered.
  • Loading branch information
reswqa authored Oct 23, 2023
1 parent 8338143 commit 5a95b25
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 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, TextSize};
Expand Down Expand Up @@ -69,11 +69,15 @@ impl Violation for UnexpectedSpacesAroundKeywordParameterEquals {
#[violation]
pub struct MissingWhitespaceAroundParameterEquals;

impl Violation for MissingWhitespaceAroundParameterEquals {
impl AlwaysFixableViolation for MissingWhitespaceAroundParameterEquals {
#[derive_message_formats]
fn message(&self) -> String {
format!("Missing whitespace around parameter equals")
}

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

fn is_in_def(tokens: &[LogicalLineToken]) -> bool {
Expand Down Expand Up @@ -131,7 +135,13 @@ pub(crate) fn whitespace_around_named_parameter_equals(
if annotated_func_arg && parens == 1 {
let start = token.start();
if start == prev_end && prev_end != TextSize::new(0) {
context.push(MissingWhitespaceAroundParameterEquals, token.range());
let mut diagnostic =
Diagnostic::new(MissingWhitespaceAroundParameterEquals, token.range);
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
" ".to_string(),
token.start(),
)));
context.push_diagnostic(diagnostic);
}

while let Some(next) = iter.peek() {
Expand All @@ -141,7 +151,15 @@ pub(crate) fn whitespace_around_named_parameter_equals(
let next_start = next.start();

if next_start == token.end() {
context.push(MissingWhitespaceAroundParameterEquals, token.range());
let mut diagnostic = Diagnostic::new(
MissingWhitespaceAroundParameterEquals,
token.range,
);
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
" ".to_string(),
token.end(),
)));
context.push_diagnostic(diagnostic);
}
break;
}
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
---
E25.py:46:15: E252 Missing whitespace around parameter equals
E25.py:46:15: E252 [*] Missing whitespace around parameter equals
|
44 | return a + b
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
Expand All @@ -10,8 +10,19 @@ E25.py:46:15: E252 Missing whitespace around parameter equals
47 | return a + b + c
48 | #: Okay
|
= help: Add missing whitespace

E25.py:46:15: E252 Missing whitespace around parameter equals
Fix
43 43 | async def add(a: int = 0, b: int = 0) -> int:
44 44 | return a + b
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
46 |+def add(a: int =0, b: int =0, c: int= 0) -> int:
47 47 | return a + b + c
48 48 | #: Okay
49 49 | def add(a: int = _default(name='f')):

E25.py:46:15: E252 [*] Missing whitespace around parameter equals
|
44 | return a + b
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
Expand All @@ -20,8 +31,19 @@ E25.py:46:15: E252 Missing whitespace around parameter equals
47 | return a + b + c
48 | #: Okay
|
= help: Add missing whitespace

Fix
43 43 | async def add(a: int = 0, b: int = 0) -> int:
44 44 | return a + b
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
46 |+def add(a: int= 0, b: int =0, c: int= 0) -> int:
47 47 | return a + b + c
48 48 | #: Okay
49 49 | def add(a: int = _default(name='f')):

E25.py:46:26: E252 Missing whitespace around parameter equals
E25.py:46:26: E252 [*] Missing whitespace around parameter equals
|
44 | return a + b
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
Expand All @@ -30,8 +52,19 @@ E25.py:46:26: E252 Missing whitespace around parameter equals
47 | return a + b + c
48 | #: Okay
|
= help: Add missing whitespace

E25.py:46:36: E252 Missing whitespace around parameter equals
Fix
43 43 | async def add(a: int = 0, b: int = 0) -> int:
44 44 | return a + b
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
46 |+def add(a: int=0, b: int = 0, c: int= 0) -> int:
47 47 | return a + b + c
48 48 | #: Okay
49 49 | def add(a: int = _default(name='f')):

E25.py:46:36: E252 [*] Missing whitespace around parameter equals
|
44 | return a + b
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
Expand All @@ -40,5 +73,16 @@ E25.py:46:36: E252 Missing whitespace around parameter equals
47 | return a + b + c
48 | #: Okay
|
= help: Add missing whitespace

Fix
43 43 | async def add(a: int = 0, b: int = 0) -> int:
44 44 | return a + b
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
46 |+def add(a: int=0, b: int =0, c: int = 0) -> int:
47 47 | return a + b + c
48 48 | #: Okay
49 49 | def add(a: int = _default(name='f')):


0 comments on commit 5a95b25

Please sign in to comment.