Skip to content

Commit

Permalink
feat(linter): output rules to json array (#7574)
Browse files Browse the repository at this point in the history
closes #7517

cc @Sysix
  • Loading branch information
camc314 committed Dec 2, 2024
1 parent 4e3044e commit 275d625
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
25 changes: 25 additions & 0 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 275d625

Please sign in to comment.