mirror of https://github.com/astral-sh/ruff
Move is_overlong to helpers (#2137)
This commit is contained in:
parent
605416922d
commit
0cac1a0d21
|
|
@ -1,3 +1,5 @@
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use regex::Regex;
|
||||||
use rustpython_parser::ast::{Cmpop, Expr, ExprKind};
|
use rustpython_parser::ast::{Cmpop, Expr, ExprKind};
|
||||||
|
|
||||||
use crate::ast::helpers::{create_expr, unparse_expr};
|
use crate::ast::helpers::{create_expr, unparse_expr};
|
||||||
|
|
@ -17,3 +19,40 @@ pub fn compare(left: &Expr, ops: &[Cmpop], comparators: &[Expr], stylist: &Styli
|
||||||
stylist,
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use rustpython_ast::Location;
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::rules::pycodestyle::rules::is_overlong;
|
use crate::rules::pycodestyle::helpers::is_overlong;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
use crate::violations;
|
use crate::violations;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
@ -2,7 +2,7 @@ use rustpython_ast::Location;
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::rules::pycodestyle::rules::is_overlong;
|
use crate::rules::pycodestyle::helpers::is_overlong;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
use crate::violations;
|
use crate::violations;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 do_not_use_bare_except::do_not_use_bare_except;
|
||||||
pub use doc_line_too_long::doc_line_too_long;
|
pub use doc_line_too_long::doc_line_too_long;
|
||||||
pub use invalid_escape_sequence::invalid_escape_sequence;
|
pub use invalid_escape_sequence::invalid_escape_sequence;
|
||||||
pub use is_overlong::is_overlong;
|
|
||||||
pub use line_too_long::line_too_long;
|
pub use line_too_long::line_too_long;
|
||||||
pub use literal_comparisons::literal_comparisons;
|
pub use literal_comparisons::literal_comparisons;
|
||||||
pub use mixed_spaces_and_tabs::mixed_spaces_and_tabs;
|
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 do_not_use_bare_except;
|
||||||
mod doc_line_too_long;
|
mod doc_line_too_long;
|
||||||
mod invalid_escape_sequence;
|
mod invalid_escape_sequence;
|
||||||
mod is_overlong;
|
|
||||||
mod line_too_long;
|
mod line_too_long;
|
||||||
mod literal_comparisons;
|
mod literal_comparisons;
|
||||||
mod mixed_spaces_and_tabs;
|
mod mixed_spaces_and_tabs;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue