Skip to content

Commit

Permalink
Per review
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Jan 6, 2025
1 parent d73bb45 commit d6f4210
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
11 changes: 6 additions & 5 deletions crates/ruff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,10 +975,11 @@ The path `{value}` does not point to a configuration file"

match Options::metadata().find(key) {
Some(OptionEntry::Set(set)) if !value.starts_with('{') => {
let prefixed_subkeys = format!("{set}")
.trim_ascii()
.split('\n')
.map(|child| format!("- `{key}.{child}`"))
let prefixed_subfields = set.collect_entries()
.iter()
.filter_map(|(name, entry)| {
entry.is_field().then(|| format!("- `{key}.{name}`"))
})
.join("\n");

tip.push_str(&format!(
Expand All @@ -989,7 +990,7 @@ Did you want to override one of the table's subkeys?
Possible choices:
{prefixed_subkeys}"
{prefixed_subfields}"
));
}
_ => {
Expand Down
53 changes: 52 additions & 1 deletion crates/ruff/tests/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ fn each_toml_option_requires_a_new_flag_2() {
}

#[test]
fn value_given_to_table_key_is_not_inline_table() {
fn value_given_to_table_key_is_not_inline_table_1() {
// https://github.com/astral-sh/ruff/issues/13995
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
Expand Down Expand Up @@ -817,6 +817,57 @@ fn value_given_to_table_key_is_not_inline_table() {
"#);
}

#[test]
fn value_given_to_table_key_is_not_inline_table_2() {
// https://github.com/astral-sh/ruff/issues/13995
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
// spaces *also* can't be used to delimit different config overrides;
// you need a new --config flag for each override
.args([".", "--config", r#"lint=123"#]),
@r"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: invalid value 'lint=123' for '--config <CONFIG_OPTION>'
tip: A `--config` flag must either be a path to a `.toml` configuration file
or a TOML `<KEY> = <VALUE>` pair overriding a specific configuration
option
`lint` is a table of configuration options.
Did you want to override one of the table's subkeys?
Possible choices:
- `lint.allowed-confusables`
- `lint.dummy-variable-rgx`
- `lint.extend-ignore`
- `lint.extend-select`
- `lint.extend-fixable`
- `lint.external`
- `lint.fixable`
- `lint.ignore`
- `lint.extend-safe-fixes`
- `lint.extend-unsafe-fixes`
- `lint.ignore-init-module-imports`
- `lint.logger-objects`
- `lint.select`
- `lint.explicit-preview-rules`
- `lint.task-tags`
- `lint.typing-modules`
- `lint.unfixable`
- `lint.per-file-ignores`
- `lint.extend-per-file-ignores`
- `lint.exclude`
- `lint.preview`
For more information, try '--help'.
");
}

#[test]
fn config_doubly_overridden_via_cli() -> Result<()> {
let tempdir = TempDir::new()?;
Expand Down
20 changes: 19 additions & 1 deletion crates/ruff_workspace/src/options_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
}

/// Metadata of an option that can either be a [`OptionField`] or [`OptionSet`].
#[derive(Clone, PartialEq, Eq, Debug, Serialize)]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, is_macro::Is)]
#[serde(untagged)]
pub enum OptionEntry {
/// A single option.
Expand Down Expand Up @@ -285,6 +285,24 @@ impl OptionSet {
None
}
}

pub fn collect_entries(&self) -> Vec<(String, OptionEntry)> {
struct EntryCollector(pub Vec<(String, OptionEntry)>);

impl Visit for EntryCollector {
fn record_field(&mut self, name: &str, field: OptionField) {
self.0.push((name.to_string(), OptionEntry::Field(field)));
}

fn record_set(&mut self, name: &str, group: OptionSet) {
self.0.push((name.to_string(), OptionEntry::Set(group)));
}
}

let mut visitor = EntryCollector(vec![]);
self.record(&mut visitor);
visitor.0
}
}

/// Visitor that writes out the names of all fields and sets.
Expand Down

0 comments on commit d6f4210

Please sign in to comment.