mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
Remove source_code's dependency on pydocstyle (#3366)
This commit is contained in:
parent
709dba2e71
commit
fc8ca6edd2
13 changed files with 54 additions and 52 deletions
|
@ -8,6 +8,7 @@ pub mod helpers;
|
||||||
pub mod logging;
|
pub mod logging;
|
||||||
pub mod operations;
|
pub mod operations;
|
||||||
pub mod relocate;
|
pub mod relocate;
|
||||||
|
pub mod strings;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod typing;
|
pub mod typing;
|
||||||
pub mod visitor;
|
pub mod visitor;
|
||||||
|
|
38
crates/ruff/src/ast/strings.rs
Normal file
38
crates/ruff/src/ast/strings.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
use ruff_python_stdlib::str::{
|
||||||
|
SINGLE_QUOTE_PREFIXES, SINGLE_QUOTE_SUFFIXES, TRIPLE_QUOTE_PREFIXES, TRIPLE_QUOTE_SUFFIXES,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Strip the leading and trailing quotes from a docstring.
|
||||||
|
pub fn raw_contents(contents: &str) -> &str {
|
||||||
|
for pattern in TRIPLE_QUOTE_PREFIXES {
|
||||||
|
if contents.starts_with(pattern) {
|
||||||
|
return &contents[pattern.len()..contents.len() - 3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for pattern in SINGLE_QUOTE_PREFIXES {
|
||||||
|
if contents.starts_with(pattern) {
|
||||||
|
return &contents[pattern.len()..contents.len() - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unreachable!("Expected docstring to start with a valid triple- or single-quote prefix")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the leading quote string for a docstring (e.g., `"""`).
|
||||||
|
pub fn leading_quote(content: &str) -> Option<&str> {
|
||||||
|
if let Some(first_line) = content.lines().next() {
|
||||||
|
for pattern in TRIPLE_QUOTE_PREFIXES.iter().chain(SINGLE_QUOTE_PREFIXES) {
|
||||||
|
if first_line.starts_with(pattern) {
|
||||||
|
return Some(pattern);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the trailing quote string for a docstring (e.g., `"""`).
|
||||||
|
pub fn trailing_quote(content: &str) -> Option<&&str> {
|
||||||
|
TRIPLE_QUOTE_SUFFIXES
|
||||||
|
.iter()
|
||||||
|
.chain(SINGLE_QUOTE_SUFFIXES)
|
||||||
|
.find(|&pattern| content.ends_with(pattern))
|
||||||
|
}
|
|
@ -43,7 +43,7 @@ use crate::settings::types::PythonVersion;
|
||||||
use crate::settings::{flags, Settings};
|
use crate::settings::{flags, Settings};
|
||||||
use crate::source_code::{Indexer, Locator, Stylist};
|
use crate::source_code::{Indexer, Locator, Stylist};
|
||||||
use crate::visibility::transition_scope;
|
use crate::visibility::transition_scope;
|
||||||
use crate::{autofix, docstrings, noqa, visibility};
|
use crate::{ast, autofix, docstrings, noqa, visibility};
|
||||||
|
|
||||||
mod deferred;
|
mod deferred;
|
||||||
|
|
||||||
|
@ -5298,7 +5298,7 @@ impl<'a> Checker<'a> {
|
||||||
Location::new(expr.location.row(), expr.location.column()),
|
Location::new(expr.location.row(), expr.location.column()),
|
||||||
));
|
));
|
||||||
|
|
||||||
let body = pydocstyle::helpers::raw_contents(contents);
|
let body = ast::strings::raw_contents(contents);
|
||||||
let docstring = Docstring {
|
let docstring = Docstring {
|
||||||
kind: definition.kind,
|
kind: definition.kind,
|
||||||
expr,
|
expr,
|
||||||
|
|
|
@ -1,49 +1,10 @@
|
||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
|
|
||||||
use ruff_python_stdlib::str::{
|
|
||||||
SINGLE_QUOTE_PREFIXES, SINGLE_QUOTE_SUFFIXES, TRIPLE_QUOTE_PREFIXES, TRIPLE_QUOTE_SUFFIXES,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::ast::cast;
|
use crate::ast::cast;
|
||||||
use crate::ast::helpers::{map_callable, to_call_path};
|
use crate::ast::helpers::{map_callable, to_call_path};
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::definition::{Definition, DefinitionKind};
|
use crate::docstrings::definition::{Definition, DefinitionKind};
|
||||||
|
|
||||||
/// Strip the leading and trailing quotes from a docstring.
|
|
||||||
pub fn raw_contents(contents: &str) -> &str {
|
|
||||||
for pattern in TRIPLE_QUOTE_PREFIXES {
|
|
||||||
if contents.starts_with(pattern) {
|
|
||||||
return &contents[pattern.len()..contents.len() - 3];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for pattern in SINGLE_QUOTE_PREFIXES {
|
|
||||||
if contents.starts_with(pattern) {
|
|
||||||
return &contents[pattern.len()..contents.len() - 1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unreachable!("Expected docstring to start with a valid triple- or single-quote prefix")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the leading quote string for a docstring (e.g., `"""`).
|
|
||||||
pub fn leading_quote(content: &str) -> Option<&str> {
|
|
||||||
if let Some(first_line) = content.lines().next() {
|
|
||||||
for pattern in TRIPLE_QUOTE_PREFIXES.iter().chain(SINGLE_QUOTE_PREFIXES) {
|
|
||||||
if first_line.starts_with(pattern) {
|
|
||||||
return Some(pattern);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the trailing quote string for a docstring (e.g., `"""`).
|
|
||||||
pub fn trailing_quote(content: &str) -> Option<&&str> {
|
|
||||||
TRIPLE_QUOTE_SUFFIXES
|
|
||||||
.iter()
|
|
||||||
.chain(SINGLE_QUOTE_SUFFIXES)
|
|
||||||
.find(|&pattern| content.ends_with(pattern))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the index of the first logical line in a string.
|
/// Return the index of the first logical line in a string.
|
||||||
pub fn logical_line(content: &str) -> Option<usize> {
|
pub fn logical_line(content: &str) -> Option<usize> {
|
||||||
// Find the first logical line.
|
// Find the first logical line.
|
||||||
|
|
|
@ -2,6 +2,7 @@ use strum::IntoEnumIterator;
|
||||||
|
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
||||||
|
use crate::ast::strings::leading_quote;
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::definition::Docstring;
|
use crate::docstrings::definition::Docstring;
|
||||||
|
@ -9,7 +10,7 @@ use crate::docstrings::sections::SectionKind;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
use crate::message::Location;
|
use crate::message::Location;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::rules::pydocstyle::helpers::{leading_quote, logical_line};
|
use crate::rules::pydocstyle::helpers::logical_line;
|
||||||
use crate::violation::AlwaysAutofixableViolation;
|
use crate::violation::AlwaysAutofixableViolation;
|
||||||
|
|
||||||
#[violation]
|
#[violation]
|
||||||
|
|
|
@ -2,6 +2,7 @@ use strum::IntoEnumIterator;
|
||||||
|
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
||||||
|
use crate::ast::strings::leading_quote;
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::definition::Docstring;
|
use crate::docstrings::definition::Docstring;
|
||||||
|
@ -9,7 +10,7 @@ use crate::docstrings::sections::SectionKind;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
use crate::message::Location;
|
use crate::message::Location;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::rules::pydocstyle::helpers::{leading_quote, logical_line};
|
use crate::rules::pydocstyle::helpers::logical_line;
|
||||||
use crate::violation::AlwaysAutofixableViolation;
|
use crate::violation::AlwaysAutofixableViolation;
|
||||||
|
|
||||||
#[violation]
|
#[violation]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_stdlib::str::TRIPLE_QUOTE_PREFIXES;
|
use ruff_python_stdlib::str::TRIPLE_QUOTE_PREFIXES;
|
||||||
|
|
||||||
|
use crate::ast::strings::leading_quote;
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::ast::whitespace::LinesWithTrailingNewline;
|
use crate::ast::whitespace::LinesWithTrailingNewline;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
@ -8,7 +9,6 @@ use crate::docstrings::definition::{DefinitionKind, Docstring};
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
use crate::message::Location;
|
use crate::message::Location;
|
||||||
use crate::registry::{Diagnostic, Rule};
|
use crate::registry::{Diagnostic, Rule};
|
||||||
use crate::rules::pydocstyle::helpers::leading_quote;
|
|
||||||
use crate::violation::AlwaysAutofixableViolation;
|
use crate::violation::AlwaysAutofixableViolation;
|
||||||
|
|
||||||
#[violation]
|
#[violation]
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
||||||
|
use crate::ast::strings::leading_quote;
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::ast::whitespace::LinesWithTrailingNewline;
|
use crate::ast::whitespace::LinesWithTrailingNewline;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
@ -7,7 +8,6 @@ use crate::docstrings::definition::Docstring;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
use crate::message::Location;
|
use crate::message::Location;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::rules::pydocstyle::helpers::leading_quote;
|
|
||||||
use crate::violation::AlwaysAutofixableViolation;
|
use crate::violation::AlwaysAutofixableViolation;
|
||||||
|
|
||||||
#[violation]
|
#[violation]
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
||||||
|
use crate::ast::strings::{leading_quote, trailing_quote};
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::ast::whitespace::LinesWithTrailingNewline;
|
use crate::ast::whitespace::LinesWithTrailingNewline;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::definition::Docstring;
|
use crate::docstrings::definition::Docstring;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::rules::pydocstyle::helpers;
|
|
||||||
use crate::violation::AlwaysAutofixableViolation;
|
use crate::violation::AlwaysAutofixableViolation;
|
||||||
|
|
||||||
#[violation]
|
#[violation]
|
||||||
|
@ -41,8 +41,8 @@ pub fn one_liner(checker: &mut Checker, docstring: &Docstring) {
|
||||||
let mut diagnostic = Diagnostic::new(FitsOnOneLine, Range::from_located(docstring.expr));
|
let mut diagnostic = Diagnostic::new(FitsOnOneLine, Range::from_located(docstring.expr));
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
if let (Some(leading), Some(trailing)) = (
|
if let (Some(leading), Some(trailing)) = (
|
||||||
helpers::leading_quote(docstring.contents),
|
leading_quote(docstring.contents),
|
||||||
helpers::trailing_quote(docstring.contents),
|
trailing_quote(docstring.contents),
|
||||||
) {
|
) {
|
||||||
// If removing whitespace would lead to an invalid string of quote
|
// If removing whitespace would lead to an invalid string of quote
|
||||||
// characters, avoid applying the fix.
|
// characters, avoid applying the fix.
|
||||||
|
|
|
@ -6,10 +6,10 @@ use rustpython_common::cformat::{CFormatPart, CFormatSpec, CFormatStrOrBytes, CF
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Location, Operator};
|
use rustpython_parser::ast::{Constant, Expr, ExprKind, Location, Operator};
|
||||||
use rustpython_parser::{lexer, Mode, Tok};
|
use rustpython_parser::{lexer, Mode, Tok};
|
||||||
|
|
||||||
|
use crate::ast::strings::{leading_quote, trailing_quote};
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::rules::pydocstyle::helpers::{leading_quote, trailing_quote};
|
|
||||||
use crate::violation::Violation;
|
use crate::violation::Violation;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
|
|
|
@ -6,11 +6,11 @@ use rustpython_common::format::{
|
||||||
use rustpython_parser::ast::{Constant, Expr, ExprKind, KeywordData};
|
use rustpython_parser::ast::{Constant, Expr, ExprKind, KeywordData};
|
||||||
use rustpython_parser::{lexer, Mode, Tok};
|
use rustpython_parser::{lexer, Mode, Tok};
|
||||||
|
|
||||||
|
use crate::ast::strings::{leading_quote, trailing_quote};
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::rules::pydocstyle::helpers::{leading_quote, trailing_quote};
|
|
||||||
use crate::rules::pyflakes::format::FormatSummary;
|
use crate::rules::pyflakes::format::FormatSummary;
|
||||||
use crate::rules::pyupgrade::helpers::curly_escape;
|
use crate::rules::pyupgrade::helpers::curly_escape;
|
||||||
use crate::violation::AlwaysAutofixableViolation;
|
use crate::violation::AlwaysAutofixableViolation;
|
||||||
|
|
|
@ -10,12 +10,12 @@ use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_stdlib::identifiers::is_identifier;
|
use ruff_python_stdlib::identifiers::is_identifier;
|
||||||
use ruff_python_stdlib::keyword::KWLIST;
|
use ruff_python_stdlib::keyword::KWLIST;
|
||||||
|
|
||||||
|
use crate::ast::strings::{leading_quote, trailing_quote};
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::ast::whitespace::indentation;
|
use crate::ast::whitespace::indentation;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::rules::pydocstyle::helpers::{leading_quote, trailing_quote};
|
|
||||||
use crate::rules::pyupgrade::helpers::curly_escape;
|
use crate::rules::pyupgrade::helpers::curly_escape;
|
||||||
use crate::violation::AlwaysAutofixableViolation;
|
use crate::violation::AlwaysAutofixableViolation;
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,8 @@ use ruff_rustpython::vendor;
|
||||||
use rustpython_parser::ast::Location;
|
use rustpython_parser::ast::Location;
|
||||||
use rustpython_parser::{lexer, Mode, Tok};
|
use rustpython_parser::{lexer, Mode, Tok};
|
||||||
|
|
||||||
|
use crate::ast::strings::leading_quote;
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::rules::pydocstyle::helpers::leading_quote;
|
|
||||||
use crate::source_code::Locator;
|
use crate::source_code::Locator;
|
||||||
|
|
||||||
pub struct Stylist<'a> {
|
pub struct Stylist<'a> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue