Skip to content
Merged
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: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ lazy-regex = "3.4.2" # Lazy regex compilation
log = "0.4.29" # Logging facade
markdown = "1.0.0" # Markdown parsing
memchr = "2.7.6" # Fast byte searching
miette = { package = "oxc-miette", version = "2.6.0", features = [
miette = { package = "oxc-miette", version = "2.7.0", features = [
"fancy-no-syscall",
] } # Error reporting
mimalloc-safe = "0.1.55" # Fast allocator
Expand Down
30 changes: 30 additions & 0 deletions crates/oxc_diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub struct OxcDiagnosticInner {
pub message: Cow<'static, str>,
pub labels: Option<Vec<LabeledSpan>>,
pub help: Option<Cow<'static, str>>,
pub note: Option<Cow<'static, str>>,
pub severity: Severity,
pub code: OxcCode,
pub url: Option<Cow<'static, str>>,
Expand All @@ -138,6 +139,14 @@ impl Diagnostic for OxcDiagnostic {
self.help.as_ref().map(Box::new).map(|c| c as Box<dyn Display>)
}

/// A note for the diagnostic.
///
/// Similar to rustc - intended for additional explanation and information,
/// e.g. why an error was emitted, how to turn it off.
fn note<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.note.as_ref().map(Box::new).map(|c| c as Box<dyn Display>)
}

/// The severity level of this diagnostic.
///
/// Diagnostics with missing severity levels should be treated as [errors](Severity::Error).
Expand Down Expand Up @@ -174,6 +183,7 @@ impl OxcDiagnostic {
inner: Box::new(OxcDiagnosticInner {
message: message.into(),
labels: None,
note: None,
help: None,
severity: Severity::Error,
code: OxcCode::default(),
Expand All @@ -189,6 +199,7 @@ impl OxcDiagnostic {
message: message.into(),
labels: None,
help: None,
note: None,
severity: Severity::Warning,
code: OxcCode::default(),
url: None,
Expand Down Expand Up @@ -269,6 +280,25 @@ impl OxcDiagnostic {
self
}

/// Show a note to the user.
///
/// ## Example
/// ```
/// use std::path::PathBuf;
/// use oxc_diagnostics::OxcDiagnostic
///
/// let config_file_path = Path::from("config.json");
/// if !config_file_path.exists() {
/// return Err(OxcDiagnostic::error("No config file found")
/// .with_help("Run my_tool --init to set up a new config file")
/// .with_note("Some useful information or suggestion"));
/// }
/// ```
pub fn with_note<T: Into<Cow<'static, str>>>(mut self, note: T) -> Self {
self.inner.note = Some(note.into());
self
}

/// Set the label covering a problematic portion of source code.
///
/// Existing labels will be removed. Use [`OxcDiagnostic::and_label`] append a label instead.
Expand Down
19 changes: 12 additions & 7 deletions crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use crate::{

fn reduce_likely_array_spread_diagnostic(spread_span: Span, reduce_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not spread accumulators in Array.prototype.reduce()")
.with_help("It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead.\nUsing spreads within accumulators leads to `O(n^2)` time complexity.")
.with_help("It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead.")
.with_note("Using spreads within accumulators leads to `O(n^2)` time complexity.")
.with_labels([
spread_span.label("From this spread"),
reduce_span.label("For this reduce")
Expand All @@ -28,7 +29,8 @@ fn reduce_likely_array_spread_diagnostic(spread_span: Span, reduce_span: Span) -

fn reduce_likely_object_spread_diagnostic(spread_span: Span, reduce_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not spread accumulators in Array.prototype.reduce()")
.with_help("It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead.\nUsing spreads within accumulators leads to `O(n^2)` time complexity.")
.with_help("It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead.")
.with_note("Using spreads within accumulators leads to `O(n^2)` time complexity.")
.with_labels([
spread_span.label("From this spread"),
reduce_span.label("For this reduce")
Expand All @@ -37,7 +39,8 @@ fn reduce_likely_object_spread_diagnostic(spread_span: Span, reduce_span: Span)

fn reduce_unknown(spread_span: Span, reduce_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not spread accumulators in Array.prototype.reduce()")
.with_help("Consider using `Object.assign()` or `Array.prototype.push()` to mutate the accumulator instead.\nUsing spreads within accumulators leads to `O(n^2)` time complexity.")
.with_help("Consider using `Object.assign()` or `Array.prototype.push()` to mutate the accumulator instead.")
.with_note("Using spreads within accumulators leads to `O(n^2)` time complexity.")
.with_labels([
spread_span.label("From this spread"),
reduce_span.label("For this reduce")
Expand All @@ -50,11 +53,12 @@ fn loop_spread_likely_object_diagnostic(
loop_span: Span,
) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not spread accumulators in loops")
.with_help("Consider using `Object.assign()` to mutate the accumulator instead.\nUsing spreads within accumulators leads to `O(n^2)` time complexity.")
.with_help("Consider using `Object.assign()` to mutate the accumulator instead.")
.with_note("Using spreads within accumulators leads to `O(n^2)` time complexity.")
.with_labels([
accumulator_decl_span.label("From this accumulator"),
spread_span.label("From this spread"),
loop_span.label("For this loop")
loop_span.label("For this loop"),
])
}
fn loop_spread_likely_array_diagnostic(
Expand All @@ -63,11 +67,12 @@ fn loop_spread_likely_array_diagnostic(
loop_span: Span,
) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not spread accumulators in loops")
.with_help("Consider using `Array.prototype.push()` to mutate the accumulator instead.\nUsing spreads within accumulators leads to `O(n^2)` time complexity.")
.with_help("Consider using `Array.prototype.push()` to mutate the accumulator instead.")
.with_note("Using spreads within accumulators leads to `O(n^2)` time complexity.")
.with_labels([
accumulator_decl_span.label("From this accumulator"),
spread_span.label("From this spread"),
loop_span.label("For this loop")
loop_span.label("For this loop"),
])
}

Expand Down
Loading
Loading