Skip to content

Commit

Permalink
Add lint.preview option
Browse files Browse the repository at this point in the history
Add a new `lint.preview` option that configures the preview mode for the linter only.
  • Loading branch information
MichaReiser committed Oct 18, 2023
1 parent 499200f commit 0817df9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
8 changes: 5 additions & 3 deletions crates/ruff_cli/src/args.rs
Expand Up @@ -395,11 +395,11 @@ 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.
/// 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)]
#[clap(long, overrides_with("preview"))]
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
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
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.

0 comments on commit 0817df9

Please sign in to comment.