-
Notifications
You must be signed in to change notification settings - Fork 944
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement pycodestyle whitespace lints W291, W293
- Loading branch information
Showing
11 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def first_func(): | ||
# The line below has two spaces after its final character | ||
pass | ||
|
||
# The line below has only spaces, but that's for W293 | ||
|
||
# Don't want trailing tabs either | ||
x = 1000 | ||
# Nor a mix of tabs and spaces | ||
y = 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def first_func(): | ||
# The line below has two spaces after its final character, but that's for W291 | ||
pass | ||
|
||
# The line below has only spaces | ||
|
||
# Don't want trailing tabs either, but that's for W291 | ||
x = 1000 | ||
# Nor a mix of tabs and spaces, but that's for W291 | ||
y = 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
crates/ruff/src/rules/pycodestyle/rules/trailing_whitespace.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use ruff_macros::{define_violation, derive_message_formats}; | ||
use rustpython_parser::ast::Location; | ||
|
||
use crate::ast::types::Range; | ||
use crate::fix::Fix; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::AlwaysAutofixableViolation; | ||
|
||
define_violation!( | ||
pub struct TrailingWhitespace; | ||
); | ||
impl AlwaysAutofixableViolation for TrailingWhitespace { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Trailing whitespace") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
"Remove trailing whitespace".to_string() | ||
} | ||
} | ||
|
||
define_violation!( | ||
pub struct BlankLineContainsWhitespace; | ||
); | ||
impl AlwaysAutofixableViolation for BlankLineContainsWhitespace { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Blank line contains whitespace") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
"Remove whitespace from blank line".to_string() | ||
} | ||
} | ||
|
||
/// W291 & W293 | ||
pub fn trailing_whitespace( | ||
lineno: usize, | ||
line: &str, | ||
enforce_trailing_whitespace: bool, | ||
fix_trailing_whitespace: bool, | ||
enforce_blank_line_contains_whitespace: bool, | ||
fix_blank_line_contains_whitespace: bool, | ||
) -> Option<Diagnostic> { | ||
let whitespace_count = line.chars().rev().take_while(|c| c.is_whitespace()).count(); | ||
if whitespace_count > 0 { | ||
let start = Location::new(lineno + 1, line.len() - whitespace_count); | ||
let end = Location::new(lineno + 1, line.len()); | ||
|
||
if whitespace_count == line.len() { | ||
if enforce_blank_line_contains_whitespace { | ||
let mut diagnostic = | ||
Diagnostic::new(BlankLineContainsWhitespace, Range::new(start, end)); | ||
if fix_blank_line_contains_whitespace { | ||
diagnostic.amend(Fix::deletion(start, end)); | ||
} | ||
return Some(diagnostic); | ||
} | ||
} else if enforce_trailing_whitespace { | ||
let mut diagnostic = Diagnostic::new(TrailingWhitespace, Range::new(start, end)); | ||
if fix_trailing_whitespace { | ||
diagnostic.amend(Fix::deletion(start, end)); | ||
} | ||
return Some(diagnostic); | ||
} | ||
} | ||
None | ||
} |
56 changes: 56 additions & 0 deletions
56
...s/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__W291_W291.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
source: crates/ruff/src/rules/pycodestyle/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
TrailingWhitespace: ~ | ||
location: | ||
row: 3 | ||
column: 8 | ||
end_location: | ||
row: 3 | ||
column: 10 | ||
fix: | ||
content: "" | ||
location: | ||
row: 3 | ||
column: 8 | ||
end_location: | ||
row: 3 | ||
column: 10 | ||
parent: ~ | ||
- kind: | ||
TrailingWhitespace: ~ | ||
location: | ||
row: 8 | ||
column: 8 | ||
end_location: | ||
row: 8 | ||
column: 9 | ||
fix: | ||
content: "" | ||
location: | ||
row: 8 | ||
column: 8 | ||
end_location: | ||
row: 8 | ||
column: 9 | ||
parent: ~ | ||
- kind: | ||
TrailingWhitespace: ~ | ||
location: | ||
row: 10 | ||
column: 7 | ||
end_location: | ||
row: 10 | ||
column: 9 | ||
fix: | ||
content: "" | ||
location: | ||
row: 10 | ||
column: 7 | ||
end_location: | ||
row: 10 | ||
column: 9 | ||
parent: ~ | ||
|
22 changes: 22 additions & 0 deletions
22
...s/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__W293_W293.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
source: crates/ruff/src/rules/pycodestyle/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
BlankLineContainsWhitespace: ~ | ||
location: | ||
row: 6 | ||
column: 0 | ||
end_location: | ||
row: 6 | ||
column: 4 | ||
fix: | ||
content: "" | ||
location: | ||
row: 6 | ||
column: 0 | ||
end_location: | ||
row: 6 | ||
column: 4 | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.