Skip to content

Commit

Permalink
feat(analyzer): top-level suppressions
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Oct 17, 2024
1 parent f557e5c commit fbd3c8c
Show file tree
Hide file tree
Showing 21 changed files with 714 additions and 373 deletions.
23 changes: 23 additions & 0 deletions .changeset/new_top_level_suppression_for_the_analyzer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
cli: minor
---

# New top-level suppression for the analyzer

The Biome analyzer now supports a new top-level suppression. These suppression have to be placed at the top of the file, and they **must** be multiple line comments.

The analyzer rules specified inside the block comment will be suppressed for the whole file.

In the example, we suppress the rules `lint/style/useConst` and `lint/suspicious/noDebugger` for the whole file:

```js
// main.js
/**
* biome-ignore lint/style/useConst: i like let
* biome-ignore lint/suspicious/noDebugger: needed now
*/

let path = "/path";
let _tmp = undefined;
debugger
```
13 changes: 13 additions & 0 deletions .changeset/remove_support_for_legacy_suppressions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
cli: major
---

# Remove support for legacy suppressions

Biome used to support "legacy suppressions" that looked like this:

```js
// biome-ignore lint(style/useWhile): reason
```

This format is no longer supported.
1 change: 1 addition & 0 deletions Cargo.lock

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

33 changes: 27 additions & 6 deletions crates/biome_analyze/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use biome_console::MarkupBuf;
use biome_console::{markup, MarkupBuf};
use biome_diagnostics::{
advice::CodeSuggestionAdvice, category, Advices, Category, Diagnostic, DiagnosticExt,
DiagnosticTags, Error, Location, Severity, Visit,
DiagnosticTags, Error, Location, LogCategory, Severity, Visit,
};
use biome_rowan::TextRange;
use std::borrow::Cow;
Expand Down Expand Up @@ -141,7 +141,7 @@ impl AnalyzerDiagnostic {

#[derive(Debug, Diagnostic, Clone)]
#[diagnostic(severity = Warning)]
pub struct SuppressionDiagnostic {
pub struct AnalyzerSuppressionDiagnostic {
#[category]
category: &'static Category,
#[location(span)]
Expand All @@ -151,9 +151,12 @@ pub struct SuppressionDiagnostic {
message: String,
#[tags]
tags: DiagnosticTags,

#[advice]
advice: SuppressionAdvice,
}

impl SuppressionDiagnostic {
impl AnalyzerSuppressionDiagnostic {
pub(crate) fn new(
category: &'static Category,
range: TextRange,
Expand All @@ -164,15 +167,33 @@ impl SuppressionDiagnostic {
range,
message: message.to_string(),
tags: DiagnosticTags::empty(),
advice: SuppressionAdvice::default(),
}
}

pub(crate) fn with_tags(mut self, tags: DiagnosticTags) -> Self {
self.tags |= tags;
pub(crate) fn note(mut self, message: impl Into<String>, range: impl Into<TextRange>) -> Self {
self.advice.messages.push((message.into(), range.into()));
self
}
}

#[derive(Debug, Default, Clone)]
struct SuppressionAdvice {
messages: Vec<(String, TextRange)>,
}

impl Advices for SuppressionAdvice {
fn record(&self, visitor: &mut dyn Visit) -> std::io::Result<()> {
for (message, range) in &self.messages {
visitor.record_log(LogCategory::Info, &markup! {{message}})?;
let location = Location::builder().span(range);

visitor.record_frame(location.build())?
}
Ok(())
}
}

/// Series of errors encountered when running rules on a file
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down
Loading

0 comments on commit fbd3c8c

Please sign in to comment.