Move is_overlong to helpers (#2137)

This commit is contained in:
Eric Roberts 2023-01-24 12:45:35 -05:00 committed by GitHub
parent 605416922d
commit 0cac1a0d21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 43 deletions

View file

@ -1,3 +1,5 @@
use once_cell::sync::Lazy;
use regex::Regex;
use rustpython_parser::ast::{Cmpop, Expr, ExprKind};
use crate::ast::helpers::{create_expr, unparse_expr};
@ -17,3 +19,40 @@ pub fn compare(left: &Expr, ops: &[Cmpop], comparators: &[Expr], stylist: &Styli
stylist,
)
}
static URL_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^https?://\S+$").unwrap());
pub fn is_overlong(
line: &str,
line_length: usize,
limit: usize,
ignore_overlong_task_comments: bool,
task_tags: &[String],
) -> bool {
if line_length <= limit {
return false;
}
let mut chunks = line.split_whitespace();
let (Some(first), Some(second)) = (chunks.next(), chunks.next()) else {
// Single word / no printable chars - no way to make the line shorter
return false;
};
if first == "#" {
if ignore_overlong_task_comments {
let second = second.trim_end_matches(':');
if task_tags.iter().any(|tag| tag == second) {
return false;
}
}
// Do not enforce the line length for commented lines that end with a URL
// or contain only a single word.
if chunks.last().map_or(true, |c| URL_REGEX.is_match(c)) {
return false;
}
}
true
}

View file

@ -2,7 +2,7 @@ use rustpython_ast::Location;
use crate::ast::types::Range;
use crate::registry::Diagnostic;
use crate::rules::pycodestyle::rules::is_overlong;
use crate::rules::pycodestyle::helpers::is_overlong;
use crate::settings::Settings;
use crate::violations;

View file

@ -1,39 +0,0 @@
use once_cell::sync::Lazy;
use regex::Regex;
static URL_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^https?://\S+$").unwrap());
pub fn is_overlong(
line: &str,
line_length: usize,
limit: usize,
ignore_overlong_task_comments: bool,
task_tags: &[String],
) -> bool {
if line_length <= limit {
return false;
}
let mut chunks = line.split_whitespace();
let (Some(first), Some(second)) = (chunks.next(), chunks.next()) else {
// Single word / no printable chars - no way to make the line shorter
return false;
};
if first == "#" {
if ignore_overlong_task_comments {
let second = second.trim_end_matches(':');
if task_tags.iter().any(|tag| tag == second) {
return false;
}
}
// Do not enforce the line length for commented lines that end with a URL
// or contain only a single word.
if chunks.last().map_or(true, |c| URL_REGEX.is_match(c)) {
return false;
}
}
true
}

View file

@ -2,7 +2,7 @@ use rustpython_ast::Location;
use crate::ast::types::Range;
use crate::registry::Diagnostic;
use crate::rules::pycodestyle::rules::is_overlong;
use crate::rules::pycodestyle::helpers::is_overlong;
use crate::settings::Settings;
use crate::violations;

View file

@ -5,7 +5,6 @@ pub use do_not_assign_lambda::do_not_assign_lambda;
pub use do_not_use_bare_except::do_not_use_bare_except;
pub use doc_line_too_long::doc_line_too_long;
pub use invalid_escape_sequence::invalid_escape_sequence;
pub use is_overlong::is_overlong;
pub use line_too_long::line_too_long;
pub use literal_comparisons::literal_comparisons;
pub use mixed_spaces_and_tabs::mixed_spaces_and_tabs;
@ -20,7 +19,6 @@ mod do_not_assign_lambda;
mod do_not_use_bare_except;
mod doc_line_too_long;
mod invalid_escape_sequence;
mod is_overlong;
mod line_too_long;
mod literal_comparisons;
mod mixed_spaces_and_tabs;