From af1cd1c520c92d3b1b59142d1c6a4b0be211fd5b Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 12 May 2023 13:54:48 +0800 Subject: [PATCH] refactor(span): make Atom a newtype --- .../deepscan/bad_array_method_on_arguments.rs | 2 +- crates/oxc_span/src/atom.rs | 81 +++++++++++++++++++ crates/oxc_span/src/lib.rs | 6 +- 3 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 crates/oxc_span/src/atom.rs diff --git a/crates/oxc_linter/src/rules/deepscan/bad_array_method_on_arguments.rs b/crates/oxc_linter/src/rules/deepscan/bad_array_method_on_arguments.rs index 354359283..8317e4e5e 100644 --- a/crates/oxc_linter/src/rules/deepscan/bad_array_method_on_arguments.rs +++ b/crates/oxc_linter/src/rules/deepscan/bad_array_method_on_arguments.rs @@ -83,7 +83,7 @@ impl Rule for BadArrayMethodOnArguments { && ARRAY_METHODS.binary_search(&name).is_ok() { ctx.diagnostic(BadArrayMethodOnArgumentsDiagnostic( - Atom::new(name), + Atom::from(name), expr.span, )); } diff --git a/crates/oxc_span/src/atom.rs b/crates/oxc_span/src/atom.rs new file mode 100644 index 000000000..ae5524a55 --- /dev/null +++ b/crates/oxc_span/src/atom.rs @@ -0,0 +1,81 @@ +use std::{borrow::Borrow, fmt, ops::Deref}; + +use compact_str::CompactString; +#[cfg(feature = "serde")] +use serde::Serialize; + +/// Newtype for [`CompactString`] +#[derive(Clone, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize))] +pub struct Atom(CompactString); + +impl Atom { + #[inline] + pub fn as_str(&self) -> &str { + self.0.as_str() + } +} + +impl Deref for Atom { + type Target = str; + + fn deref(&self) -> &Self::Target { + self.0.as_str() + } +} + +impl<'a> From<&'a str> for Atom { + fn from(s: &'a str) -> Self { + Self(s.into()) + } +} + +impl From for Atom { + fn from(s: String) -> Self { + Self(s.into()) + } +} + +impl AsRef for Atom { + #[inline] + fn as_ref(&self) -> &str { + self.0.as_str() + } +} + +impl Borrow for Atom { + #[inline] + fn borrow(&self) -> &str { + self.0.as_str() + } +} + +impl> PartialEq for Atom { + fn eq(&self, other: &T) -> bool { + self.0.as_str() == other.as_ref() + } +} + +impl PartialEq for String { + fn eq(&self, other: &Atom) -> bool { + self.as_str() == other.0.as_str() + } +} + +impl PartialEq for &str { + fn eq(&self, other: &Atom) -> bool { + *self == other.0.as_str() + } +} + +impl fmt::Debug for Atom { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(self.0.as_str(), f) + } +} + +impl fmt::Display for Atom { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.0.as_str(), f) + } +} diff --git a/crates/oxc_span/src/lib.rs b/crates/oxc_span/src/lib.rs index 54213c2c6..4ba6c653a 100644 --- a/crates/oxc_span/src/lib.rs +++ b/crates/oxc_span/src/lib.rs @@ -1,13 +1,11 @@ +mod atom; use std::hash::{Hash, Hasher}; -use compact_str::CompactString; +pub use atom::Atom; use miette::{SourceOffset, SourceSpan}; #[cfg(feature = "serde")] use serde::Serialize; -/// Type alias for [`CompactString`] -pub type Atom = CompactString; - /// Newtype for working with text ranges /// /// See the [`text-size`](https://docs.rs/text-size) crate for details.