Skip to content

feat(service): sort diagnostics by severity in descending order #5534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/biome_service/src/workspace/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use biome_diagnostics::{Diagnostic as _, Error, Severity};
use biome_fs::{BiomePath, PathInterner, TraversalContext, TraversalScope};
use camino::Utf8Path;
use crossbeam::channel::{Receiver, Sender, unbounded};
use std::cmp::Reverse;
use std::collections::BTreeSet;
use std::panic::catch_unwind;
use std::sync::RwLock;
Expand Down Expand Up @@ -191,6 +192,9 @@ impl DiagnosticsCollector {
}
}

// Sort diagnostics by severity to put the most severe diagnostics first.
diagnostics.sort_by_key(|d| Reverse(d.severity()));

diagnostics
}
}
Expand Down Expand Up @@ -281,3 +285,7 @@ fn open_file(ctx: &ScanContext, path: &BiomePath) {
}
}
}

#[cfg(test)]
#[path = "scanner.tests.rs"]
mod tests;
47 changes: 47 additions & 0 deletions crates/biome_service/src/workspace/scanner.tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use biome_diagnostics::Diagnostic;
use biome_diagnostics::diagnostic::Severity;
use biome_diagnostics::serde::Diagnostic as SerdeDiagnostic;
use crossbeam::channel::unbounded;

use super::*;

#[derive(Debug, Diagnostic)]
#[diagnostic(
category = "lint/style/noShoutyConstants",
tags(FIXABLE),
message = "Test diagnostic message"
)]
struct TestDiagnostic {
#[severity]
severity: Severity,
}

#[test]
fn test_diagnostics_collector_sorting() {
let collector = DiagnosticsCollector::new();
let (sender, receiver) = unbounded();

// Send diagnostics with different severities
let warning = SerdeDiagnostic::new(TestDiagnostic {
severity: Severity::Warning,
});
let error = SerdeDiagnostic::new(TestDiagnostic {
severity: Severity::Error,
});
let info = SerdeDiagnostic::new(TestDiagnostic {
severity: Severity::Information,
});

// Send in an order different from severity
sender.send(info).unwrap();
sender.send(warning).unwrap();
sender.send(error).unwrap();
drop(sender);

let result = collector.run(receiver);

// Verify diagnostics are sorted by severity (error > warning > info)
assert_eq!(result[0].severity(), Severity::Error);
assert_eq!(result[1].severity(), Severity::Warning);
assert_eq!(result[2].severity(), Severity::Information);
}