Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
28 changes: 9 additions & 19 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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::{
Expand Down Expand Up @@ -304,25 +303,16 @@ 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> =
config_store.rules().iter().map(|(rule, _)| rule.name()).collect();

let table = RuleTable::default();
for section in &table.sections {
let md = section.render_markdown_table_cli(None, &enabled);
print_and_flush_stdout(stdout, &md);
print_and_flush_stdout(stdout, "\n");
}
// Put together the enabled hashset if the format is default, otherwise None
let enabled: Option<FxHashSet<&str>> =
if self.options.output_options.format == OutputFormat::Default {
// Build the set of enabled builtin rule names from the resolved config.
Some(config_store.rules().iter().map(|(rule, _)| rule.name()).collect())
} else {
None
};

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.as_ref()) {
print_and_flush_stdout(stdout, &output);
}

Expand Down
23 changes: 20 additions & 3 deletions apps/oxlint/src/output_formatter/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ 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: Option<&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(None).as_str());
output.push_str(section.render_markdown_table(None, enabled).as_str());
output.push('\n');
}
output.push_str(format!("Default: {}\n", table.turned_on_by_default_count).as_str());
if enabled.is_some() {
output.push_str(format!("Enabled: {}\n", enabled.unwrap().len()).as_str());
}
output.push_str(format!("Total: {}\n", table.total).as_str());
Some(output)
}
Expand Down Expand Up @@ -163,13 +167,26 @@ 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(None);

assert!(result.is_some());
}

#[test]
fn all_rules_with_enabled() {
let formatter = DefaultOutputFormatter;
// Pass in one enabled rule to make sure it renders fine:
let mut enabled = FxHashSet::default();
enabled.insert("no-unused-vars");
let result = formatter.all_rules(Some(&enabled));

assert!(result.is_some());
assert!(result.unwrap().contains("Enabled: 1\n"));
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion apps/oxlint/src/output_formatter/json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{cell::RefCell, rc::Rc};

use miette::JSONReportHandler;
use rustc_hash::FxHashSet;
use serde::Serialize;

use oxc_diagnostics::{
Expand All @@ -17,7 +18,7 @@ pub struct JsonOutputFormatter {
}

impl InternalFormatter for JsonOutputFormatter {
fn all_rules(&self) -> Option<String> {
fn all_rules(&self, _enabled: Option<&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: Option<&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: Option<&FxHashSet<&str>>) -> Option<String> {
self.internal.all_rules(enabled)
}

/// At the end of the Lint command we may output extra information.
Expand Down
19 changes: 9 additions & 10 deletions crates/oxc_linter/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,15 @@ impl RuleTableSection {
///
/// Provide [`Some`] prefix to render the rule name as a link. Provide
/// [`None`] to just display the rule name as text.
pub fn render_markdown_table(&self, link_prefix: Option<&str>) -> String {
self.render_markdown_table_inner(link_prefix, None)
}

pub fn render_markdown_table_cli(
///
/// Provide [`Some`] set of enabled rule names to include an "Enabled?" column.
/// Provide [`None`] to omit the column.
pub fn render_markdown_table(
&self,
link_prefix: Option<&str>,
enabled: &FxHashSet<&str>,
enabled: Option<&FxHashSet<&str>>,
) -> String {
self.render_markdown_table_inner(link_prefix, Some(enabled))
self.render_markdown_table_inner(link_prefix, enabled)
}
}

Expand All @@ -256,7 +255,7 @@ mod test {
fn test_table_no_links() {
let options = Options::gfm();
for section in &table().sections {
let rendered_table = section.render_markdown_table(None);
let rendered_table = section.render_markdown_table(None, None);
assert!(!rendered_table.is_empty());
assert_eq!(rendered_table.split('\n').count(), 5 + section.rows.len());

Expand All @@ -274,7 +273,7 @@ mod test {
let options = Options::gfm();

for section in &table().sections {
let rendered_table = section.render_markdown_table(Some(PREFIX));
let rendered_table = section.render_markdown_table(Some(PREFIX), None);
assert!(!rendered_table.is_empty());
assert_eq!(rendered_table.split('\n').count(), 5 + section.rows.len());

Expand All @@ -296,7 +295,7 @@ mod test {
enabled.insert(first.name);
}

let rendered_table = section.render_markdown_table_cli(Some(PREFIX), &enabled);
let rendered_table = section.render_markdown_table(Some(PREFIX), Some(&enabled));
assert!(!rendered_table.is_empty());
// same number of lines as other renderer (header + desc + separator + rows + trailing newline)
assert_eq!(rendered_table.split('\n').count(), 5 + section.rows.len());
Expand Down
2 changes: 1 addition & 1 deletion tasks/website/src/linter/rules/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn render_rules_table(table: &RuleTable, docs_prefix: &str) -> String {
let body = table
.sections
.iter()
.map(|s| s.render_markdown_table(Some(docs_prefix)))
.map(|s| s.render_markdown_table(Some(docs_prefix), None))
.collect::<Vec<_>>()
.join("\n");

Expand Down
Loading