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

Add lint.preview #8002

Merged
merged 1 commit into from
Oct 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ pub struct FormatCommand {
#[arg(long, help_heading = "Miscellaneous")]
pub stdin_filename: Option<PathBuf>,

/// Enable preview mode; checks will include unstable rules and fixes.
/// Enable preview mode; enables unstable formatting.
MichaReiser marked this conversation as resolved.
Show resolved Hide resolved
/// Use `--no-preview` to disable.
#[arg(long, overrides_with("no_preview"), hide = true)]
#[arg(long, overrides_with("no_preview"))]
preview: bool,
#[clap(long, overrides_with("preview"), hide = true)]
MichaReiser marked this conversation as resolved.
Show resolved Hide resolved
no_preview: bool,
Expand Down Expand Up @@ -668,6 +668,8 @@ impl ConfigurationTransformer for CliOverrides {
}
if let Some(preview) = &self.preview {
config.preview = Some(*preview);
config.lint.preview = Some(*preview);
config.format.preview = Some(*preview);
}
if let Some(per_file_ignores) = &self.per_file_ignores {
config.lint.per_file_ignores = Some(collect_per_file_ignores(per_file_ignores.clone()));
Expand Down
16 changes: 10 additions & 6 deletions crates/ruff_workspace/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ impl Configuration {
}

let target_version = self.target_version.unwrap_or_default();
let preview = self.preview.unwrap_or_default();
let global_preview = self.preview.unwrap_or_default();

let format = self.format;
let format_defaults = FormatterSettings::default();
// TODO(micha): Support changing the tab-width but disallow changing the number of spaces
let formatter = FormatterSettings {
exclude: FilePatternSet::try_from_iter(format.exclude.unwrap_or_default())?,
preview: match format.preview.unwrap_or(preview) {
preview: match format.preview.unwrap_or(global_preview) {
PreviewMode::Disabled => ruff_python_formatter::PreviewMode::Disabled,
PreviewMode::Enabled => ruff_python_formatter::PreviewMode::Enabled,
},
Expand All @@ -176,6 +176,7 @@ impl Configuration {
};

let lint = self.lint;
let lint_preview = lint.preview.unwrap_or(global_preview);

Ok(Settings {
cache_dir: self
Expand Down Expand Up @@ -204,8 +205,9 @@ impl Configuration {
},

linter: LinterSettings {
rules: lint.as_rule_table(preview),
rules: lint.as_rule_table(lint_preview),
exclude: FilePatternSet::try_from_iter(lint.exclude.unwrap_or_default())?,
preview: lint_preview,
target_version,
project_root: project_root.to_path_buf(),
allowed_confusables: lint
Expand Down Expand Up @@ -234,7 +236,7 @@ impl Configuration {
.iter()
.flat_map(|selector| {
selector.rules(&PreviewOptions {
mode: preview,
mode: lint_preview,
require_explicit: false,
})
})
Expand All @@ -244,7 +246,7 @@ impl Configuration {
.iter()
.flat_map(|selector| {
selector.rules(&PreviewOptions {
mode: preview,
mode: lint_preview,
require_explicit: false,
})
})
Expand All @@ -257,7 +259,6 @@ impl Configuration {
.task_tags
.unwrap_or_else(|| TASK_TAGS.iter().map(ToString::to_string).collect()),
logger_objects: lint.logger_objects.unwrap_or_default(),
preview,
typing_modules: lint.typing_modules.unwrap_or_default(),
// Plugins
flake8_annotations: lint
Expand Down Expand Up @@ -511,6 +512,7 @@ impl Configuration {
#[derive(Debug, Default)]
pub struct LintConfiguration {
pub exclude: Option<Vec<FilePattern>>,
pub preview: Option<PreviewMode>,

// Rule selection
pub extend_per_file_ignores: Vec<PerFileIgnore>,
Expand Down Expand Up @@ -570,6 +572,7 @@ impl LintConfiguration {
})
.collect()
}),
preview: options.preview.map(PreviewMode::from),

rule_selections: vec![RuleSelection {
select: options.common.select,
Expand Down Expand Up @@ -887,6 +890,7 @@ impl LintConfiguration {
pub fn combine(self, config: Self) -> Self {
Self {
exclude: self.exclude.or(config.exclude),
preview: self.preview.or(config.preview),
rule_selections: config
.rule_selections
.into_iter()
Expand Down
14 changes: 13 additions & 1 deletion crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub struct Options {
pub required_version: Option<Version>,

/// Whether to enable preview mode. When preview mode is enabled, Ruff will
/// use unstable rules and fixes.
/// use unstable rules, fixes, and formatting.
#[option(
default = "false",
value_type = "bool",
Expand Down Expand Up @@ -412,6 +412,18 @@ pub struct LintOptions {
"#
)]
pub exclude: Option<Vec<String>>,

/// Whether to enable preview mode. When preview mode is enabled, Ruff will
/// use unstable rules and fixes.
#[option(
default = "false",
value_type = "bool",
example = r#"
# Enable preview features.
preview = true
"#
)]
pub preview: Option<bool>,
}

// Note: This struct should be inlined into [`LintOptions`] once support for the top-level lint settings
Expand Down
9 changes: 8 additions & 1 deletion ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.