Skip to content

Commit 275d625

Browse files
committed
feat(linter): output rules to json array (#7574)
closes #7517 cc @Sysix
1 parent 4e3044e commit 275d625

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

apps/oxlint/src/lint.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ impl Runner for LintRunner {
3636
fn run(self) -> CliRunResult {
3737
if self.options.list_rules {
3838
let mut stdout = BufWriter::new(std::io::stdout());
39-
Linter::print_rules(&mut stdout);
39+
if self.options.output_options.format == OutputFormat::Json {
40+
Linter::print_rules_json(&mut stdout);
41+
} else {
42+
Linter::print_rules(&mut stdout);
43+
}
4044
return CliRunResult::None;
4145
}
4246

crates/oxc_linter/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub mod table;
2626
use std::{io::Write, path::Path, rc::Rc, sync::Arc};
2727

2828
use oxc_semantic::{AstNode, Semantic};
29+
use rules::RULES;
2930

3031
pub use crate::{
3132
builder::{LinterBuilder, LinterBuilderError},
@@ -211,6 +212,30 @@ impl Linter {
211212
writeln!(writer, "Default: {}", table.turned_on_by_default_count).unwrap();
212213
writeln!(writer, "Total: {}", table.total).unwrap();
213214
}
215+
216+
/// # Panics
217+
pub fn print_rules_json<W: Write>(writer: &mut W) {
218+
#[derive(Debug, serde::Serialize)]
219+
struct RuleInfoJson<'a> {
220+
scope: &'a str,
221+
value: &'a str,
222+
category: RuleCategory,
223+
}
224+
225+
let rules_info = RULES.iter().map(|rule| RuleInfoJson {
226+
scope: rule.plugin_name(),
227+
value: rule.name(),
228+
category: rule.category(),
229+
});
230+
231+
writer
232+
.write_all(
233+
serde_json::to_string_pretty(&rules_info.collect::<Vec<_>>())
234+
.expect("Failed to serialize")
235+
.as_bytes(),
236+
)
237+
.unwrap();
238+
}
214239
}
215240

216241
#[cfg(test)]

0 commit comments

Comments
 (0)