-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement whitespace-around-keywords (E271, E272, E273, E274)
- Loading branch information
1 parent
8261d06
commit 3f4a72d
Showing
11 changed files
with
434 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,58 @@ | ||
#: Okay | ||
True and False | ||
#: E271 | ||
True and False | ||
#: E272 | ||
True and False | ||
#: E271 | ||
if 1: | ||
#: E273 | ||
True and False | ||
#: E273 E274 | ||
True and False | ||
#: E271 | ||
a and b | ||
#: E271 | ||
1 and b | ||
#: E271 | ||
a and 2 | ||
#: E271 E272 | ||
1 and b | ||
#: E271 E272 | ||
a and 2 | ||
#: E272 | ||
this and False | ||
#: E273 | ||
a and b | ||
#: E274 | ||
a and b | ||
#: E273 E274 | ||
this and False | ||
#: Okay | ||
from u import (a, b) | ||
from v import c, d | ||
#: E271 | ||
from w import (e, f) | ||
#: E275 | ||
from w import(e, f) | ||
#: E275 | ||
from importable.module import(e, f) | ||
#: E275 | ||
try: | ||
from importable.module import(e, f) | ||
except ImportError: | ||
pass | ||
#: E275 | ||
if(foo): | ||
pass | ||
else: | ||
pass | ||
#: Okay | ||
matched = {"true": True, "false": False} | ||
#: E275:2:11 | ||
if True: | ||
assert(1) | ||
#: Okay | ||
def f(): | ||
print((yield)) | ||
x = (yield) |
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
81 changes: 81 additions & 0 deletions
81
crates/ruff/src/rules/pycodestyle/rules/whitespace_around_keywords.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,81 @@ | ||
#![allow(dead_code)] | ||
|
||
use once_cell::sync::Lazy; | ||
use regex::Regex; | ||
|
||
use ruff_macros::{define_violation, derive_message_formats}; | ||
|
||
use crate::registry::DiagnosticKind; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
pub struct MultipleSpacesAfterKeyword; | ||
); | ||
impl Violation for MultipleSpacesAfterKeyword { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Multiple spaces after keyword") | ||
} | ||
} | ||
|
||
define_violation!( | ||
pub struct MultipleSpacesBeforeKeyword; | ||
); | ||
impl Violation for MultipleSpacesBeforeKeyword { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Multiple spaces before keyword") | ||
} | ||
} | ||
|
||
define_violation!( | ||
pub struct TabAfterKeyword; | ||
); | ||
impl Violation for TabAfterKeyword { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Tab after keyword") | ||
} | ||
} | ||
|
||
define_violation!( | ||
pub struct TabBeforeKeyword; | ||
); | ||
impl Violation for TabBeforeKeyword { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Tab before keyword") | ||
} | ||
} | ||
|
||
static KEYWORD_REGEX: Lazy<Regex> = Lazy::new(|| { | ||
Regex::new(r"(\s*)\b(?:False|None|True|and|as|assert|async|await|break|class|continue|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|raise|return|try|while|with|yield)\b(\s*)").unwrap() | ||
}); | ||
|
||
/// E271, E272, E273, E274 | ||
#[cfg(feature = "logical_lines")] | ||
pub fn whitespace_around_keywords(line: &str) -> Vec<(usize, DiagnosticKind)> { | ||
let mut diagnostics = vec![]; | ||
for line_match in KEYWORD_REGEX.captures_iter(line) { | ||
let before = line_match.get(1).unwrap(); | ||
let after = line_match.get(2).unwrap(); | ||
|
||
if before.as_str().contains('\t') { | ||
diagnostics.push((before.start(), TabBeforeKeyword.into())); | ||
} else if before.as_str().len() > 1 { | ||
diagnostics.push((before.start(), MultipleSpacesBeforeKeyword.into())); | ||
} | ||
|
||
if after.as_str().contains('\t') { | ||
diagnostics.push((after.start(), TabAfterKeyword.into())); | ||
} else if after.as_str().len() > 1 { | ||
diagnostics.push((after.start(), MultipleSpacesAfterKeyword.into())); | ||
} | ||
} | ||
diagnostics | ||
} | ||
|
||
#[cfg(not(feature = "logical_lines"))] | ||
pub fn whitespace_around_keywords(_line: &str) -> Vec<(usize, DiagnosticKind)> { | ||
vec![] | ||
} |
Oops, something went wrong.