Skip to content

Commit

Permalink
feat: flag to suppress existing diagnostics (#4008)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
  • Loading branch information
anthonyshew and ematipico authored Oct 21, 2024
1 parent 4cf4c23 commit 03e48b8
Show file tree
Hide file tree
Showing 20 changed files with 402 additions and 25 deletions.
1 change: 1 addition & 0 deletions crates/biome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl CommandRunner for CheckCommandPayload {
apply: self.apply,
apply_unsafe: self.apply_unsafe,
write: self.write,
suppress: false,
fix: self.fix,
unsafe_: self.unsafe_,
},
Expand Down
3 changes: 3 additions & 0 deletions crates/biome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub(crate) struct LintCommandPayload {
pub(crate) write: bool,
pub(crate) fix: bool,
pub(crate) unsafe_: bool,
pub(crate) suppress: bool,
pub(crate) linter_configuration: Option<PartialLinterConfiguration>,
pub(crate) vcs_configuration: Option<PartialVcsConfiguration>,
pub(crate) files_configuration: Option<PartialFilesConfiguration>,
Expand Down Expand Up @@ -136,6 +137,7 @@ impl CommandRunner for LintCommandPayload {
write: self.write,
fix: self.fix,
unsafe_: self.unsafe_,
suppress: self.suppress,
},
console,
)?;
Expand All @@ -145,6 +147,7 @@ impl CommandRunner for LintCommandPayload {
only: self.only.clone(),
skip: self.skip.clone(),
vcs_targeted: (self.staged, self.changed).into(),
suppress: self.suppress,
})
.set_report(cli_options))
}
Expand Down
1 change: 1 addition & 0 deletions crates/biome_cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl CommandRunner for MigrateCommandPayload {
write: self.write,
fix: self.fix,
unsafe_: false,
suppress: false,
})
}

