diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-11-14 20:51:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-14 20:51:30 +0100 |
commit | 2cbf5c26ac1f81c740df862626145f5e1d6b2770 (patch) | |
tree | 5ea90de5aa46e24f40f466fb5d0c06d04d019f11 /cli/lint.rs | |
parent | 90290030469cc7f278492ece67dd783c4d380b43 (diff) |
feat(lint): support --rules --json (#8384)
This commit adds support for "--json" flag in combination
with "--rules". List of rules is serialized to JSON and printed.
Diffstat (limited to 'cli/lint.rs')
-rw-r--r-- | cli/lint.rs | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/cli/lint.rs b/cli/lint.rs index 02cf269c9..1ac680bac 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<dyn LintRule>) -> 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<serde_json::Value> = + 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()); + } } } |