Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/oxlint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
31 changes: 6 additions & 25 deletions apps/oxlint/src/mode/rules.rs
Original file line number Diff line number Diff line change
@@ -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);
}

Expand Down
10 changes: 6 additions & 4 deletions apps/oxlint/src/output_formatter/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
fn all_rules(&self, enabled_rules: FxHashSet<&str>) -> Option<String> {
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(&section.render_markdown_table_cli(&enabled_rules));
output.push('\n');
}
output.push_str(format!("Default: {}\n", table.turned_on_by_default_count).as_str());
Expand Down Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion apps/oxlint/src/output_formatter/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct JsonOutputFormatter {
}

impl InternalFormatter for JsonOutputFormatter {
fn all_rules(&self) -> Option<String> {
fn all_rules(&self, _enabled_rules: FxHashSet<&str>) -> Option<String> {
#[derive(Debug, Serialize)]
struct RuleInfoJson<'a> {
scope: &'a str,
Expand Down
7 changes: 4 additions & 3 deletions apps/oxlint/src/output_formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> {
fn all_rules(&self, _enabled_rules: FxHashSet<&str>) -> Option<String> {
None
}

Expand Down Expand Up @@ -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<String> {
self.internal.all_rules()
pub fn all_rules(&self, enabled_rules: FxHashSet<&str>) -> Option<String> {
self.internal.all_rules(enabled_rules)
}

/// At the end of the Lint command we may output extra information.
Expand Down
Loading