Expand Down
55 changes: 38 additions & 17 deletions crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ pub enum BiomeCommand {
#[bpaf(long("write"), switch)]
write: bool,

/// Fix diagnostics with suppression comments if the language supports it.
#[bpaf(long("suppress"))]
suppress: bool,

/// Allow to do unsafe fixes, should be used with `--write` or `--fix`
#[bpaf(long("unsafe"), switch)]
unsafe_: bool,
Expand Down Expand Up @@ -703,6 +707,7 @@ pub(crate) struct FixFileModeOptions {
apply: bool,
apply_unsafe: bool,
write: bool,
suppress: bool,
fix: bool,
unsafe_: bool,
}
Expand All @@ -719,6 +724,7 @@ pub(crate) fn determine_fix_file_mode(
apply_unsafe,
write,
fix,
suppress,
unsafe_,
} = options;

Expand All @@ -743,6 +749,8 @@ pub(crate) fn determine_fix_file_mode(
Ok(Some(FixFileMode::SafeAndUnsafeFixes))
} else if safe_fixes {
Ok(Some(FixFileMode::SafeFixes))
} else if suppress {
Ok(Some(FixFileMode::ApplySuppressions))
} else {
Ok(None)
}
Expand All @@ -754,6 +762,7 @@ fn check_fix_incompatible_arguments(options: FixFileModeOptions) -> Result<(), C
apply,
apply_unsafe,
write,
suppress,
fix,
unsafe_,
} = options;
Expand All @@ -779,6 +788,13 @@ fn check_fix_incompatible_arguments(options: FixFileModeOptions) -> Result<(), C
));
} else if write && fix {
return Err(CliDiagnostic::incompatible_arguments("--write", "--fix"));
} else if suppress && write {
return Err(CliDiagnostic::incompatible_arguments(
"--suppress",
"--write",
));
} else if suppress && fix {
return Err(CliDiagnostic::incompatible_arguments("--suppress", "--fix"));
}
Ok(())
}
Expand Down Expand Up @@ -967,19 +983,20 @@ mod tests {

#[test]
fn incompatible_arguments() {
for (apply, apply_unsafe, write, fix, unsafe_) in [
(true, true, false, false, false), // --apply --apply-unsafe
(true, false, true, false, false), // --apply --write
(true, false, false, true, false), // --apply --fix
(false, true, false, false, true), // --apply-unsafe --unsafe
(false, true, true, false, false), // --apply-unsafe --write
(false, true, false, true, false), // --apply-unsafe --fix
(false, false, true, true, false), // --write --fix
for (apply, apply_unsafe, write, suppress, fix, unsafe_) in [
(true, true, false, false, false, false), // --apply --apply-unsafe
(true, false, true, false, false, false), // --apply --write
(true, false, false, false, true, false), // --apply --fix
(false, true, false, false, false, true), // --apply-unsafe --unsafe
(false, true, true, false, false, false), // --apply-unsafe --write
(false, true, false, false, true, false), // --apply-unsafe --fix
(false, false, true, false, true, false), // --write --fix
] {
assert!(check_fix_incompatible_arguments(FixFileModeOptions {
apply,
apply_unsafe,
write,
suppress,
fix,
unsafe_
})
Expand All @@ -991,17 +1008,18 @@ mod tests {
fn safe_fixes() {
let mut console = BufferConsole::default();

for (apply, apply_unsafe, write, fix, unsafe_) in [
(true, false, false, false, false), // --apply
(false, false, true, false, false), // --write
(false, false, false, true, false), // --fix
for (apply, apply_unsafe, write, suppress, fix, unsafe_) in [
(true, false, false, false, false, false), // --apply
(false, false, true, false, false, false), // --write
(false, false, false, false, true, false), // --fix
] {
assert_eq!(
determine_fix_file_mode(
FixFileModeOptions {
apply,
apply_unsafe,
write,
suppress,
fix,
unsafe_
},
Expand All @@ -1017,17 +1035,18 @@ mod tests {
fn safe_and_unsafe_fixes() {
let mut console = BufferConsole::default();

for (apply, apply_unsafe, write, fix, unsafe_) in [
(false, true, false, false, false), // --apply-unsafe
(false, false, true, false, true), // --write --unsafe
(false, false, false, true, true), // --fix --unsafe
for (apply, apply_unsafe, write, suppress, fix, unsafe_) in [
(false, true, false, false, false, false), // --apply-unsafe
(false, false, true, false, false, true), // --write --unsafe
(false, false, false, false, true, true), // --fix --unsafe
] {
assert_eq!(
determine_fix_file_mode(
FixFileModeOptions {
apply,
apply_unsafe,
write,
suppress,
fix,
unsafe_
},
Expand All @@ -1043,13 +1062,15 @@ mod tests {
fn no_fix() {
let mut console = BufferConsole::default();

let (apply, apply_unsafe, write, fix, unsafe_) = (false, false, false, false, false);
let (apply, apply_unsafe, write, suppress, fix, unsafe_) =
(false, false, false, false, false, false);
assert_eq!(
determine_fix_file_mode(
FixFileModeOptions {
apply,
apply_unsafe,
write,
suppress,
fix,
unsafe_
},
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_cli/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ pub enum TraversalMode {
skip: Vec<RuleSelector>,
/// A flag to know vcs integrated options such as `--staged` or `--changed` are enabled
vcs_targeted: VcsTargeted,
/// Supress existing diagnostics with a `// biome-ignore` comment
suppress: bool,
},
/// This mode is enabled when running the command `biome ci`
CI {
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 @@ -130,6 +130,7 @@ impl<'app> CliSession<'app> {
apply,
apply_unsafe,
write,
suppress,
fix,
unsafe_,
cli_options,
Expand All @@ -154,6 +155,7 @@ impl<'app> CliSession<'app> {
apply_unsafe,
apply,
write,
suppress,
fix,
unsafe_,
linter_configuration,
Expand Down
1 change: 1 addition & 0 deletions crates/biome_cli/tests/cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ mod reporter_github;
mod reporter_gitlab;
mod reporter_junit;
mod reporter_summary;
mod suppressions;
mod unknown_files;
Loading

0 comments on commit 03e48b8

Please sign in to comment.