mirror of https://github.com/astral-sh/ruff
Avoid overlong-line errors for lines that end with URLs (#3663)
This commit is contained in:
parent
d594179275
commit
32be63fd1e
|
|
@ -56,7 +56,29 @@ sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labor
|
||||||
# OK
|
# OK
|
||||||
# https://loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong.url.com
|
# https://loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong.url.com
|
||||||
|
|
||||||
# Not OK
|
# OK
|
||||||
_ = """
|
_ = """
|
||||||
Source: https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
|
Source: https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# OK
|
||||||
|
_ = """
|
||||||
|
[this-is-ok](https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533)
|
||||||
|
[this is ok](https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# OK
|
||||||
|
class Foo:
|
||||||
|
"""
|
||||||
|
@see https://looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong.url.com
|
||||||
|
|
||||||
|
:param dynamodb_scan_kwargs: kwargs pass to <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.scan>
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# Error
|
||||||
|
class Bar:
|
||||||
|
"""
|
||||||
|
This is a long sentence that ends with a shortened URL and, therefore, could easily be broken across multiple lines ([source](https://ruff.rs))
|
||||||
|
"""
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use regex::Regex;
|
|
||||||
use rustpython_parser::ast::{Cmpop, Expr, ExprKind};
|
use rustpython_parser::ast::{Cmpop, Expr, ExprKind};
|
||||||
use rustpython_parser::Tok;
|
use rustpython_parser::Tok;
|
||||||
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
use ruff_python_ast::helpers::{create_expr, unparse_expr};
|
use ruff_python_ast::helpers::{create_expr, unparse_expr};
|
||||||
use ruff_python_ast::source_code::Stylist;
|
use ruff_python_ast::source_code::Stylist;
|
||||||
|
|
@ -21,8 +20,6 @@ pub fn compare(left: &Expr, ops: &[Cmpop], comparators: &[Expr], stylist: &Styli
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
static URL_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^https?://\S+$").unwrap());
|
|
||||||
|
|
||||||
pub fn is_overlong(
|
pub fn is_overlong(
|
||||||
line: &str,
|
line: &str,
|
||||||
line_width: usize,
|
line_width: usize,
|
||||||
|
|
@ -35,22 +32,25 @@ pub fn is_overlong(
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut chunks = line.split_whitespace();
|
let mut chunks = line.split_whitespace();
|
||||||
let (Some(first), Some(second)) = (chunks.next(), chunks.next()) else {
|
let (Some(first_chunk), Some(second_chunk)) = (chunks.next(), chunks.next()) else {
|
||||||
// Single word / no printable chars - no way to make the line shorter
|
// Single word / no printable chars - no way to make the line shorter
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
if first == "#" {
|
if first_chunk == "#" {
|
||||||
if ignore_overlong_task_comments {
|
if ignore_overlong_task_comments {
|
||||||
let second = second.trim_end_matches(':');
|
let second = second_chunk.trim_end_matches(':');
|
||||||
if task_tags.iter().any(|tag| tag == second) {
|
if task_tags.iter().any(|task_tag| task_tag == second) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Do not enforce the line length for commented lines that end with a URL
|
// Do not enforce the line length for lines that end with a URL, as long as the URL
|
||||||
// or contain only a single word.
|
// begins before the limit.
|
||||||
if chunks.last().map_or(true, |c| URL_REGEX.is_match(c)) {
|
let last_chunk = chunks.last().unwrap_or(second_chunk);
|
||||||
|
if last_chunk.contains("://") {
|
||||||
|
if line_width - last_chunk.width() <= limit {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,15 +69,15 @@ expression: diagnostics
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
name: LineTooLong
|
name: LineTooLong
|
||||||
body: Line too long (129 > 88 characters)
|
body: Line too long (147 > 88 characters)
|
||||||
suggestion: ~
|
suggestion: ~
|
||||||
fixable: false
|
fixable: false
|
||||||
location:
|
location:
|
||||||
row: 61
|
row: 83
|
||||||
column: 88
|
column: 88
|
||||||
end_location:
|
end_location:
|
||||||
row: 61
|
row: 83
|
||||||
column: 129
|
column: 147
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue