Skip to content

Commit

Permalink
Add ruff rules-json subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jun 13, 2023
1 parent f9f08d6 commit c782b67
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
2 changes: 2 additions & 0 deletions crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub enum Command {
#[arg(long, value_enum, default_value = "text")]
format: HelpFormat,
},
/// Print a JSON document explaining all of the rules known to Ruff.
RulesJSON,
/// List or describe the available configuration options
Config { option: Option<String> },
/// List all supported upstream linters
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub(crate) mod clean;
pub(crate) mod config;
pub(crate) mod linter;
pub(crate) mod rule;
pub(crate) mod rules_json;
pub(crate) mod run;
pub(crate) mod run_stdin;
pub(crate) mod show_files;
Expand Down
47 changes: 47 additions & 0 deletions crates/ruff_cli/src/commands/rules_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Generate a JSON file describing supported lint rules.
//! NB: the format may change in the future.
use anyhow::Error;
use std::io::{stdout, Write};

use serde::ser::{SerializeMap, Serializer};

use serde_json::json;

use strum::IntoEnumIterator;

use ruff::registry::{Linter, RuleNamespace};
use ruff_diagnostics::AutofixKind;

fn write_rules_json(writer: &mut dyn Write) -> Result<(), Error> {
let mut ser = serde_json::Serializer::pretty(writer);
let mut map = ser.serialize_map(None)?;
for linter in Linter::iter() {
for rule in &linter {
let code = format!(
"{}{}",
linter.common_prefix(),
linter.code_for_rule(rule).unwrap()
);
let fixable = match rule.autofixable() {
AutofixKind::Sometimes => "sometimes",
AutofixKind::Always => "always",
AutofixKind::None => "none",
};
let value = json!({
"name": rule.as_ref(),
"message": rule.message_formats()[0],
"explanation": rule.explanation(),
"fixable": fixable,
"linter": linter.name(),
});
map.serialize_entry(&code, &value)?;
}
}
map.end()?;
Ok(())
}

pub(crate) fn rules_json() -> Result<(), Error> {
write_rules_json(&mut stdout())
}
1 change: 1 addition & 0 deletions crates/ruff_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ quoting the executed command, along with the relevant file contents and `pyproje

match command {
Command::Rule { rule, format } => commands::rule::rule(rule, format)?,
Command::RulesJSON => commands::rules_json::rules_json()?,
Command::Config { option } => return Ok(commands::config::config(option.as_deref())),
Command::Linter { format } => commands::linter::linter(format)?,
Command::Clean => commands::clean::clean(log_level)?,
Expand Down
13 changes: 7 additions & 6 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,13 @@ Ruff: An extremely fast Python linter.
Usage: ruff [OPTIONS] <COMMAND>
Commands:
check Run Ruff on the given files or directories (default)
rule Explain a rule
config List or describe the available configuration options
linter List all supported upstream linters
clean Clear any caches in the current directory and any subdirectories
help Print this message or the help of the given subcommand(s)
check Run Ruff on the given files or directories (default)
rule Explain a rule
rules-json Print a JSON document explaining all of the rules known to Ruff
config List or describe the available configuration options
linter List all supported upstream linters
clean Clear any caches in the current directory and any subdirectories
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
Expand Down

0 comments on commit c782b67

Please sign in to comment.