feat(rome_diagnostics): add `with_severity` API
MODIFY
crates/rome_analyze/src/diagnostics.rs
crates/rome_analyze/src/diagnostics.rs
@@ -25,10 +25,7 @@ pub struct AnalyzerDiagnostic {
impl From<RuleDiagnostic> for AnalyzerDiagnostic {
fn from(rule_diagnostic: RuleDiagnostic) -> Self {
Self {
- kind: DiagnosticKind::Rule {
- rule_diagnostic,
- severity: None,
- },
+ kind: DiagnosticKind::Rule(rule_diagnostic),
code_suggestion_list: vec![],
}
}
@@ -37,12 +34,7 @@ impl From<RuleDiagnostic> for AnalyzerDiagnostic {
#[derive(Debug)]
enum DiagnosticKind {
/// It holds various info related to diagnostics emitted by the rules
- Rule {
- /// The severity of the rule
- severity: Option<Severity>,
- /// The diagnostic emitted by a rule
- rule_diagnostic: RuleDiagnostic,
- },
+ Rule(RuleDiagnostic),
/// We have raw information to create a basic [Diagnostic]
Raw(Error),
}
@@ -50,53 +42,44 @@ enum DiagnosticKind {
impl Diagnostic for AnalyzerDiagnostic {
fn category(&self) -> Option<&'static Category> {
match &self.kind {
- DiagnosticKind::Rule {
- rule_diagnostic, ..
- } => Some(rule_diagnostic.category),
+ DiagnosticKind::Rule(rule_diagnostic) => Some(rule_diagnostic.category),
DiagnosticKind::Raw(error) => error.category(),
}
}
fn description(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result {
match &self.kind {
- DiagnosticKind::Rule {
- rule_diagnostic, ..
- } => Debug::fmt(&rule_diagnostic.message, fmt),
+ DiagnosticKind::Rule(rule_diagnostic) => Debug::fmt(&rule_diagnostic.message, fmt),
DiagnosticKind::Raw(error) => error.description(fmt),
}
}
fn message(&self, fmt: &mut rome_console::fmt::Formatter<'_>) -> std::io::Result<()> {
match &self.kind {
- DiagnosticKind::Rule {
- rule_diagnostic, ..
- } => rome_console::fmt::Display::fmt(&rule_diagnostic.message, fmt),
+ DiagnosticKind::Rule(rule_diagnostic) => {
+ rome_console::fmt::Display::fmt(&rule_diagnostic.message, fmt)
+ }
DiagnosticKind::Raw(error) => error.message(fmt),
}
}
fn severity(&self) -> Severity {
match &self.kind {
- DiagnosticKind::Rule { severity, .. } => severity.unwrap_or(Severity::Error),
+ DiagnosticKind::Rule { .. } => Severity::Error,
DiagnosticKind::Raw(error) => error.severity(),
}
}
fn tags(&self) -> DiagnosticTags {
match &self.kind {
- DiagnosticKind::Rule {
- rule_diagnostic, ..
- } => rule_diagnostic.tags,
+ DiagnosticKind::Rule(rule_diagnostic) => rule_diagnostic.tags,
DiagnosticKind::Raw(error) => error.tags(),
}
}
fn location(&self) -> Location<'_> {
match &self.kind {
- DiagnosticKind::Rule {
- rule_diagnostic, ..
- } => {
- let builder = Location::builder().span(&rule_diagnostic.span);
- builder.build()
+ DiagnosticKind::Rule(rule_diagnostic) => {
+ Location::builder().span(&rule_diagnostic.span).build()
}
DiagnosticKind::Raw(error) => error.location(),
}
@@ -104,9 +87,7 @@ impl Diagnostic for AnalyzerDiagnostic {
fn advices(&self, visitor: &mut dyn Visit) -> std::io::Result<()> {
match &self.kind {
- DiagnosticKind::Rule {
- rule_diagnostic, ..
- } => rule_diagnostic.advices().record(visitor)?,
+ DiagnosticKind::Rule(rule_diagnostic) => rule_diagnostic.advices().record(visitor)?,
DiagnosticKind::Raw(error) => error.advices(visitor)?,
}
@@ -128,18 +109,9 @@ impl AnalyzerDiagnostic {
}
}
- /// Sets the severity of the current diagnostic
- pub fn set_severity(&mut self, new_severity: Severity) {
- if let DiagnosticKind::Rule { severity, .. } = &mut self.kind {
- *severity = Some(new_severity);
- }
- }
-
pub fn get_span(&self) -> Option<TextRange> {
match &self.kind {
- DiagnosticKind::Rule {
- rule_diagnostic, ..
- } => rule_diagnostic.span,
+ DiagnosticKind::Rule(rule_diagnostic) => rule_diagnostic.span,
DiagnosticKind::Raw(error) => error.location().span,
}
}
@@ -148,15 +120,9 @@ impl AnalyzerDiagnostic {
/// a automatic code fix.
pub fn add_code_suggestion(mut self, suggestion: CodeSuggestionAdvice<MarkupBuf>) -> Self {
self.kind = match self.kind {
- DiagnosticKind::Rule {
- severity,
- mut rule_diagnostic,
- } => {
+ DiagnosticKind::Rule(mut rule_diagnostic) => {
rule_diagnostic.tags = DiagnosticTags::FIXABLE;
- DiagnosticKind::Rule {
- severity,
- rule_diagnostic,
- }
+ DiagnosticKind::Rule(rule_diagnostic)
}
DiagnosticKind::Raw(error) => {
DiagnosticKind::Raw(error.with_tags(DiagnosticTags::FIXABLE))

MODIFY
crates/rome_analyze/src/matcher.rs
crates/rome_analyze/src/matcher.rs
@@ -153,7 +153,7 @@ where
#[cfg(test)]
mod tests {
- use rome_diagnostics::{category, location::FileId};
+ use rome_diagnostics::{category, location::FileId, DiagnosticExt};
use rome_diagnostics::{Diagnostic, Severity};
use rome_rowan::{
raw_language::{RawLanguage, RawLanguageKind, RawLanguageRoot, RawSyntaxTreeBuilder},
@@ -306,10 +306,10 @@ mod tests {
let mut diagnostics = Vec::new();
let mut emit_signal = |signal: &dyn AnalyzerSignal<RawLanguage>| -> ControlFlow<Never> {
- let mut diag = signal.diagnostic().expect("diagnostic");
- diag.set_severity(Severity::Warning);
- let code = diag.category().expect("code");
+ let diag = signal.diagnostic().expect("diagnostic");
let range = diag.get_span().expect("range");
+ let error = diag.with_severity(Severity::Warning);
+ let code = error.category().expect("code");
diagnostics.push((code, range));
ControlFlow::Continue(())

MODIFY
crates/rome_js_analyze/src/lib.rs
crates/rome_js_analyze/src/lib.rs
@@ -173,10 +173,12 @@ mod tests {
&options,
|signal| {
dbg!("here");
- if let Some(mut diag) = signal.diagnostic() {
- diag.set_severity(Severity::Warning);
+ if let Some(diag) = signal.diagnostic() {
error_ranges.push(diag.location().span.unwrap());
- let error = diag.with_file_path("ahahah").with_file_source_code(SOURCE);
+ let error = diag
+ .with_severity(Severity::Warning)
+ .with_file_path("ahahah")
+ .with_file_source_code(SOURCE);
let text = markup_to_string(markup! {
{PrintDiagnostic::verbose(&error)}
});
@@ -249,11 +251,10 @@ mod tests {
AnalysisFilter::default(),
&options,
|signal| {
- if let Some(mut diag) = signal.diagnostic() {
- diag.set_severity(Severity::Warning);
-
+ if let Some(diag) = signal.diagnostic() {
let span = diag.get_span();
let error = diag
+ .with_severity(Severity::Warning)
.with_file_path(FileId::zero())
.with_file_source_code(SOURCE);

MODIFY
crates/rome_js_analyze/tests/spec_tests.rs
crates/rome_js_analyze/tests/spec_tests.rs
use json_comments::StripComments;
use rome_analyze::{
- AnalysisFilter, AnalyzerAction, AnalyzerDiagnostic, AnalyzerOptions, ControlFlow, Never,
- RuleFilter,
+ AnalysisFilter, AnalyzerAction, AnalyzerOptions, ControlFlow, Never, RuleFilter,
};
use rome_console::{
fmt::{Formatter, Termcolor},
@@ -10,7 +9,7 @@ use rome_console::{
use rome_diagnostics::advice::CodeSuggestionAdvice;
use rome_diagnostics::location::FileId;
use rome_diagnostics::termcolor::NoColor;
-use rome_diagnostics::{DiagnosticExt, PrintDiagnostic, Severity};
+use rome_diagnostics::{DiagnosticExt, Error, PrintDiagnostic, Severity};
use rome_js_parser::{
parse,
test_utils::{assert_errors_are_absent, has_unknown_nodes_or_empty_slots},
@@ -91,15 +90,14 @@ fn write_analysis_to_snapshot(
let options = AnalyzerOptions::default();
rome_js_analyze::analyze(FileId::zero(), &root, filter, &options, |event| {
if let Some(mut diag) = event.diagnostic() {
- diag.set_severity(Severity::Warning);
for action in event.actions() {
if !action.is_suppression() {
check_code_action(input_file, input_code, source_type, &action);
diag = diag.add_code_suggestion(CodeSuggestionAdvice::from(action));
}
}
-
- diagnostics.push(diagnostic_to_string(file_name, input_code, diag));
+ let error = diag.with_severity(Severity::Warning);
+ diagnostics.push(diagnostic_to_string(file_name, input_code, error));
return ControlFlow::Continue(());
}
@@ -169,7 +167,7 @@ fn markup_to_string(markup: Markup) -> String {
String::from_utf8(buffer).unwrap()
}
#[allow(clippy::let_and_return)]
-fn diagnostic_to_string(name: &str, source: &str, diag: AnalyzerDiagnostic) -> String {
+fn diagnostic_to_string(name: &str, source: &str, diag: Error) -> String {
let error = diag
.with_file_path((name, FileId::zero()))
.with_file_source_code(source);

MODIFY
crates/rome_service/src/file_handlers/javascript.rs
crates/rome_service/src/file_handlers/javascript.rs
@@ -17,7 +17,7 @@ use rome_analyze::{
AnalysisFilter, AnalyzerOptions, ControlFlow, GroupCategory, Never, QueryMatch,
RegistryVisitor, RuleCategories, RuleCategory, RuleFilter, RuleGroup,
};
-use rome_diagnostics::{category, Applicability, Diagnostic, Severity};
+use rome_diagnostics::{category, Applicability, Diagnostic, DiagnosticExt, Severity};
use rome_formatter::{FormatError, Printed};
use rome_fs::RomePath;
use rome_js_analyze::utils::rename::{RenameError, RenameSymbolExtensions};
@@ -241,22 +241,22 @@ fn lint(params: LintParams) -> LintResults {
.category()
.filter(|category| category.name().starts_with("lint/"))
.and_then(|category| params.rules.as_ref()?.get_severity_from_code(category))
- .unwrap_or(Severity::Error);
+ .unwrap_or(Severity::Warning);
if severity <= Severity::Error {
errors += 1;
}
if diagnostic_count <= params.max_diagnostics {
- diagnostic.set_severity(severity);
-
for action in signal.actions() {
if !action.is_suppression() {
diagnostic = diagnostic.add_code_suggestion(action.into());
}
}
- diagnostics.push(rome_diagnostics::serde::Diagnostic::new(diagnostic));
+ let error = diagnostic.with_severity(severity);
+
+ diagnostics.push(rome_diagnostics::serde::Diagnostic::new(error));
}
}

MODIFY
website/src/playground/tabs/DiagnosticsListTab.tsx
website/src/playground/tabs/DiagnosticsListTab.tsx
@@ -99,7 +99,7 @@ export default function DiagnosticsListTab({ editorRef, diagnostics }: Props) {
<ul className="diagnostics-list">
{diagnostics.map((diag, i) => {
return (
- // rome-ignore lint(correctness/noArrayIndexKey): Diagnostic has no stable id.
+ // rome-ignore lint/correctness/noArrayIndexKey: Diagnostic has no stable id.
<DiagnosticListItem key={i} editorRef={editorRef} diagnostic={diag} />
);
})}

MODIFY
xtask/lintdoc/src/main.rs
xtask/lintdoc/src/main.rs
@@ -539,7 +539,6 @@ fn assert_lint(
let severity = settings.get_severity_from_rule_code(category).expect(
"If you see this error, it means you need to run cargo codegen-configuration",
);
- diag.set_severity(severity);
for action in signal.actions() {
if !action.is_suppression() {
@@ -548,6 +547,7 @@ fn assert_lint(
}
let error = diag
+ .with_severity(severity)
.with_file_path((file.clone(), FileId::zero()))
.with_file_source_code(code);
let res = write_diagnostic(code, error);

feat(rome_diagnostics): add `with_severity` API (#3823)