mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Implement autofix for relative imports (TID252) (#2739)
This commit is contained in:
parent
dadbfea497
commit
e83ed0ecba
8 changed files with 619 additions and 83 deletions
|
@ -3,6 +3,7 @@ use regex::Regex;
|
|||
|
||||
pub static STRING_QUOTE_PREFIX_REGEX: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(r#"^(?i)[urb]*['"](?P<raw>.*)['"]$"#).unwrap());
|
||||
pub static LOWER_OR_UNDERSCORE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z_]+$").unwrap());
|
||||
|
||||
pub fn is_lower(s: &str) -> bool {
|
||||
let mut cased = false;
|
||||
|
@ -28,6 +29,11 @@ pub fn is_upper(s: &str) -> bool {
|
|||
cased
|
||||
}
|
||||
|
||||
// Module names should be lowercase, and may contain underscore
|
||||
pub fn is_lower_with_underscore(s: &str) -> bool {
|
||||
LOWER_OR_UNDERSCORE.is_match(s)
|
||||
}
|
||||
|
||||
/// Remove prefixes (u, r, b) and quotes around a string. This expects the given
|
||||
/// string to be a valid Python string representation, it doesn't do any
|
||||
/// validation.
|
||||
|
@ -43,7 +49,7 @@ pub fn strip_quotes_and_prefixes(s: &str) -> &str {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::string::{is_lower, is_upper, strip_quotes_and_prefixes};
|
||||
use crate::string::{is_lower, is_lower_with_underscore, is_upper, strip_quotes_and_prefixes};
|
||||
|
||||
#[test]
|
||||
fn test_is_lower() {
|
||||
|
@ -56,6 +62,14 @@ mod tests {
|
|||
assert!(!is_lower("_"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_lower_underscore() {
|
||||
assert!(is_lower_with_underscore("abc"));
|
||||
assert!(is_lower_with_underscore("a_b_c"));
|
||||
assert!(!is_lower_with_underscore("a-b-c"));
|
||||
assert!(!is_lower_with_underscore("a_B_c"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_upper() {
|
||||
assert!(is_upper("ABC"));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue