Skip to content

Commit

Permalink
refactor: allow group as rule filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed May 15, 2024
1 parent cc05d50 commit 8990648
Show file tree
Hide file tree
Showing 18 changed files with 532 additions and 190 deletions.
15 changes: 12 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,28 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
For example, you can execute the `style/useNamingConvention` rule on a set of files:

```shell
biome lint --rule=style/useNamingConvention src/index.js src/main.js
biome lint --rule=style/useNamingConvention src/main.js
```

If the rule has code action (autofix), you can use `--apply` to apply the fix:

```shell
biome lint --rule=style/useNamingConvention --apply index.js src/main.js
biome lint --rule=style/useNamingConvention --apply src/main.js
```

The option takes the rule options set in the Biome configuration file into account.
The option takes the rule options in the Biome configuration file into account.
Only, the severity level of the rule is overridden by its default value,
i.e. `error` for a recommended rule or `warn` otherwise.

You can also run a group of rules:

```shell
biome lint --rule=suspicous src/main.js
```

In this case, the severity level of a rule is not overridden.
Thus, disabled rules stay disabled.

The option is compatible with other options such as `--apply`, `--apply-unsafe` and `--reporter`.

Contributed by @Conaclos
Expand Down
9 changes: 8 additions & 1 deletion crates/biome_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,14 @@ pub enum RuleFilter<'a> {
Rule(&'a str, &'a str),
}

