Make SourceKind a required parameter (#7013)

This commit is contained in:
Dhruv Manilawala 2023-09-04 13:15:59 +05:30 committed by GitHub
parent 93ca8ebbc0
commit 1067261a55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 62 additions and 60 deletions

View file

@ -12,6 +12,7 @@ use ruff_python_ast::{PySourceType, SourceType};
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig};
use crate::args::Overrides;
use crate::diagnostics::LintSource;
/// Add `noqa` directives to a collection of files.
pub(crate) fn add_noqa(
@ -56,7 +57,15 @@ pub(crate) fn add_noqa(
.and_then(|parent| package_roots.get(parent))
.and_then(|package| *package);
let settings = resolver.resolve(path, pyproject_config);
match add_noqa_to_path(path, package, source_type, settings) {
let LintSource(source_kind) = match LintSource::try_from_path(path, source_type) {
Ok(Some(source)) => source,
Ok(None) => return None,
Err(e) => {
error!("Failed to extract source from {}: {e}", path.display());
return None;
}
};
match add_noqa_to_path(path, package, &source_kind, source_type, settings) {
Ok(count) => Some(count),
Err(e) => {
error!("Failed to add noqa to {}: {e}", path.display());

View file

@ -354,7 +354,7 @@ pub(crate) fn lint_path(
source_kind.source_code(),
&LineIndex::from_source_text(source_kind.source_code())
),
Some(&source_kind),
&source_kind,
)
);
}
@ -503,11 +503,11 @@ pub(crate) fn lint_stdin(
}
#[derive(Debug)]
struct LintSource(SourceKind);
pub(crate) struct LintSource(pub(crate) SourceKind);
impl LintSource {
/// Extract the lint [`LintSource`] from the given file path.
fn try_from_path(
pub(crate) fn try_from_path(
path: &Path,
source_type: PySourceType,
) -> Result<Option<LintSource>, SourceExtractionError> {
@ -526,7 +526,7 @@ impl LintSource {
/// Extract the lint [`LintSource`] from the raw string contents, optionally accompanied by a
/// file path indicating the path to the file from which the contents were read. If provided,
/// the file path should be used for diagnostics, but not for reading the file from disk.
fn try_from_source_code(
pub(crate) fn try_from_source_code(
source_code: String,
source_type: PySourceType,
) -> Result<Option<LintSource>, SourceExtractionError> {