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 cde089f
Show file tree
Hide file tree
Showing 19 changed files with 652 additions and 204 deletions.
23 changes: 17 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,35 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

- Add a new option `--rule` to the command `biome lint` ([#58](https://github.com/biomejs/biome/issues/58)).

This new option allows you to execute a single rule. This option is convenient to test a rule or apply the code fixes of a single rule.
This new option allows you to execute a single rule or a rule group.
This option is convenient to test a rule or apply the code fixes of a single rule.

For example, you can execute the `style/useNamingConvention` rule on a set of files:
For example, you can execute the `style/useNamingConvention` rule on the working directory:

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

If the rule has code action (autofix), you can use `--apply` to apply the fix:
If the rule has a 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 ./
```

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.
To ensure that the group is run, the `recommended` field of the group is turned on.

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
13 changes: 8 additions & 5 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 @@ -153,10 +153,13 @@ pub enum BiomeCommand {
#[bpaf(external, hide_usage)]
cli_options: CliOptions,

/// 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>,
/// Run only the given rule or rule group taking into account the configurations file.
///
/// Example: `biome lint --rule=correctness/noUnusedVariables`
///
/// Example: `biome lint --rule=suspicious`
#[bpaf(long("rule"), argument("GROUP|RULE"))]
rule: Option<RuleSelector>,

/// Use this option when you want to format code piped from `stdin`, and print the output to `stdout`.
///
Expand Down
6 changes: 3 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,8 @@ 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 or rule group taking into account the configurations file.
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
Expand Up @@ -7,7 +7,8 @@ expression: content
```block
Run various checks on a set of files.
Usage: lint [--apply] [--apply-unsafe] [--rule=NAME] [--staged] [--changed] [--since=REF] [PATH]...
Usage: lint [--apply] [--apply-unsafe] [--rule=<GROUP|RULE>] [--staged] [--changed] [--since=REF] [PATH
]...
Set of properties to integrate Biome with a VCS software.
--vcs-client-kind=<git> The kind of client.
Expand Down Expand Up @@ -60,8 +61,10 @@ Available positional items:
Available options:
--apply Apply safe fixes, formatting and import sorting
--apply-unsafe Apply safe fixes and unsafe fixes, formatting and import sorting
--rule=NAME 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=<GROUP|RULE> Run only the given rule or rule group taking into account the configurations
file.
Example: `biome lint --rule=correctness/noUnusedVariables`
Example: `biome lint --rule=suspicious`
--stdin-file-path=PATH Use this option when you want to format code piped from `stdin`, and
print the output to `stdout`.
The file doesn't need to exist on disk, what matters is the extension of
Expand Down
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.
```
Loading

0 comments on commit cde089f

Please sign in to comment.