Skip to content

Commit

Permalink
feat(cli): rage now prints all enabled rules (#2946)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed May 29, 2024
1 parent 8a26e25 commit 4ed208a
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 109 deletions.
13 changes: 13 additions & 0 deletions crates/biome_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,19 @@ impl<'a> Display for RuleFilter<'a> {
}
}

impl<'a> biome_console::fmt::Display for RuleFilter<'a> {
fn fmt(&self, fmt: &mut biome_console::fmt::Formatter) -> std::io::Result<()> {
match self {
RuleFilter::Group(group) => {
write!(fmt, "{group}")
}
RuleFilter::Rule(group, rule) => {
write!(fmt, "{group}/{rule}")
}
}
}
}

/// Allows filtering the list of rules that will be executed in a run of the analyzer,
/// and at what source code range signals (diagnostics or actions) may be raised
#[derive(Debug, Default, Clone, Copy)]
Expand Down
66 changes: 20 additions & 46 deletions crates/biome_cli/src/commands/rage.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
use biome_configuration::ConfigurationPathHint;
use biome_configuration::{ConfigurationPathHint, Rules};
use biome_console::fmt::{Display, Formatter};
use biome_console::{fmt, markup, ConsoleExt, HorizontalLine, Markup};
use biome_console::{fmt, markup, ConsoleExt, HorizontalLine, Markup, Padding, SOFT_LINE};
use biome_diagnostics::termcolor::{ColorChoice, WriteColor};
use biome_diagnostics::{termcolor, PrintDescription};
use biome_fs::FileSystem;
use biome_service::configuration::{load_configuration, LoadedConfiguration};
use biome_service::workspace::{client, RageEntry, RageParams};
use biome_service::{DynRef, Workspace};
use serde_json::Value;
use std::collections::HashMap;
use std::{env, io, ops::Deref};
use tokio::runtime::Runtime;

use crate::commands::daemon::read_most_recent_log_file;
use crate::service::enumerate_pipes;
use crate::{service, CliDiagnostic, CliSession, VERSION};
use serde::Serialize;

/// Handler for the `rage` command
pub(crate) fn rage(
Expand Down Expand Up @@ -273,11 +270,18 @@ impl Display for RageConfiguration<'_, '_> {
// Print linter configuration if --linter option is true
if self.linter {
let linter_configuration = configuration.get_linter_rules();

let javascript_linter = configuration.get_javascript_linter_configuration();
let json_linter = configuration.get_json_linter_configuration();
let css_linter = configuration.get_css_linter_configuration();
markup! (
{Section("Linter")}
{KeyValuePair("JavaScript enabled", markup!({DebugDisplay(javascript_linter.enabled)}))}
{KeyValuePair("JSON enabled", markup!({DebugDisplay(json_linter.enabled)}))}
{KeyValuePair("CSS enabled", markup!({DebugDisplay(css_linter.enabled)}))}
{KeyValuePair("Recommended", markup!({DebugDisplay(linter_configuration.recommended.unwrap_or_default())}))}
{KeyValuePair("All", markup!({DebugDisplay(linter_configuration.all.unwrap_or_default())}))}
{RageConfigurationLintRules("Rules",linter_configuration)}
{RageConfigurationLintRules("Enabled rules",linter_configuration)}
).fmt(fmt)?;
}
}
Expand All @@ -293,48 +297,18 @@ impl Display for RageConfiguration<'_, '_> {
}
}

struct RageConfigurationLintRules<'a, T>(&'a str, T);
struct RageConfigurationLintRules<'a>(&'a str, Rules);

