Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pycodestyle-whitespace-rules #1111

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions resources/test/fixtures/pycodestyle/E201.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# E201: Whitespace after '(', '[' or '{'

def foo():
return (1, 2)

x = [1, 2]
x2 = [ 1, 2]

y = {1: 2}
y2 = { 1: 2}

z = (1, 2)
z2 = ( 1, 2)


def fun( x, y):
pass

fun(1, 2)
fun( 1, 3)

my_dict = {'key': 'value'}
my_dict2 = { 'key': 'value' }
# trailing whitespace after '{' below
my_dict3 = {
'key': 'value'
}
38 changes: 38 additions & 0 deletions src/check_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ use crate::{
pyflakes, pygrep_hooks, pylint, pyupgrade,
};

use once_cell::sync::Lazy;
use regex::Regex;

static EXTRANEOUS_WHITESPACE_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"[\[({][ \t]|[ \t][\]}),;:]").unwrap());

const GLOBAL_SCOPE_INDEX: usize = 0;

type DeferralContext = (Vec<usize>, Vec<usize>);
Expand Down Expand Up @@ -198,6 +204,38 @@ where
fn visit_stmt(&mut self, stmt: &'b Stmt) {
self.push_parent(stmt);

// some stuff for whitespace
if self.settings.enabled.contains(&CheckCode::E201) {
// need to put this inside above if statement
println!("checking whitespace");
let logical_line = self
.locator
.slice_source_code_range(&Range::from_located(stmt));
println!("logical line: {}", logical_line);

for regex_match in EXTRANEOUS_WHITESPACE_REGEX.find_iter(&logical_line) {
let text = regex_match.as_str();
let character = text.trim().chars().next().unwrap();
let found = regex_match.start();
println!("found position: {}", found + 1);

println!(
"line bounds: row {}, column {}",
stmt.location.row(),
stmt.location.column()
);
println!("end location {:?}", stmt.end_location.unwrap());
println!("end location added {}", stmt.location.column() + found + 1);
if text.ends_with(' ') {
// assert char in '([{'
self.add_check(Check::new(
CheckKind::WhiteSpaceAfter(character),
Range::from_located(stmt),
))
}
}
}

// Track whether we've seen docstrings, non-imports, etc.
match &stmt.node {
StmtKind::ImportFrom { module, .. } => {
Expand Down
8 changes: 8 additions & 0 deletions src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::pyupgrade::types::Primitive;
)]
pub enum CheckCode {
// pycodestyle errors
E201,
E402,
E501,
E711,
Expand Down Expand Up @@ -528,6 +529,7 @@ pub enum CheckKind {
SyntaxError(String),
TrueFalseComparison(bool, RejectedCmpop),
TypeComparison,
WhiteSpaceAfter(char),
// pycodestyle warnings
NoNewLineAtEndOfFile,
InvalidEscapeSequence(char),
Expand Down Expand Up @@ -808,6 +810,7 @@ impl CheckCode {
pub fn kind(&self) -> CheckKind {
match self {
// pycodestyle errors
CheckCode::E201 => CheckKind::WhiteSpaceAfter('c'),
CheckCode::E402 => CheckKind::ModuleImportNotAtTopOfFile,
CheckCode::E501 => CheckKind::LineTooLong(89, 88),
CheckCode::E711 => CheckKind::NoneComparison(RejectedCmpop::Eq),
Expand Down Expand Up @@ -1227,6 +1230,7 @@ impl CheckCode {
CheckCode::D417 => CheckCategory::Pydocstyle,
CheckCode::D418 => CheckCategory::Pydocstyle,
CheckCode::D419 => CheckCategory::Pydocstyle,
CheckCode::E201 => CheckCategory::Pycodestyle,
CheckCode::E402 => CheckCategory::Pycodestyle,
CheckCode::E501 => CheckCategory::Pycodestyle,
CheckCode::E711 => CheckCategory::Pycodestyle,
Expand Down Expand Up @@ -1374,6 +1378,7 @@ impl CheckKind {
pub fn code(&self) -> &'static CheckCode {
match self {
// pycodestyle errors
CheckKind::WhiteSpaceAfter(_) => &CheckCode::E201,
CheckKind::AmbiguousClassName(_) => &CheckCode::E742,
CheckKind::AmbiguousFunctionName(_) => &CheckCode::E743,
CheckKind::AmbiguousVariableName(_) => &CheckCode::E741,
Expand Down Expand Up @@ -1824,6 +1829,9 @@ impl CheckKind {
CheckKind::InvalidEscapeSequence(char) => {
format!("Invalid escape sequence: '\\{char}'")
}
CheckKind::WhiteSpaceAfter(char) => {
format!("Whitespace after '{char}'")
}
// pylint
CheckKind::UselessImportAlias => {
"Import alias does not rename original package".to_string()
Expand Down
Loading