Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove source_code's dependency on pydocstyle #3366

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/ruff/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod helpers;
pub mod logging;
pub mod operations;
pub mod relocate;
pub mod strings;
pub mod types;
pub mod typing;
pub mod visitor;
Expand Down
38 changes: 38 additions & 0 deletions crates/ruff/src/ast/strings.rs
Original file line number Diff line number Diff line change
@@ -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))
}
4 changes: 2 additions & 2 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::settings::types::PythonVersion;
use crate::settings::{flags, Settings};
use crate::source_code::{Indexer, Locator, Stylist};
use crate::visibility::transition_scope;
use crate::{autofix, docstrings, noqa, visibility};
use crate::{ast, autofix, docstrings, noqa, visibility};

mod deferred;

Expand Down Expand Up @@ -5298,7 +5298,7 @@ impl<'a> Checker<'a> {
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 {
kind: definition.kind,
expr,
Expand Down
39 changes: 0 additions & 39 deletions crates/ruff/src/rules/pydocstyle/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,10 @@
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::helpers::{map_callable, to_call_path};
use crate::checkers::ast::Checker;
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.
pub fn logical_line(content: &str) -> Option<usize> {
// Find the first logical line.
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff/src/rules/pydocstyle/rules/ends_with_period.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use strum::IntoEnumIterator;

use ruff_macros::{derive_message_formats, violation};

use crate::ast::strings::leading_quote;
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::docstrings::definition::Docstring;
use crate::docstrings::sections::SectionKind;
use crate::fix::Fix;
use crate::message::Location;
use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers::{leading_quote, logical_line};
use crate::rules::pydocstyle::helpers::logical_line;
use crate::violation::AlwaysAutofixableViolation;

#[violation]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use strum::IntoEnumIterator;

use ruff_macros::{derive_message_formats, violation};

use crate::ast::strings::leading_quote;
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::docstrings::definition::Docstring;
use crate::docstrings::sections::SectionKind;
use crate::fix::Fix;
use crate::message::Location;
use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers::{leading_quote, logical_line};
use crate::rules::pydocstyle::helpers::logical_line;
use crate::violation::AlwaysAutofixableViolation;

#[violation]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use ruff_macros::{derive_message_formats, violation};
use ruff_python_stdlib::str::TRIPLE_QUOTE_PREFIXES;

use crate::ast::strings::leading_quote;
use crate::ast::types::Range;
use crate::ast::whitespace::LinesWithTrailingNewline;
use crate::checkers::ast::Checker;
use crate::docstrings::definition::{DefinitionKind, Docstring};
use crate::fix::Fix;
use crate::message::Location;
use crate::registry::{Diagnostic, Rule};
use crate::rules::pydocstyle::helpers::leading_quote;
use crate::violation::AlwaysAutofixableViolation;

#[violation]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use ruff_macros::{derive_message_formats, violation};

use crate::ast::strings::leading_quote;
use crate::ast::types::Range;
use crate::ast::whitespace::LinesWithTrailingNewline;
use crate::checkers::ast::Checker;
use crate::docstrings::definition::Docstring;
use crate::fix::Fix;
use crate::message::Location;
use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers::leading_quote;
use crate::violation::AlwaysAutofixableViolation;

#[violation]
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/pydocstyle/rules/one_liner.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use ruff_macros::{derive_message_formats, violation};

use crate::ast::strings::{leading_quote, trailing_quote};
use crate::ast::types::Range;
use crate::ast::whitespace::LinesWithTrailingNewline;
use crate::checkers::ast::Checker;
use crate::docstrings::definition::Docstring;
use crate::fix::Fix;
use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers;
use crate::violation::AlwaysAutofixableViolation;

#[violation]
Expand Down Expand Up @@ -41,8 +41,8 @@ pub fn one_liner(checker: &mut Checker, docstring: &Docstring) {
let mut diagnostic = Diagnostic::new(FitsOnOneLine, Range::from_located(docstring.expr));
if checker.patch(diagnostic.kind.rule()) {
if let (Some(leading), Some(trailing)) = (
helpers::leading_quote(docstring.contents),
helpers::trailing_quote(docstring.contents),
leading_quote(docstring.contents),
trailing_quote(docstring.contents),
) {
// If removing whitespace would lead to an invalid string of quote
// characters, avoid applying the fix.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use rustpython_common::cformat::{CFormatPart, CFormatSpec, CFormatStrOrBytes, CF
use rustpython_parser::ast::{Constant, Expr, ExprKind, Location, Operator};
use rustpython_parser::{lexer, Mode, Tok};

use crate::ast::strings::{leading_quote, trailing_quote};
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers::{leading_quote, trailing_quote};
use crate::violation::Violation;

/// ## What it does
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pyupgrade/rules/f_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use rustpython_common::format::{
use rustpython_parser::ast::{Constant, Expr, ExprKind, KeywordData};
use rustpython_parser::{lexer, Mode, Tok};

use crate::ast::strings::{leading_quote, trailing_quote};
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::fix::Fix;
use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers::{leading_quote, trailing_quote};
use crate::rules::pyflakes::format::FormatSummary;
use crate::rules::pyupgrade::helpers::curly_escape;
use crate::violation::AlwaysAutofixableViolation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use ruff_macros::{derive_message_formats, violation};
use ruff_python_stdlib::identifiers::is_identifier;
use ruff_python_stdlib::keyword::KWLIST;

use crate::ast::strings::{leading_quote, trailing_quote};
use crate::ast::types::Range;
use crate::ast::whitespace::indentation;
use crate::checkers::ast::Checker;
use crate::fix::Fix;
use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers::{leading_quote, trailing_quote};
use crate::rules::pyupgrade::helpers::curly_escape;
use crate::violation::AlwaysAutofixableViolation;

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/source_code/stylist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use ruff_rustpython::vendor;
use rustpython_parser::ast::Location;
use rustpython_parser::{lexer, Mode, Tok};

use crate::ast::strings::leading_quote;
use crate::ast::types::Range;
use crate::rules::pydocstyle::helpers::leading_quote;
use crate::source_code::Locator;

pub struct Stylist<'a> {
Expand Down