From 0cac1a0d21aeb53459b46cf048b24fc55d73587c Mon Sep 17 00:00:00 2001 From: Eric Roberts Date: Tue, 24 Jan 2023 12:45:35 -0500 Subject: [PATCH] Move is_overlong to helpers (#2137) --- src/rules/pycodestyle/helpers.rs | 39 +++++++++++++++++++ .../pycodestyle/rules/doc_line_too_long.rs | 2 +- src/rules/pycodestyle/rules/is_overlong.rs | 39 ------------------- src/rules/pycodestyle/rules/line_too_long.rs | 2 +- src/rules/pycodestyle/rules/mod.rs | 2 - 5 files changed, 41 insertions(+), 43 deletions(-) delete mode 100644 src/rules/pycodestyle/rules/is_overlong.rs diff --git a/src/rules/pycodestyle/helpers.rs b/src/rules/pycodestyle/helpers.rs index f98b5845ef..f63cf3e297 100644 --- a/src/rules/pycodestyle/helpers.rs +++ b/src/rules/pycodestyle/helpers.rs @@ -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 = 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 +} diff --git a/src/rules/pycodestyle/rules/doc_line_too_long.rs b/src/rules/pycodestyle/rules/doc_line_too_long.rs index d843bcab65..28fa277001 100644 --- a/src/rules/pycodestyle/rules/doc_line_too_long.rs +++ b/src/rules/pycodestyle/rules/doc_line_too_long.rs @@ -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; diff --git a/src/rules/pycodestyle/rules/is_overlong.rs b/src/rules/pycodestyle/rules/is_overlong.rs deleted file mode 100644 index 7617a996c4..0000000000 --- a/src/rules/pycodestyle/rules/is_overlong.rs +++ /dev/null @@ -1,39 +0,0 @@ -use once_cell::sync::Lazy; -use regex::Regex; - -static URL_REGEX: Lazy = 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 -} diff --git a/src/rules/pycodestyle/rules/line_too_long.rs b/src/rules/pycodestyle/rules/line_too_long.rs index 947edbb241..ff5d99e699 100644 --- a/src/rules/pycodestyle/rules/line_too_long.rs +++ b/src/rules/pycodestyle/rules/line_too_long.rs @@ -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; diff --git a/src/rules/pycodestyle/rules/mod.rs b/src/rules/pycodestyle/rules/mod.rs index d0c5e8be86..08ea0c6d54 100644 --- a/src/rules/pycodestyle/rules/mod.rs +++ b/src/rules/pycodestyle/rules/mod.rs @@ -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;