Refactor SourceKind to store file content (#6640)

This commit is contained in:
Micha Reiser 2023-08-18 15:45:38 +02:00 committed by GitHub
parent 2aeb27334d
commit ea72d5feba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 206 additions and 179 deletions

View file

@ -445,9 +445,9 @@ mod tests {
}
// Not stored in the cache.
expected_diagnostics.source_kind.clear();
got_diagnostics.source_kind.clear();
assert!(expected_diagnostics == got_diagnostics);
expected_diagnostics.notebooks.clear();
got_diagnostics.notebooks.clear();
assert_eq!(expected_diagnostics, got_diagnostics);
}
#[test]

View file

@ -37,7 +37,7 @@ pub(crate) fn run_stdin(
let mut diagnostics = lint_stdin(
filename,
package_root,
&stdin,
stdin,
&pyproject_config.settings.lib,
noqa,
autofix,

View file

@ -1,23 +1,20 @@
#![cfg_attr(target_family = "wasm", allow(dead_code))]
use std::borrow::Cow;
use std::fs::write;
use std::io;
use std::io::Write;
use std::ops::AddAssign;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use anyhow::{anyhow, Context, Result};
use colored::Colorize;
use filetime::FileTime;
use log::{debug, error, warn};
use ruff_text_size::{TextRange, TextSize};
use rustc_hash::FxHashMap;
use similar::TextDiff;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use crate::cache::Cache;
use ruff::jupyter::{Cell, Notebook};
use ruff::linter::{lint_fix, lint_only, FixTable, FixerResult, LinterResult};
use ruff::logging::DisplayParseError;
@ -33,6 +30,9 @@ use ruff_python_ast::imports::ImportMap;
use ruff_python_ast::PySourceType;
use ruff_python_stdlib::path::is_project_toml;
use ruff_source_file::{LineIndex, SourceCode, SourceFileBuilder};
use ruff_text_size::{TextRange, TextSize};
use crate::cache::Cache;
#[derive(CacheKey)]
pub(crate) struct FileCacheKey {
@ -64,7 +64,7 @@ pub(crate) struct Diagnostics {
pub(crate) messages: Vec<Message>,
pub(crate) fixed: FxHashMap<String, FixTable>,
pub(crate) imports: ImportMap,
pub(crate) source_kind: FxHashMap<String, SourceKind>,
pub(crate) notebooks: FxHashMap<String, Notebook>,
}
impl Diagnostics {
@ -73,7 +73,7 @@ impl Diagnostics {
messages,
fixed: FxHashMap::default(),
imports,
source_kind: FxHashMap::default(),
notebooks: FxHashMap::default(),
}
}
@ -118,7 +118,7 @@ impl AddAssign for Diagnostics {
}
}
}
self.source_kind.extend(other.source_kind);
self.notebooks.extend(other.notebooks);
}
}
@ -256,8 +256,7 @@ pub(crate) fn lint_path(
// Extract the sources from the file.
let LintSources {
source_type,
mut source_kind,
contents,
source_kind,
} = match LintSources::try_from_path(path) {
Ok(sources) => sources,
Err(SourceExtractionError::Io(err)) => {
@ -281,18 +280,17 @@ pub(crate) fn lint_path(
transformed,
fixed,
}) = lint_fix(
&contents,
path,
package,
noqa,
&settings.lib,
&mut source_kind,
&source_kind,
source_type,
) {
if !fixed.is_empty() {
match autofix {
flags::FixMode::Apply => match &source_kind {
SourceKind::Python => {
flags::FixMode::Apply => match transformed.as_ref() {
SourceKind::Python(transformed) => {
write(path, transformed.as_bytes())?;
}
SourceKind::Jupyter(notebook) => {
@ -300,10 +298,10 @@ pub(crate) fn lint_path(
}
},
flags::FixMode::Diff => {
match &source_kind {
SourceKind::Python => {
match transformed.as_ref() {
SourceKind::Python(transformed) => {
let mut stdout = io::stdout().lock();
TextDiff::from_lines(&contents, &transformed)
TextDiff::from_lines(source_kind.source_code(), transformed)
.unified_diff()
.header(&fs::relativize_path(path), &fs::relativize_path(path))
.to_writer(&mut stdout)?;
@ -313,10 +311,7 @@ pub(crate) fn lint_path(
SourceKind::Jupyter(dest_notebook) => {
// We need to load the notebook again, since we might've
// mutated it.
let src_notebook = match notebook_from_path(path) {
Ok(notebook) => notebook,
Err(diagnostic) => return Ok(*diagnostic),
};
let src_notebook = source_kind.as_jupyter().unwrap();
let mut stdout = io::stdout().lock();
for ((idx, src_cell), dest_cell) in src_notebook
.cells()
@ -368,12 +363,11 @@ pub(crate) fn lint_path(
} else {
// If we fail to autofix, lint the original source code.
let result = lint_only(
&contents,
path,
package,
&settings.lib,
noqa,
Some(&source_kind),
&source_kind,
source_type,
);
let fixed = FxHashMap::default();
@ -381,12 +375,11 @@ pub(crate) fn lint_path(
}
} else {
let result = lint_only(
&contents,
path,
package,
&settings.lib,
noqa,
Some(&source_kind),
&source_kind,
source_type,
);
let fixed = FxHashMap::default();
@ -407,22 +400,31 @@ pub(crate) fn lint_path(
"{}",
DisplayParseError::new(
err,
SourceCode::new(&contents, &LineIndex::from_source_text(&contents)),
SourceCode::new(
source_kind.source_code(),
&LineIndex::from_source_text(source_kind.source_code())
),
Some(&source_kind),
)
);
}
let notebooks = if let SourceKind::Jupyter(notebook) = source_kind {
FxHashMap::from_iter([(
path.to_str()
.ok_or_else(|| anyhow!("Unable to parse filename: {:?}", path))?
.to_string(),
notebook,
)])
} else {
FxHashMap::default()
};
Ok(Diagnostics {
messages,
fixed: FxHashMap::from_iter([(fs::relativize_path(path), fixed)]),
imports,
source_kind: FxHashMap::from_iter([(
path.to_str()
.ok_or_else(|| anyhow!("Unable to parse filename: {:?}", path))?
.to_string(),
source_kind,
)]),
notebooks,
})
}
@ -431,7 +433,7 @@ pub(crate) fn lint_path(
pub(crate) fn lint_stdin(
path: Option<&Path>,
package: Option<&Path>,
contents: &str,
contents: String,
settings: &Settings,
noqa: flags::Noqa,
autofix: flags::FixMode,
@ -439,8 +441,7 @@ pub(crate) fn lint_stdin(
// Extract the sources from the file.
let LintSources {
source_type,
mut source_kind,
contents,
source_kind,
} = match LintSources::try_from_source_code(contents, path) {
Ok(sources) => sources,
Err(SourceExtractionError::Io(err)) => {
@ -465,23 +466,25 @@ pub(crate) fn lint_stdin(
transformed,
fixed,
}) = lint_fix(
&contents,
path.unwrap_or_else(|| Path::new("-")),
package,
noqa,
settings,
&mut source_kind,
&source_kind,
source_type,
) {
match autofix {
flags::FixMode::Apply => {
// Write the contents to stdout, regardless of whether any errors were fixed.
io::stdout().write_all(transformed.as_bytes())?;
io::stdout().write_all(transformed.source_code().as_bytes())?;
}
flags::FixMode::Diff => {
// But only write a diff if it's non-empty.
if !fixed.is_empty() {
let text_diff = TextDiff::from_lines(&contents, &transformed);
let text_diff = TextDiff::from_lines(
source_kind.source_code(),
transformed.source_code(),
);
let mut unified_diff = text_diff.unified_diff();
if let Some(path) = path {
unified_diff
@ -501,31 +504,29 @@ pub(crate) fn lint_stdin(
} else {
// If we fail to autofix, lint the original source code.
let result = lint_only(
&contents,
path.unwrap_or_else(|| Path::new("-")),
package,
settings,
noqa,
Some(&source_kind),
&source_kind,
source_type,
);
let fixed = FxHashMap::default();
// Write the contents to stdout anyway.
if autofix.is_apply() {
io::stdout().write_all(contents.as_bytes())?;
io::stdout().write_all(source_kind.source_code().as_bytes())?;
}
(result, fixed)
}
} else {
let result = lint_only(
&contents,
path.unwrap_or_else(|| Path::new("-")),
package,
settings,
noqa,
Some(&source_kind),
&source_kind,
source_type,
);
let fixed = FxHashMap::default();
@ -548,21 +549,19 @@ pub(crate) fn lint_stdin(
fixed,
)]),
imports,
source_kind: FxHashMap::default(),
notebooks: FxHashMap::default(),
})
}
#[derive(Debug)]
struct LintSources<'a> {
struct LintSources {
/// The "type" of source code, e.g. `.py`, `.pyi`, `.ipynb`, etc.
source_type: PySourceType,
/// The "kind" of source, e.g. Python file, Jupyter Notebook, etc.
source_kind: SourceKind,
/// The contents of the source code.
contents: Cow<'a, str>,
}
impl<'a> LintSources<'a> {
impl LintSources {
/// Extract the lint [`LintSources`] from the given file path.
fn try_from_path(path: &Path) -> Result<LintSources, SourceExtractionError> {
let source_type = PySourceType::from(path);
@ -570,20 +569,17 @@ impl<'a> LintSources<'a> {
// Read the file from disk.
if source_type.is_jupyter() {
let notebook = notebook_from_path(path).map_err(SourceExtractionError::Diagnostics)?;
let contents = notebook.source_code().to_string();
let source_kind = SourceKind::Jupyter(notebook);
Ok(LintSources {
source_type,
source_kind,
contents: Cow::Owned(contents),
})
} else {
// This is tested by ruff_cli integration test `unreadable_file`
let contents = std::fs::read_to_string(path).map_err(SourceExtractionError::Io)?;
Ok(LintSources {
source_type,
source_kind: SourceKind::Python,
contents: Cow::Owned(contents),
source_kind: SourceKind::Python(contents),
})
}
}
@ -592,26 +588,23 @@ impl<'a> LintSources<'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(
source_code: &'a str,
source_code: String,
path: Option<&Path>,
) -> Result<LintSources<'a>, SourceExtractionError> {
) -> Result<LintSources, SourceExtractionError> {
let source_type = path.map(PySourceType::from).unwrap_or_default();
if source_type.is_jupyter() {
let notebook = notebook_from_source_code(source_code, path)
let notebook = notebook_from_source_code(&source_code, path)
.map_err(SourceExtractionError::Diagnostics)?;
let contents = notebook.source_code().to_string();
let source_kind = SourceKind::Jupyter(notebook);
Ok(LintSources {
source_type,
source_kind,
contents: Cow::Owned(contents),
})
} else {
Ok(LintSources {
source_type,
source_kind: SourceKind::Python,
contents: Cow::Borrowed(source_code),
source_kind: SourceKind::Python(source_code),
})
}
}

View file

@ -177,7 +177,7 @@ impl Printer {
return Ok(());
}
let context = EmitterContext::new(&diagnostics.source_kind);
let context = EmitterContext::new(&diagnostics.notebooks);
match self.format {
SerializationFormat::Json => {
@ -364,7 +364,7 @@ impl Printer {
writeln!(writer)?;
}
let context = EmitterContext::new(&diagnostics.source_kind);
let context = EmitterContext::new(&diagnostics.notebooks);
TextEmitter::default()
.with_show_fix_status(show_fix_status(self.autofix_level))
.with_show_source(self.flags.intersects(Flags::SHOW_SOURCE))