diff --git a/cli/lint.rs b/cli/lint.rs index 02cf269c97..1ac680bac3 100644 --- a/cli/lint.rs +++ b/cli/lint.rs @@ -96,14 +96,29 @@ pub async fn lint_files( Ok(()) } -pub fn print_rules_list() { +fn rule_to_json(rule: Box) -> serde_json::Value { + serde_json::json!({ + "code": rule.code(), + "tags": rule.tags(), + "docs": rule.docs(), + }) +} + +pub fn print_rules_list(json: bool) { let lint_rules = rules::get_recommended_rules(); - // The rules should still be printed even if `--quiet` option is enabled, - // so use `println!` here instead of `info!`. - println!("Available rules:"); - for rule in lint_rules { - println!(" - {}", rule.code()); + if json { + let json_rules: Vec = + lint_rules.into_iter().map(rule_to_json).collect(); + let json_str = serde_json::to_string_pretty(&json_rules).unwrap(); + println!("{}", json_str); + } else { + // The rules should still be printed even if `--quiet` option is enabled, + // so use `println!` here instead of `info!`. + println!("Available rules:"); + for rule in lint_rules { + println!(" - {}", rule.code()); + } } } diff --git a/cli/main.rs b/cli/main.rs index 752e2e5a53..87e817cf00 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -229,7 +229,7 @@ async fn lint_command( } if list_rules { - lint::print_rules_list(); + lint::print_rules_list(json); return Ok(()); }