mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-03 05:03:33 +00:00
## Stack Summary This stack splits `Settings` into `FormatterSettings` and `LinterSettings` and moves it into `ruff_workspace`. This change is necessary to add the `FormatterSettings` to `Settings` without adding `ruff_python_formatter` as a dependency to `ruff_linter` (and the linter should not contain the formatter settings). A quick overview of our settings struct at play: * `Options`: 1:1 representation of the options in the `pyproject.toml` or `ruff.toml`. Used for deserialization. * `Configuration`: Resolved `Options`, potentially merged from multiple configurations (when using `extend`). The representation is very close if not identical to the `Options`. * `Settings`: The resolved configuration that uses a data format optimized for reading. Optional fields are initialized with their default values. Initialized by `Configuration::into_settings` . The goal of this stack is to split `Settings` into tool-specific resolved `Settings` that are independent of each other. This comes at the advantage that the individual crates don't need to know anything about the other tools. The downside is that information gets duplicated between `Settings`. Right now the duplication is minimal (`line-length`, `tab-width`) but we may need to come up with a solution if more expensive data needs sharing. This stack focuses on `Settings`. Splitting `Configuration` into some smaller structs is something I'll follow up on later. ## PR Summary This PR extracts the linter-specific settings into a new `LinterSettings` struct and adds it as a `linter` field to the `Settings` struct. This is in preparation for moving `Settings` from `ruff_linter` to `ruff_workspace` ## Test Plan `cargo test`
78 lines
2.7 KiB
Rust
78 lines
2.7 KiB
Rust
use colored::Colorize;
|
|
use log::warn;
|
|
use pyproject_toml::{BuildSystem, Project};
|
|
use ruff_text_size::{TextRange, TextSize};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use ruff_diagnostics::Diagnostic;
|
|
use ruff_source_file::SourceFile;
|
|
|
|
use crate::message::Message;
|
|
use crate::registry::Rule;
|
|
use crate::rules::ruff::rules::InvalidPyprojectToml;
|
|
use crate::settings::LinterSettings;
|
|
use crate::IOError;
|
|
|
|
/// Unlike [`pyproject_toml::PyProjectToml`], in our case `build_system` is also optional
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
|
#[serde(rename_all = "kebab-case")]
|
|
struct PyProjectToml {
|
|
/// Build-related data
|
|
build_system: Option<BuildSystem>,
|
|
/// Project metadata
|
|
project: Option<Project>,
|
|
}
|
|
|
|
pub fn lint_pyproject_toml(source_file: SourceFile, settings: &LinterSettings) -> Vec<Message> {
|
|
let Some(err) = toml::from_str::<PyProjectToml>(source_file.source_text()).err() else {
|
|
return Vec::default();
|
|
};
|
|
|
|
let mut messages = Vec::new();
|
|
let range = match err.span() {
|
|
// This is bad but sometimes toml and/or serde just don't give us spans
|
|
// TODO(konstin,micha): https://github.com/astral-sh/ruff/issues/4571
|
|
None => TextRange::default(),
|
|
Some(range) => {
|
|
let Ok(end) = TextSize::try_from(range.end) else {
|
|
let message = format!(
|
|
"{} is larger than 4GB, but ruff assumes all files to be smaller",
|
|
source_file.name(),
|
|
);
|
|
if settings.rules.enabled(Rule::IOError) {
|
|
let diagnostic = Diagnostic::new(IOError { message }, TextRange::default());
|
|
messages.push(Message::from_diagnostic(
|
|
diagnostic,
|
|
source_file,
|
|
TextSize::default(),
|
|
));
|
|
} else {
|
|
warn!(
|
|
"{}{}{} {message}",
|
|
"Failed to lint ".bold(),
|
|
source_file.name().bold(),
|
|
":".bold()
|
|
);
|
|
}
|
|
return messages;
|
|
};
|
|
TextRange::new(
|
|
// start <= end, so if end < 4GB follows start < 4GB
|
|
TextSize::try_from(range.start).unwrap(),
|
|
end,
|
|
)
|
|
}
|
|
};
|
|
|
|
if settings.rules.enabled(Rule::InvalidPyprojectToml) {
|
|
let toml_err = err.message().to_string();
|
|
let diagnostic = Diagnostic::new(InvalidPyprojectToml { message: toml_err }, range);
|
|
messages.push(Message::from_diagnostic(
|
|
diagnostic,
|
|
source_file,
|
|
TextSize::default(),
|
|
));
|
|
}
|
|
|
|
messages
|
|
}
|