Reduce visibility of more functions, structs, and fields (#4570)

This commit is contained in:
Charlie Marsh 2023-05-21 23:36:48 -04:00 committed by GitHub
parent 55c4020ba9
commit f73b398776
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 61 additions and 82 deletions

View file

@ -83,11 +83,7 @@ pub fn extract_directives(
}
/// Extract a mapping from logical line to noqa line.
pub fn extract_noqa_line_for(
lxr: &[LexResult],
locator: &Locator,
indexer: &Indexer,
) -> NoqaMapping {
fn extract_noqa_line_for(lxr: &[LexResult], locator: &Locator, indexer: &Indexer) -> NoqaMapping {
let mut string_mappings = Vec::new();
for (tok, range) in lxr.iter().flatten() {
@ -166,7 +162,7 @@ pub fn extract_noqa_line_for(
}
/// Extract a set of ranges over which to disable isort.
pub fn extract_isort_directives(lxr: &[LexResult], locator: &Locator) -> IsortDirectives {
fn extract_isort_directives(lxr: &[LexResult], locator: &Locator) -> IsortDirectives {
let mut exclusions: Vec<TextRange> = Vec::default();
let mut splits: Vec<TextSize> = Vec::default();
let mut off: Option<TextSize> = None;

View file

@ -57,11 +57,6 @@ impl<'a> DocstringBody<'a> {
self.range().start()
}
#[inline]
pub(crate) fn end(self) -> TextSize {
self.range().end()
}
pub(crate) fn range(self) -> TextRange {
self.docstring.body_range + self.docstring.start()
}

View file

@ -11,7 +11,7 @@ use crate::docstrings::styles::SectionStyle;
use crate::docstrings::{Docstring, DocstringBody};
#[derive(EnumIter, PartialEq, Eq, Debug, Clone, Copy)]
pub enum SectionKind {
pub(crate) enum SectionKind {
Args,
Arguments,
Attention,
@ -50,7 +50,7 @@ pub enum SectionKind {
}
impl SectionKind {
pub fn from_str(s: &str) -> Option<Self> {
pub(crate) fn from_str(s: &str) -> Option<Self> {
match s.to_ascii_lowercase().as_str() {
"args" => Some(Self::Args),
"arguments" => Some(Self::Arguments),
@ -91,7 +91,7 @@ impl SectionKind {
}
}
pub fn as_str(self) -> &'static str {
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::Args => "Args",
Self::Arguments => "Arguments",
@ -219,7 +219,7 @@ impl Debug for SectionContexts<'_> {
}
}
pub struct SectionContextsIter<'a> {
pub(crate) struct SectionContextsIter<'a> {
docstring_body: DocstringBody<'a>,
inner: std::slice::Iter<'a, SectionContextData>,
}
@ -268,28 +268,24 @@ struct SectionContextData {
summary_full_end: TextSize,
}
pub struct SectionContext<'a> {
pub(crate) struct SectionContext<'a> {
data: &'a SectionContextData,
docstring_body: DocstringBody<'a>,
}
impl<'a> SectionContext<'a> {
pub fn is_last(&self) -> bool {
self.range().end() == self.docstring_body.end()
}
/// The `kind` of the section, e.g. [`SectionKind::Args`] or [`SectionKind::Returns`].
pub const fn kind(&self) -> SectionKind {
pub(crate) const fn kind(&self) -> SectionKind {
self.data.kind
}
/// The name of the section as it appears in the docstring, e.g. "Args" or "Returns".
pub fn section_name(&self) -> &'a str {
pub(crate) fn section_name(&self) -> &'a str {
&self.docstring_body.as_str()[self.data.name_range]
}
/// Returns the rest of the summary line after the section name.
pub fn summary_after_section_name(&self) -> &'a str {
pub(crate) fn summary_after_section_name(&self) -> &'a str {
&self.summary_line()[usize::from(self.data.name_range.end() - self.data.range.start())..]
}
@ -298,17 +294,12 @@ impl<'a> SectionContext<'a> {
}
/// The absolute range of the section name
pub fn section_name_range(&self) -> TextRange {
pub(crate) fn section_name_range(&self) -> TextRange {
self.data.name_range + self.offset()
}
/// Summary range relative to the start of the document. Includes the trailing newline.
pub fn summary_full_range(&self) -> TextRange {
self.summary_full_range_relative() + self.offset()
}
/// The absolute range of the summary line, excluding any trailing newline character.
pub fn summary_range(&self) -> TextRange {
pub(crate) fn summary_range(&self) -> TextRange {
TextRange::at(self.range().start(), self.summary_line().text_len())
}
@ -323,12 +314,12 @@ impl<'a> SectionContext<'a> {
}
/// The absolute range of the full-section.
pub fn range(&self) -> TextRange {
pub(crate) fn range(&self) -> TextRange {
self.range_relative() + self.offset()
}
/// Summary line without the trailing newline characters
pub fn summary_line(&self) -> &'a str {
pub(crate) fn summary_line(&self) -> &'a str {
let full_summary = &self.docstring_body.as_str()[self.summary_full_range_relative()];
let mut bytes = full_summary.bytes().rev();
@ -349,14 +340,14 @@ impl<'a> SectionContext<'a> {
}
/// Returns the text of the last line of the previous section or an empty string if it is the first section.
pub fn previous_line(&self) -> Option<&'a str> {
pub(crate) fn previous_line(&self) -> Option<&'a str> {
let previous =
&self.docstring_body.as_str()[TextRange::up_to(self.range_relative().start())];
previous.universal_newlines().last().map(|l| l.as_str())
}
/// Returns the lines belonging to this section after the summary line.
pub fn following_lines(&self) -> UniversalNewlineIterator<'a> {
pub(crate) fn following_lines(&self) -> UniversalNewlineIterator<'a> {
let lines = self.following_lines_str();
UniversalNewlineIterator::with_offset(lines, self.offset() + self.data.summary_full_end)
}
@ -371,7 +362,7 @@ impl<'a> SectionContext<'a> {
}
/// Returns the absolute range of the following lines.
pub fn following_range(&self) -> TextRange {
pub(crate) fn following_range(&self) -> TextRange {
self.following_range_relative() + self.offset()
}
}

View file

@ -31,7 +31,7 @@ use crate::checkers::ast::Checker;
use crate::registry::AsRule;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, result_like::BoolLike)]
pub enum Certainty {
enum Certainty {
Certain,
Uncertain,
}
@ -39,15 +39,15 @@ pub enum Certainty {
#[violation]
pub struct UnusedLoopControlVariable {
/// The name of the loop control variable.
pub name: String,
name: String,
/// The name to which the variable should be renamed, if it can be
/// safely renamed.
pub rename: Option<String>,
rename: Option<String>,
/// Whether the variable is certain to be unused in the loop body, or
/// merely suspect. A variable _may_ be used, but undetectably
/// so, if the loop incorporates by magic control flow (e.g.,
/// `locals()`).
pub certainty: Certainty,
certainty: Certainty,
}
impl Violation for UnusedLoopControlVariable {

View file

@ -63,7 +63,7 @@ enum Reason<'a> {
}
#[allow(clippy::too_many_arguments)]
pub fn categorize<'a>(
pub(crate) fn categorize<'a>(
module_name: &str,
level: Option<u32>,
src: &[PathBuf],
@ -220,7 +220,7 @@ pub struct KnownModules {
}
impl KnownModules {
pub fn new(
pub(crate) fn new(
first_party: Vec<String>,
third_party: Vec<String>,
local_folder: Vec<String>,
@ -318,7 +318,7 @@ impl KnownModules {
}
/// Return the list of modules that are known to be of a given type.
pub fn modules_for_known_type(&self, import_type: ImportType) -> Vec<String> {
pub(crate) fn modules_for_known_type(&self, import_type: ImportType) -> Vec<String> {
self.known
.iter()
.filter_map(|(module, known_section)| {
@ -336,7 +336,7 @@ impl KnownModules {
}
/// Return the list of user-defined modules, indexed by section.
pub fn user_defined(&self) -> FxHashMap<String, Vec<String>> {
pub(crate) fn user_defined(&self) -> FxHashMap<String, Vec<String>> {
let mut user_defined: FxHashMap<String, Vec<String>> = FxHashMap::default();
for (module, section) in &self.known {
if let ImportSection::UserDefined(section_name) = section {

View file

@ -6,23 +6,19 @@ use rustpython_parser::{lexer, Mode, Tok};
use ruff_python_ast::source_code::Locator;
#[derive(Debug)]
pub struct Comment<'a> {
pub value: Cow<'a, str>,
pub range: TextRange,
pub(crate) struct Comment<'a> {
pub(crate) value: Cow<'a, str>,
pub(crate) range: TextRange,
}
impl Comment<'_> {
pub const fn start(&self) -> TextSize {
pub(crate) const fn start(&self) -> TextSize {
self.range.start()
}
pub const fn end(&self) -> TextSize {
pub(crate) const fn end(&self) -> TextSize {
self.range.end()
}
pub const fn range(&self) -> TextRange {
self.range
}
}
/// Collect all comments in an import block.

View file

@ -4,8 +4,9 @@ use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use annotate::annotate_imports;
pub(crate) use categorize::categorize;
use categorize::categorize_imports;
pub use categorize::{categorize, ImportSection, ImportType};
pub use categorize::{ImportSection, ImportType};
use comments::Comment;
use normalize::normalize_imports;
use order::order_imports;
@ -35,15 +36,15 @@ pub(crate) mod track;
mod types;
#[derive(Debug)]
pub struct AnnotatedAliasData<'a> {
pub name: &'a str,
pub asname: Option<&'a str>,
pub atop: Vec<Comment<'a>>,
pub inline: Vec<Comment<'a>>,
pub(crate) struct AnnotatedAliasData<'a> {
pub(crate) name: &'a str,
pub(crate) asname: Option<&'a str>,
pub(crate) atop: Vec<Comment<'a>>,
pub(crate) inline: Vec<Comment<'a>>,
}
#[derive(Debug)]
pub enum AnnotatedImport<'a> {
pub(crate) enum AnnotatedImport<'a> {
Import {
names: Vec<AliasData<'a>>,
atop: Vec<Comment<'a>>,
@ -60,7 +61,7 @@ pub enum AnnotatedImport<'a> {
}
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
pub fn format_imports(
pub(crate) fn format_imports(
block: &Block,
comments: Vec<Comment>,
locator: &Locator,

View file

@ -8,17 +8,17 @@ use crate::directives::IsortDirectives;
use crate::rules::isort::helpers;
#[derive(Debug, Copy, Clone)]
pub enum Trailer {
pub(crate) enum Trailer {
Sibling,
ClassDef,
FunctionDef,
}
#[derive(Debug, Default)]
pub struct Block<'a> {
pub nested: bool,
pub imports: Vec<&'a Stmt>,
pub trailer: Option<Trailer>,
pub(crate) struct Block<'a> {
pub(crate) nested: bool,
pub(crate) imports: Vec<&'a Stmt>,
pub(crate) trailer: Option<Trailer>,
}
pub(crate) struct ImportTracker<'a> {

View file

@ -5,7 +5,7 @@ use rustc_hash::FxHashMap;
use ruff_python_ast::helpers::format_import_from;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum TrailingComma {
pub(crate) enum TrailingComma {
Present,
#[default]
Absent,
@ -18,9 +18,9 @@ pub(crate) struct ImportFromData<'a> {
}
#[derive(Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct AliasData<'a> {
pub name: &'a str,
pub asname: Option<&'a str>,
pub(crate) struct AliasData<'a> {
pub(crate) name: &'a str,
pub(crate) asname: Option<&'a str>,
}
#[derive(Debug, Default, Clone)]

View file

@ -11,7 +11,7 @@ use crate::registry::AsRule;
use crate::rules::pycodestyle::helpers::compare;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum EqCmpop {
enum EqCmpop {
Eq,
NotEq,
}
@ -50,7 +50,7 @@ impl From<&Cmpop> for EqCmpop {
/// ## References
/// - [PEP 8](https://peps.python.org/pep-0008/#programming-recommendations)
#[violation]
pub struct NoneComparison(pub EqCmpop);
pub struct NoneComparison(EqCmpop);
impl AlwaysAutofixableViolation for NoneComparison {
#[derive_message_formats]
@ -97,7 +97,7 @@ impl AlwaysAutofixableViolation for NoneComparison {
/// ## References
/// - [PEP 8](https://peps.python.org/pep-0008/#programming-recommendations)
#[violation]
pub struct TrueFalseComparison(pub bool, pub EqCmpop);
pub struct TrueFalseComparison(bool, EqCmpop);
impl AlwaysAutofixableViolation for TrueFalseComparison {
#[derive_message_formats]

View file

@ -10,16 +10,16 @@ use ruff_python_stdlib::future::ALL_FEATURE_NAMES;
use crate::checkers::ast::Checker;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum UnusedImportContext {
pub(crate) enum UnusedImportContext {
ExceptHandler,
Init,
}
#[violation]
pub struct UnusedImport {
pub name: String,
pub context: Option<UnusedImportContext>,
pub multiple: bool,
pub(crate) name: String,
pub(crate) context: Option<UnusedImportContext>,
pub(crate) multiple: bool,
}
impl Violation for UnusedImport {
@ -56,8 +56,8 @@ impl Violation for UnusedImport {
}
#[violation]
pub struct ImportShadowedByLoopVar {
pub name: String,
pub line: OneIndexed,
pub(crate) name: String,
pub(crate) line: OneIndexed,
}
impl Violation for ImportShadowedByLoopVar {
@ -70,7 +70,7 @@ impl Violation for ImportShadowedByLoopVar {
#[violation]
pub struct UndefinedLocalWithImportStar {
pub name: String,
pub(crate) name: String,
}
impl Violation for UndefinedLocalWithImportStar {
@ -93,8 +93,8 @@ impl Violation for LateFutureImport {
#[violation]
pub struct UndefinedLocalWithImportStarUsage {
pub name: String,
pub sources: Vec<String>,
pub(crate) name: String,
pub(crate) sources: Vec<String>,
}
impl Violation for UndefinedLocalWithImportStarUsage {
@ -111,7 +111,7 @@ impl Violation for UndefinedLocalWithImportStarUsage {
#[violation]
pub struct UndefinedLocalWithNestedImportStarUsage {
pub name: String,
pub(crate) name: String,
}
impl Violation for UndefinedLocalWithNestedImportStarUsage {