Skip to content

Commit

Permalink
feat(cli/lint): add the --rule option (#2860)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed May 16, 2024
1 parent 40f966b commit 3d74b9f
Show file tree
Hide file tree
Showing 31 changed files with 2,675 additions and 274 deletions.
37 changes: 37 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,43 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

### CLI

#### New features

- 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 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 the working directory:

```shell
biome lint --rule=style/useNamingConvention ./
```

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

```shell
biome lint --rule=style/useNamingConvention --apply ./
```

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

#### Enhancements

- Biome now executes commands (lint, format, check and ci) on the working directory by default. [#2266](https://github.com/biomejs/biome/issues/2266) Contributed by @unvalley
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: 4 additions & 0 deletions crates/biome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::commands::{
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
use biome_configuration::linter::RuleSelector;
use biome_configuration::vcs::PartialVcsConfiguration;
use biome_configuration::{
PartialConfiguration, PartialFilesConfiguration, PartialLinterConfiguration,
Expand All @@ -24,6 +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<RuleSelector>,
pub(crate) stdin_file_path: Option<String>,
pub(crate) staged: bool,
pub(crate) changed: bool,
Expand All @@ -38,6 +40,7 @@ pub(crate) fn lint(session: CliSession, payload: LintCommandPayload) -> Result<(
cli_options,
mut linter_configuration,
mut paths,
rule,
stdin_file_path,
vcs_configuration,
files_configuration,
Expand Down Expand Up @@ -128,6 +131,7 @@ pub(crate) fn lint(session: CliSession, payload: LintCommandPayload) -> Result<(
Execution::new(TraversalMode::Lint {
fix_file_mode,
stdin,
rule,
})
.set_report(&cli_options),
session,
Expand Down
10 changes: 10 additions & 0 deletions crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::diagnostics::DeprecatedConfigurationFile;
use crate::execute::Stdin;
use crate::logging::LoggingKind;
use crate::{CliDiagnostic, CliSession, LoggingLevel, VERSION};
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 @@ -151,6 +152,15 @@ pub enum BiomeCommand {

#[bpaf(external, hide_usage)]
cli_options: CliOptions,

/// Run only the given rule or rule group taking the configurations file into account.
///
/// 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`.
///
/// The file doesn't need to exist on disk, what matters is the extension of the file. Based on the extension, Biome knows how to lint the code.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/biome_cli/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +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::RuleSelector;
use biome_console::{markup, ConsoleExt};
use biome_diagnostics::adapters::SerdeJsonError;
use biome_diagnostics::{category, Category};
Expand Down Expand Up @@ -125,6 +126,8 @@ pub enum TraversalMode {
/// 1. The virtual path to the file
/// 2. The content of the file
stdin: Option<Stdin>,
/// Run only the given rule or rule group taking the configurations file into account.
rule: Option<RuleSelector>,
},
/// This mode is enabled when running the command `biome ci`
CI {
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_cli/src/execute/process_file/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) fn format_with_guard<'ctx>(
debug!("Pulling diagnostics from parsed file");
let diagnostics_result = workspace_file
.guard()
.pull_diagnostics(RuleCategories::SYNTAX, max_diagnostics.into())
.pull_diagnostics(RuleCategories::SYNTAX, max_diagnostics.into(), None)
.with_file_path_and_code(
workspace_file.path.display().to_string(),
category!("format"),
Expand Down
7 changes: 7 additions & 0 deletions crates/biome_cli/src/execute/process_file/lint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::execute::diagnostics::ResultExt;
use crate::execute::process_file::workspace_file::WorkspaceFile;
use crate::execute::process_file::{FileResult, FileStatus, Message, SharedTraversalOptions};
use crate::TraversalMode;
use biome_diagnostics::{category, Error};
use biome_service::file_handlers::{AstroFileHandler, SvelteFileHandler, VueFileHandler};
use biome_service::workspace::RuleCategories;
Expand Down Expand Up @@ -56,11 +57,17 @@ pub(crate) fn lint_with_guard<'ctx>(
}

let max_diagnostics = ctx.remaining_diagnostics.load(Ordering::Relaxed);
let rule = if let TraversalMode::Lint { rule, .. } = ctx.execution.traversal_mode() {
*rule
} else {
None
};
let pull_diagnostics_result = workspace_file
.guard()
.pull_diagnostics(
RuleCategories::LINT | RuleCategories::SYNTAX,
max_diagnostics.into(),
rule,
)
.with_file_path_and_code(
workspace_file.path.display().to_string(),
Expand Down
6 changes: 6 additions & 0 deletions crates/biome_cli/src/execute/std_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,17 @@ pub(crate) fn run<'a>(
}
}

let rule = if let TraversalMode::Lint { rule, .. } = mode.traversal_mode() {
*rule
} else {
None
};
if !mode.is_check_apply_unsafe() {
let result = workspace.pull_diagnostics(PullDiagnosticsParams {
categories: RuleCategories::LINT | RuleCategories::SYNTAX,
path: biome_path.clone(),
max_diagnostics: mode.max_diagnostics.into(),
rule,
})?;
diagnostics.extend(result.diagnostics);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl<'app> CliSession<'app> {
cli_options,
linter_configuration,
paths,
rule,
stdin_file_path,
vcs_configuration,
files_configuration,
Expand All @@ -129,6 +130,7 @@ impl<'app> CliSession<'app> {
cli_options,
linter_configuration,
paths,
rule,
stdin_file_path,
vcs_configuration,
files_configuration,
Expand Down
Loading

0 comments on commit 3d74b9f

Please sign in to comment.