diff --git a/apps/oxlint/src/lib.rs b/apps/oxlint/src/lib.rs index 632a0208fbe7b..4812ac88c741a 100644 --- a/apps/oxlint/src/lib.rs +++ b/apps/oxlint/src/lib.rs @@ -25,6 +25,7 @@ pub mod cli { mod run; #[cfg(feature = "napi")] pub use run::*; +use rustc_hash::FxHashSet; // JS plugins are only supported on 64-bit little-endian platforms at present. // Note: `raw_transfer_constants` module will not compile on 32-bit systems. @@ -57,5 +58,7 @@ const DEFAULT_OXLINTRC: &str = ".oxlintrc.json"; pub fn get_all_rules_json() -> String { use crate::output_formatter::{OutputFormat, OutputFormatter}; - OutputFormatter::new(OutputFormat::Json).all_rules().expect("Failed to generate rules JSON") + OutputFormatter::new(OutputFormat::Json) + .all_rules(FxHashSet::default()) + .expect("Failed to generate rules JSON") } diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 790214f27118d..ecf6e45c0dfef 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -227,7 +227,7 @@ impl CliRunner { }; if self.options.list_rules { - return crate::mode::run_rules(&lint_config, format_str, &output_formatter, stdout); + return crate::mode::run_rules(&lint_config, &output_formatter, stdout); } let search_for_nested_configs = !disable_nested_config && diff --git a/apps/oxlint/src/mode/rules.rs b/apps/oxlint/src/mode/rules.rs index 90706067a03e1..3ae83d47e5a09 100644 --- a/apps/oxlint/src/mode/rules.rs +++ b/apps/oxlint/src/mode/rules.rs @@ -1,39 +1,20 @@ -use oxc_linter::{Config, table::RuleTable}; +use oxc_linter::Config; use rustc_hash::FxHashSet; -use crate::{ - cli::CliRunResult, - lint::print_and_flush_stdout, - output_formatter::{OutputFormat, OutputFormatter}, -}; +use crate::{cli::CliRunResult, lint::print_and_flush_stdout, output_formatter::OutputFormatter}; /// If the user requested `--rules`, print a CLI-specific table that /// includes an "Enabled?" column based on the resolved configuration. pub fn run_rules( lint_config: &Config, - output_format: OutputFormat, output_formatter: &OutputFormatter, stdout: &mut dyn std::io::Write, ) -> CliRunResult { - // Preserve previous behavior of `--rules` output when `-f` is set - if output_format == OutputFormat::Default { - // Build the set of enabled builtin rule names from the resolved config. - let enabled: FxHashSet<&str> = - lint_config.rules().iter().map(|(rule, _)| rule.name()).collect(); + // Build the set of enabled builtin rule names from the resolved config. + let enabled: FxHashSet<&str> = + lint_config.rules().iter().map(|(rule, _)| rule.name()).collect(); - let table = RuleTable::default(); - for section in &table.sections { - let md = section.render_markdown_table_cli(&enabled); - print_and_flush_stdout(stdout, &md); - print_and_flush_stdout(stdout, "\n"); - } - - print_and_flush_stdout( - stdout, - format!("Default: {}\n", table.turned_on_by_default_count).as_str(), - ); - print_and_flush_stdout(stdout, format!("Total: {}\n", table.total).as_str()); - } else if let Some(output) = output_formatter.all_rules() { + if let Some(output) = output_formatter.all_rules(enabled) { print_and_flush_stdout(stdout, &output); } diff --git a/apps/oxlint/src/output_formatter/default.rs b/apps/oxlint/src/output_formatter/default.rs index 8c5444e2f2a7f..fe9113f720eb7 100644 --- a/apps/oxlint/src/output_formatter/default.rs +++ b/apps/oxlint/src/output_formatter/default.rs @@ -6,16 +6,17 @@ use oxc_diagnostics::{ reporter::{DiagnosticReporter, DiagnosticResult}, }; use oxc_linter::table::RuleTable; +use rustc_hash::FxHashSet; #[derive(Debug)] pub struct DefaultOutputFormatter; impl InternalFormatter for DefaultOutputFormatter { - fn all_rules(&self) -> Option { + fn all_rules(&self, enabled_rules: FxHashSet<&str>) -> Option { let mut output = String::new(); let table = RuleTable::default(); - for section in table.sections { - output.push_str(section.render_markdown_table().as_str()); + for section in &table.sections { + output.push_str(§ion.render_markdown_table_cli(&enabled_rules)); output.push('\n'); } output.push_str(format!("Default: {}\n", table.turned_on_by_default_count).as_str()); @@ -165,11 +166,12 @@ mod test { default::{DefaultOutputFormatter, GraphicalReporter}, }; use oxc_diagnostics::reporter::{DiagnosticReporter, DiagnosticResult}; + use rustc_hash::FxHashSet; #[test] fn all_rules() { let formatter = DefaultOutputFormatter; - let result = formatter.all_rules(); + let result = formatter.all_rules(FxHashSet::default()); assert!(result.is_some()); } diff --git a/apps/oxlint/src/output_formatter/json.rs b/apps/oxlint/src/output_formatter/json.rs index 3aa1e0d1e51b7..572e0d71782f8 100644 --- a/apps/oxlint/src/output_formatter/json.rs +++ b/apps/oxlint/src/output_formatter/json.rs @@ -19,7 +19,7 @@ pub struct JsonOutputFormatter { } impl InternalFormatter for JsonOutputFormatter { - fn all_rules(&self) -> Option { + fn all_rules(&self, _enabled_rules: FxHashSet<&str>) -> Option { #[derive(Debug, Serialize)] struct RuleInfoJson<'a> { scope: &'a str, diff --git a/apps/oxlint/src/output_formatter/mod.rs b/apps/oxlint/src/output_formatter/mod.rs index ebad01589e3e5..9fea06632dd63 100644 --- a/apps/oxlint/src/output_formatter/mod.rs +++ b/apps/oxlint/src/output_formatter/mod.rs @@ -15,6 +15,7 @@ use checkstyle::CheckStyleOutputFormatter; use github::GithubOutputFormatter; use gitlab::GitlabOutputFormatter; use junit::JUnitOutputFormatter; +use rustc_hash::FxHashSet; use stylish::StylishOutputFormatter; use unix::UnixOutputFormatter; @@ -72,7 +73,7 @@ pub struct LintCommandInfo { /// The Formatter is then managed by [`OutputFormatter`]. trait InternalFormatter { /// Print all available rules by oxlint - fn all_rules(&self) -> Option { + fn all_rules(&self, _enabled_rules: FxHashSet<&str>) -> Option { None } @@ -110,8 +111,8 @@ impl OutputFormatter { /// Print all available rules by oxlint /// See [`InternalFormatter::all_rules`] for more details. - pub fn all_rules(&self) -> Option { - self.internal.all_rules() + pub fn all_rules(&self, enabled_rules: FxHashSet<&str>) -> Option { + self.internal.all_rules(enabled_rules) } /// At the end of the Lint command we may output extra information.