impl<T> Display for RageConfigurationLintRules<'_, T>
where
T: Serialize,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> io::Result<()> {
impl Display for RageConfigurationLintRules<'_> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> io::Result<()> {
let rules_str = self.0;
write!(fmt, " {rules_str}:")?;

let rule_json_str = serde_json::to_string(&self.1)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to serialize"))?;
let group_to_rules: HashMap<String, Value> = serde_json::from_str(rule_json_str.as_str())
.map_err(|_| {
io::Error::new(io::ErrorKind::Other, "Failed to convert to HashMap")
})?;

let mut groups: Vec<&String> = group_to_rules.keys().collect();
groups.sort();

let first_padding_width = 30usize.saturating_sub(rules_str.len() + 1);
let mut padding_width = first_padding_width;

if groups.is_empty() {
for _ in 0..padding_width {
fmt.write_str(" ")?;
}
markup!(<Dim>"unset\n"</Dim>).fmt(fmt)?;
} else {
for group in groups {
if let Some(rules) = group_to_rules.get(group).and_then(Value::as_object) {
for (rule_name, rule_config) in rules {
for _ in 0..padding_width {
fmt.write_str(" ")?;
}
fmt.write_str(&format!("{}/{} = {}\n", group, rule_name, rule_config))?;
if padding_width == first_padding_width {
padding_width = 30usize.saturating_sub(0) + 2;
}
}
}
}
let padding = Padding::new(2);
fmt.write_markup(markup! {{padding}{rules_str}":"})?;
fmt.write_markup(markup! {{SOFT_LINE}})?;
let rules = self.1.as_enabled_rules();
for rule in rules {
fmt.write_markup(markup! {{padding}{rule}})?;
fmt.write_markup(markup! {{SOFT_LINE}})?;
}

Ok(())
Expand Down
13 changes: 1 addition & 12 deletions crates/biome_cli/tests/commands/rage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,25 +289,14 @@ fn with_linter_configuration() {
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"recommended": false,
"a11y": {
"noAccessKey": "off",
"noAutofocus": "off"
},
"complexity": {
"recommended": true
},
"correctness": {
"all": true
},
"nursery": {
"useConsistentArrayType": {
"level": "warn",
"options": {
"syntax": "shorthand"
}
}
},
"suspicious": {
"noCommentText": {
"level": "warn"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,14 @@ expression: content
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"recommended": false,
"a11y": {
"noAccessKey": "off",
"noAutofocus": "off"
},
"complexity": {
"recommended": true
},
"correctness": {
"all": true
},
"nursery": {
"useConsistentArrayType": {
"level": "warn",
"options": {
"syntax": "shorthand"
}
}
},
"suspicious": {
"noCommentText": {
"level": "warn"
Expand Down Expand Up @@ -61,22 +50,46 @@ Environment:
NODE_PACKAGE_MANAGER: unset
Biome Configuration:
Error: Found an unknown key `useConsistentArrayType`.
Status: Loaded with errors
Status: Loaded successfully
Formatter disabled: false
Linter disabled: false
Organize imports disabled: false
VCS disabled: true
Linter:
Recommended: true
JavaScript enabled: true
JSON enabled: true
CSS enabled: false
Recommended: false
All: false
Rules: a11y/noAccessKey = "off"
a11y/noAutofocus = "off"
complexity/recommended = true
correctness/all = true
style/noNonNullAssertion = "off"
suspicious/noCommentText = {"level":"warn","options":null}
Enabled rules:
complexity/noBannedTypes
complexity/noThisInStatic
complexity/noUselessConstructor
complexity/useLiteralKeys
complexity/useOptionalChain
complexity/noMultipleSpacesInRegularExpressionLiterals
complexity/noUselessLoneBlockStatements
complexity/noUselessEmptyExport
complexity/noUselessSwitchCase
complexity/noStaticOnlyClass
complexity/noUselessTypeConstraint
complexity/noWith
complexity/useRegexLiterals
complexity/useArrowFunction
complexity/noExtraBooleanCast
complexity/noEmptyTypeParameters
complexity/useSimpleNumberKeys
suspicious/noCommentText
complexity/noExcessiveNestedTestSuites
complexity/noUselessLabel
complexity/noUselessCatch
complexity/noUselessFragments
complexity/noUselessTernary
complexity/noForEach
complexity/useFlatMap
complexity/noUselessRename
complexity/noUselessThisAlias
Server:
Version: 0.0.0
Expand All @@ -87,5 +100,3 @@ Server:
Workspace:
Open Documents: 0
```


8 changes: 8 additions & 0 deletions crates/biome_configuration/src/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,11 @@ pub struct CssLinter {
#[partial(bpaf(long("css-linter-enabled"), argument("true|false"), optional))]
pub enabled: bool,
}

impl PartialCssLinter {
pub fn get_linter_configuration(&self) -> CssLinter {
CssLinter {
enabled: self.enabled.unwrap_or_default(),
}
}
}
25 changes: 0 additions & 25 deletions crates/biome_configuration/src/javascript/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,3 @@ impl Default for JavascriptFormatter {
}
}
}

/// Linter options specific to the JavaScript linter
#[derive(Clone, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)]
#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))]
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
pub struct JavascriptLinter {
/// Control the linter for JavaScript (and its super languages) files.
#[partial(bpaf(long("javascript-linter-enabled"), argument("true|false"), optional))]
pub enabled: bool,
}

impl Default for JavascriptLinter {
fn default() -> Self {
Self { enabled: true }
}
}

impl PartialJavascriptLinter {
pub fn get_formatter_configuration(&self) -> JavascriptLinter {
JavascriptLinter {
enabled: self.enabled.unwrap_or_default(),
}
}
}
29 changes: 26 additions & 3 deletions crates/biome_configuration/src/javascript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ mod formatter;

use std::str::FromStr;

use crate::javascript::formatter::JavascriptLinter;
use biome_deserialize::StringSet;
use biome_deserialize_macros::{Deserializable, Merge, Partial};
use bpaf::Bpaf;
pub use formatter::{
partial_javascript_formatter, partial_javascript_linter, JavascriptFormatter,
PartialJavascriptFormatter, PartialJavascriptLinter,
partial_javascript_formatter, JavascriptFormatter, PartialJavascriptFormatter,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -97,3 +95,28 @@ impl FromStr for JsxRuntime {
}
}
}

/// Linter options specific to the JavaScript linter
#[derive(Clone, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)]
#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))]
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
pub struct JavascriptLinter {
/// Control the linter for JavaScript (and its super languages) files.
#[partial(bpaf(long("javascript-linter-enabled"), argument("true|false"), optional))]
pub enabled: bool,
}

impl Default for JavascriptLinter {
fn default() -> Self {
Self { enabled: true }
}
}

impl PartialJavascriptLinter {
pub fn get_linter_configuration(&self) -> JavascriptLinter {
JavascriptLinter {
enabled: self.enabled.unwrap_or_default(),
}
}
}
8 changes: 8 additions & 0 deletions crates/biome_configuration/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,11 @@ impl Default for JsonLinter {
Self { enabled: true }
}
}

impl PartialJsonLinter {
pub fn get_linter_configuration(&self) -> JsonLinter {
JsonLinter {
enabled: self.enabled.unwrap_or_default(),
}
}
}
Loading

0 comments on commit 4ed208a

Please sign in to comment.