Loading...
Loading...
Guide for creating high-quality, user-friendly diagnostics in Biome. Use when implementing error messages, warnings, and code frame displays. Examples:<example>User needs to create a diagnostic for a lint rule</example><example>User wants to add helpful advice to error messages</example><example>User is improving diagnostic quality</example>
npx skill4agent add biomejs/biome diagnostics-developmentDiagnosticcrates/biome_diagnostics/CONTRIBUTING.md#[derive(Diagnostic)]use biome_diagnostics::{Diagnostic, category};
#[derive(Debug, Diagnostic)]
#[diagnostic(
severity = Error,
category = "lint/correctness/noVar"
)]
struct NoVarDiagnostic {
#[location(span)]
span: TextRange,
#[message]
#[description]
message: MessageAndDescription,
#[advice]
advice: NoVarAdvice,
}
#[derive(Debug)]
struct MessageAndDescription;
impl fmt::Display for MessageAndDescription {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Use 'let' or 'const' instead of 'var'")
}
}Advicesuse biome_diagnostics::{Advices, Visit};
use biome_console::markup;
struct NoVarAdvice {
is_const_candidate: bool,
}
impl Advices for NoVarAdvice {
fn record(&self, visitor: &mut dyn Visit) -> std::io::Result<()> {
if self.is_const_candidate {
visitor.record_log(
LogCategory::Info,
&markup! {
"This variable is never reassigned, use 'const' instead."
}
)?;
} else {
visitor.record_log(
LogCategory::Info,
&markup! {
"Variables declared with 'var' are function-scoped, use 'let' for block-scoping."
}
)?;
}
Ok(())
}
}use biome_diagnostics::v2::{
LogAdvice, CodeFrameAdvice, DiffAdvice, CommandAdvice
};
// Log advice - simple text
LogAdvice {
category: LogCategory::Info,
message: markup! { "Consider using arrow functions." }
}
// Code frame advice - highlight code location
CodeFrameAdvice {
location: node.text_range(),
source_code: ctx.source_code(),
annotation: markup! { "This code is problematic" },
}
// Diff advice - show before/after
DiffAdvice {
old: "var x = 1;",
new: "const x = 1;",
}
// Command advice - suggest CLI command
CommandAdvice {
command: "biome check --apply",
message: markup! { "Run this command to fix automatically" },
}use biome_analyze::{Rule, RuleDiagnostic};
impl Rule for NoVar {
fn diagnostic(ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> {
let node = ctx.query();
Some(
RuleDiagnostic::new(
rule_category!(),
node.range(),
markup! {
"Use "<Emphasis>"let"</Emphasis>" or "<Emphasis>"const"</Emphasis>" instead of "<Emphasis>"var"</Emphasis>"."
},
)
.note(markup! {
"Variables declared with "<Emphasis>"var"</Emphasis>" are function-scoped, not block-scoped."
})
.note(markup! {
"See the "<Hyperlink href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/var">"MDN documentation"</Hyperlink>" for more details."
})
)
}
}use biome_console::markup;
markup! {
// Emphasis (bold/colored)
"Use "<Emphasis>"const"</Emphasis>" instead."
// Code/identifiers
"The variable "<Emphasis>{variable_name}</Emphasis>" is never used."
// Hyperlinks
"See the "<Hyperlink href="https://example.com">"documentation"</Hyperlink>"."
// Interpolation
"Found "{count}" issues."
}crates/biome_diagnostics_categories/src/categories.rsdefine_categories! {
// Existing categories...
"lint/correctness/noVar": "https://biomejs.dev/linter/rules/no-var",
"lint/style/useConst": "https://biomejs.dev/linter/rules/use-const",
}#[derive(Debug, Diagnostic)]
#[diagnostic(severity = Warning)]
struct ComplexDiagnostic {
#[location(span)]
span: TextRange,
#[message]
message: &'static str,
// Multiple advices
#[advice]
first_advice: LogAdvice,
#[advice]
code_frame: CodeFrameAdvice,
#[verbose_advice]
verbose_help: LogAdvice,
}#[derive(Debug, Diagnostic)]
#[diagnostic(
severity = Warning,
tags(FIXABLE, DEPRECATED_CODE) // Add diagnostic tags
)]
struct MyDiagnostic {
// ...
}FIXABLEINTERNALUNNECESSARY_CODEDEPRECATED_CODE// ✅ Specific and actionable
"Use 'let' or 'const' instead of 'var'"
// ✅ Explains why
"This variable is never reassigned, consider using 'const'"
// ✅ Shows what to do
"Remove the unused import statement"// ❌ Too vague
"Invalid syntax"
// ❌ Just states the obvious
"Variable declared with 'var'"
// ❌ No guidance
"This code has a problem"// ✅ Good - shows code frame
CodeFrameAdvice {
location: node.range(),
source_code: source,
annotation: markup! { "This expression is always truthy" }
}
// ❌ Less helpful - just text
LogAdvice {
message: markup! { "The expression at line 5 is always truthy" }
}// ✅ Good - shows exact change
DiffAdvice {
old: "var x = 1;",
new: "const x = 1;",
}
// ❌ Less helpful - describes change
LogAdvice {
message: markup! { "Change 'var' to 'const'" }
}// Fatal - Biome can't continue
severity = Fatal
// Error - Must be fixed (correctness, security, a11y)
severity = Error
// Warning - Should be fixed (suspicious code)
severity = Warning
// Information - Style suggestions
severity = Information
// Hint - Minor improvements
severity = Hint// Pattern 1: Simple diagnostic with note
RuleDiagnostic::new(
rule_category!(),
node.range(),
markup! { "Main message" },
)
.note(markup! { "Additional context" })
// Pattern 2: Diagnostic with code frame
RuleDiagnostic::new(
rule_category!(),
node.range(),
markup! { "Main message" },
)
.detail(
node.syntax().text_range(),
markup! { "This part is problematic" }
)
// Pattern 3: Diagnostic with link
RuleDiagnostic::new(
rule_category!(),
node.range(),
markup! { "Main message" },
)
.note(markup! {
"See "<Hyperlink href="https://biomejs.dev/linter">"documentation"</Hyperlink>"."
})
// Pattern 4: Conditional advice
impl Advices for MyAdvice {
fn record(&self, visitor: &mut dyn Visit) -> std::io::Result<()> {
if self.show_hint {
visitor.record_log(
LogCategory::Info,
&markup! { "Hint: ..." }
)?;
}
Ok(())
}
}area/group/ruleNamelint/correctness/noVarmarkup!categories.rscrates/biome_diagnostics/CONTRIBUTING.mdcrates/biome_diagnostics/src/diagnostic.rscrates/biome_diagnostics/src/v2/#[derive(Diagnostic)]