perf(diagnostic): use Cow<'static, str> over String (#4175)

Allows us to use `&'static str` for  error and help messages without allocating them into `String`s.
This commit is contained in:
DonIsaac 2024-07-11 01:44:13 +00:00
parent 7a059ab53a
commit ddfa343475
2 changed files with 7 additions and 6 deletions

View file

@ -7,6 +7,7 @@ mod reporter;
mod service; mod service;
use std::{ use std::{
borrow::Cow,
fmt::{self, Display}, fmt::{self, Display},
ops::Deref, ops::Deref,
}; };
@ -42,9 +43,9 @@ impl Deref for OxcDiagnostic {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct OxcDiagnosticInner { pub struct OxcDiagnosticInner {
pub message: String, pub message: Cow<'static, str>,
pub labels: Option<Vec<LabeledSpan>>, pub labels: Option<Vec<LabeledSpan>>,
pub help: Option<String>, pub help: Option<Cow<'static, str>>,
pub severity: Severity, pub severity: Severity,
} }
@ -76,7 +77,7 @@ impl Diagnostic for OxcDiagnostic {
impl OxcDiagnostic { impl OxcDiagnostic {
#[must_use] #[must_use]
pub fn error<T: Into<String>>(message: T) -> Self { pub fn error<T: Into<Cow<'static, str>>>(message: T) -> Self {
Self { Self {
inner: Box::new(OxcDiagnosticInner { inner: Box::new(OxcDiagnosticInner {
message: message.into(), message: message.into(),
@ -88,7 +89,7 @@ impl OxcDiagnostic {
} }
#[must_use] #[must_use]
pub fn warn<T: Into<String>>(message: T) -> Self { pub fn warn<T: Into<Cow<'static, str>>>(message: T) -> Self {
Self { Self {
inner: Box::new(OxcDiagnosticInner { inner: Box::new(OxcDiagnosticInner {
message: message.into(), message: message.into(),
@ -106,7 +107,7 @@ impl OxcDiagnostic {
} }
#[must_use] #[must_use]
pub fn with_help<T: Into<String>>(mut self, help: T) -> Self { pub fn with_help<T: Into<Cow<'static, str>>>(mut self, help: T) -> Self {
self.inner.help = Some(help.into()); self.inner.help = Some(help.into());
self self
} }

View file

@ -11,7 +11,7 @@ fn no_optional_chaining_diagnostic(span0: Span, x1: &str) -> OxcDiagnostic {
.with_label(span0) .with_label(span0)
} else { } else {
OxcDiagnostic::warn("oxc(no-optional-chaining): Optional chaining is not allowed.") OxcDiagnostic::warn("oxc(no-optional-chaining): Optional chaining is not allowed.")
.with_help(x1) .with_help(x1.to_owned())
.with_label(span0) .with_label(span0)
} }
} }