From 275d6256bb4e14be9afdc9e50eaa18cd06285589 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Mon, 2 Dec 2024 03:17:56 +0000 Subject: [PATCH] feat(linter): output rules to json array (#7574) closes #7517 cc @Sysix --- apps/oxlint/src/lint.rs | 6 +++++- crates/oxc_linter/src/lib.rs | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index f5649f919..70d3f70b4 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -36,7 +36,11 @@ impl Runner for LintRunner { fn run(self) -> CliRunResult { if self.options.list_rules { let mut stdout = BufWriter::new(std::io::stdout()); - Linter::print_rules(&mut stdout); + if self.options.output_options.format == OutputFormat::Json { + Linter::print_rules_json(&mut stdout); + } else { + Linter::print_rules(&mut stdout); + } return CliRunResult::None; } diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 03a89a6e3..fa42f867c 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -26,6 +26,7 @@ pub mod table; use std::{io::Write, path::Path, rc::Rc, sync::Arc}; use oxc_semantic::{AstNode, Semantic}; +use rules::RULES; pub use crate::{ builder::{LinterBuilder, LinterBuilderError}, @@ -211,6 +212,30 @@ impl Linter { writeln!(writer, "Default: {}", table.turned_on_by_default_count).unwrap(); writeln!(writer, "Total: {}", table.total).unwrap(); } + + /// # Panics + pub fn print_rules_json(writer: &mut W) { + #[derive(Debug, serde::Serialize)] + struct RuleInfoJson<'a> { + scope: &'a str, + value: &'a str, + category: RuleCategory, + } + + let rules_info = RULES.iter().map(|rule| RuleInfoJson { + scope: rule.plugin_name(), + value: rule.name(), + category: rule.category(), + }); + + writer + .write_all( + serde_json::to_string_pretty(&rules_info.collect::>()) + .expect("Failed to serialize") + .as_bytes(), + ) + .unwrap(); + } } #[cfg(test)]