impl RuleFilter<'_> {
impl<'a> RuleFilter<'a> {
// Returns the group name of thie filter.
pub fn group(self) -> &'a str {
match self {
RuleFilter::Group(group) => group,
RuleFilter::Rule(group, _) => group,
}
}
/// Return `true` if the group `G` matches this filter
fn match_group<G: RuleGroup>(self) -> bool {
match self {
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::commands::{
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
use biome_configuration::linter::RuleCode;
use biome_configuration::linter::RuleSelector;
use biome_configuration::vcs::PartialVcsConfiguration;
use biome_configuration::{
PartialConfiguration, PartialFilesConfiguration, PartialLinterConfiguration,
Expand All @@ -25,7 +25,7 @@ pub(crate) struct LintCommandPayload {
pub(crate) vcs_configuration: Option<PartialVcsConfiguration>,
pub(crate) files_configuration: Option<PartialFilesConfiguration>,
pub(crate) paths: Vec<OsString>,
pub(crate) rule: Option<RuleCode>,
pub(crate) rule: Option<RuleSelector>,
pub(crate) stdin_file_path: Option<String>,
pub(crate) staged: bool,
pub(crate) changed: bool,
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::diagnostics::DeprecatedConfigurationFile;
use crate::execute::Stdin;
use crate::logging::LoggingKind;
use crate::{CliDiagnostic, CliSession, LoggingLevel, VERSION};
use biome_configuration::linter::RuleCode;
use biome_configuration::linter::RuleSelector;
use biome_configuration::{
css::partial_css_formatter, javascript::partial_javascript_formatter,
json::partial_json_formatter, partial_configuration, partial_files_configuration,
Expand Down Expand Up @@ -156,7 +156,7 @@ pub enum BiomeCommand {
/// Run only the given rule taking into account the options set in the configurations files.
/// The severity level of the rule is set to its default.
#[bpaf(long("rule"), argument("NAME"))]
rule: Option<RuleCode>,
rule: Option<RuleSelector>,

/// Use this option when you want to format code piped from `stdin`, and print the output to `stdout`.
///
Expand Down
7 changes: 4 additions & 3 deletions crates/biome_cli/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::execute::traverse::traverse;
use crate::reporter::json::{JsonReporter, JsonReporterVisitor};
use crate::reporter::terminal::{ConsoleReporter, ConsoleReporterVisitor};
use crate::{CliDiagnostic, CliSession, DiagnosticsPayload, Reporter};
use biome_configuration::linter::RuleCode;
use biome_configuration::linter::RuleSelector;
use biome_console::{markup, ConsoleExt};
use biome_diagnostics::adapters::SerdeJsonError;
use biome_diagnostics::{category, Category};
Expand Down Expand Up @@ -126,8 +126,9 @@ pub enum TraversalMode {
/// 1. The virtual path to the file
/// 2. The content of the file
stdin: Option<Stdin>,

rule: Option<RuleCode>,
/// Run only the given rule taking into account the options set in the configurations files.
/// The severity level of the rule is set to its default.
rule: Option<RuleSelector>,
},
/// This mode is enabled when running the command `biome ci`
CI {
Expand Down
92 changes: 92 additions & 0 deletions crates/biome_cli/tests/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3498,3 +3498,95 @@ fn lint_rule_filter_with_linter_disabled() {
result,
));
}

#[test]
fn lint_rule_filter_group() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();
let config = r#"{
"linter": {
"rules": {
"suspicious": {
"recommended": false
}
}
}
}"#;
let content = r#"
export function CONSTANT_CASE(){
debugger;
}
"#;

let file_path = Path::new("check.js");
fs.insert(file_path.into(), content.as_bytes());
let config_path = Path::new("biome.json");
fs.insert(config_path.into(), config.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("lint"),
"--rule=suspicious",
file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"lint_rule_filter_group",
fs,
console,
result,
));
}

#[test]
fn lint_rule_filter_group_with_disabled_rule() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();
let config = r#"{
"linter": {
"rules": {
"suspicious": {
"noDebugger": "off"
}
}
}
}"#;
let content = r#"
export function CONSTANT_CASE(){
debugger;
}
"#;

let file_path = Path::new("check.js");
fs.insert(file_path.into(), content.as_bytes());
let config_path = Path::new("biome.json");
fs.insert(config_path.into(), config.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("lint"),
"--rule=suspicious",
file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"lint_rule_filter_group_with_disabled_rule",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{
"linter": {
"rules": {
"suspicious": {
"recommended": false
}
}
}
}
```

## `check.js`

```js

export function CONSTANT_CASE(){
debugger;
}

```

# Termination Message

```block
lint ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Some errors were emitted while running checks.
```

# Emitted Messages

```block
check.js:3:9 lint/suspicious/noDebugger FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× This is an unexpected use of the debugger statement.
2 │ export function CONSTANT_CASE(){
> 3 │ debugger;
│ ^^^^^^^^^
4 │ }
5 │
i Unsafe fix: Remove debugger statement
1 1 │
2 2 │ export function CONSTANT_CASE(){
3 │ - ········debugger;
4 3 │ }
5 4 │
```

```block
Checked 1 file in <TIME>. No fixes needed.
Found 1 error.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{
"linter": {
"rules": {
"suspicious": {
"recommended": false
}
}
}
}
```

## `check.js`

```js

export function CONSTANT_CASE(){
debugger;
}

```

# Termination Message

```block
lint ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Some errors were emitted while running checks.
```

# Emitted Messages

```block
check.js:3:9 lint/suspicious/noDebugger FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× This is an unexpected use of the debugger statement.
2 │ export function CONSTANT_CASE(){
> 3 │ debugger;
│ ^^^^^^^^^
4 │ }
5 │
i Unsafe fix: Remove debugger statement
1 1 │
2 2 │ export function CONSTANT_CASE(){
3 │ - ········debugger;
4 3 │ }
5 4 │
```

```block
Checked 1 file in <TIME>. No fixes needed.
Found 1 error.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{
"linter": {
"rules": {
"suspicious": {
"noDebugger": "off"
}
}
}
}
```

## `check.js`

```js

export function CONSTANT_CASE(){
debugger;
}

```

# Emitted Messages

```block
Checked 1 file in <TIME>. No fixes needed.
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ flags/invalid ━━━━━━━━━━━━━━━━━━━━━━
× Failed to parse CLI arguments.
Caused by:
couldn't parse `noDebugger`: a group and a rule name separated by a slash is required.
couldn't parse `noDebugger`: This group doesn't exist. Use the syntax `<group>/<rule>` to specify a rule.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ flags/invalid ━━━━━━━━━━━━━━━━━━━━━━
× Failed to parse CLI arguments.
Caused by:
couldn't parse `suspicious/inexistant`: the rule doesn't exist.
couldn't parse `suspicious/inexistant`: This rule doesn't exist.
Expand Down
Loading

0 comments on commit 8990648

Please sign in to comment.