feat(linter): output rules to json array (#7574)

closes #7517

cc @Sysix
This commit is contained in:
camc314 2024-12-02 03:17:56 +00:00
parent 4e3044e225
commit 275d6256bb
2 changed files with 30 additions and 1 deletions

View file

@ -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;
}

View file

@ -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<W: Write>(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::<Vec<_>>())
.expect("Failed to serialize")
.as_bytes(),
)
.unwrap();
}
}
#[cfg(test)]