diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 266d65e866bcc..790214f27118d 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -15,13 +15,12 @@ use oxc_diagnostics::{DiagnosticSender, DiagnosticService, GraphicalReportHandle use oxc_linter::{ AllowWarnDeny, Config, ConfigStore, ConfigStoreBuilder, ExternalLinter, ExternalPluginStore, InvalidFilterKind, LintFilter, LintOptions, LintRunner, LintServiceOptions, Linter, Oxlintrc, - table::RuleTable, }; use crate::{ DEFAULT_OXLINTRC, cli::{CliRunResult, LintCommand, MiscOptions, ReportUnusedDirectives, WarningOptions}, - output_formatter::{LintCommandInfo, OutputFormat, OutputFormatter}, + output_formatter::{LintCommandInfo, OutputFormatter}, walk::Walk, }; use oxc_linter::LintIgnoreMatcher; @@ -227,32 +226,8 @@ impl CliRunner { } }; - // If the user requested `--rules`, print a CLI-specific table that - // includes an "Enabled?" column based on the resolved configuration. if self.options.list_rules { - // Preserve previous behavior of `--rules` output when `-f` is set - if self.options.output_options.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(); - - 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() { - print_and_flush_stdout(stdout, &output); - } - - return CliRunResult::None; + return crate::mode::run_rules(&lint_config, format_str, &output_formatter, stdout); } let search_for_nested_configs = !disable_nested_config && diff --git a/apps/oxlint/src/mode/mod.rs b/apps/oxlint/src/mode/mod.rs index 7c2ed50cecb7d..874180c16efe8 100644 --- a/apps/oxlint/src/mode/mod.rs +++ b/apps/oxlint/src/mode/mod.rs @@ -1,5 +1,7 @@ mod init; mod print_config; +mod rules; pub use init::run_init; pub use print_config::run_print_config; +pub use rules::run_rules; diff --git a/apps/oxlint/src/mode/rules.rs b/apps/oxlint/src/mode/rules.rs new file mode 100644 index 0000000000000..90706067a03e1 --- /dev/null +++ b/apps/oxlint/src/mode/rules.rs @@ -0,0 +1,41 @@ +use oxc_linter::{Config, table::RuleTable}; +use rustc_hash::FxHashSet; + +use crate::{ + cli::CliRunResult, + lint::print_and_flush_stdout, + output_formatter::{OutputFormat, 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(); + + 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() { + print_and_flush_stdout(stdout, &output); + } + + CliRunResult::None +}