From b3b70282c7cdf8bd16a5c1972547af08f60f9ce9 Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Sat, 3 Aug 2024 17:07:03 +0000 Subject: [PATCH] feat(ast): implement missing Clone, Hash, and Display traits for literals (#4552) --- crates/oxc_ast/src/ast/literal.rs | 2 +- crates/oxc_ast/src/ast_impl/literal.rs | 44 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/crates/oxc_ast/src/ast/literal.rs b/crates/oxc_ast/src/ast/literal.rs index eaf89103a1a32..6991aa2e6a55a 100644 --- a/crates/oxc_ast/src/ast/literal.rs +++ b/crates/oxc_ast/src/ast/literal.rs @@ -64,7 +64,7 @@ pub struct NumericLiteral<'a> { /// BigInt literal #[ast(visit)] -#[derive(Debug, Hash)] +#[derive(Debug, Clone, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize, Tsify))] #[serde(tag = "type")] pub struct BigIntLiteral<'a> { diff --git a/crates/oxc_ast/src/ast_impl/literal.rs b/crates/oxc_ast/src/ast_impl/literal.rs index aa474075f73fc..9a6c554bc6651 100644 --- a/crates/oxc_ast/src/ast_impl/literal.rs +++ b/crates/oxc_ast/src/ast_impl/literal.rs @@ -27,7 +27,15 @@ impl BooleanLiteral { } } +impl fmt::Display for BooleanLiteral { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.as_str().fmt(f) + } +} + impl Hash for NullLiteral { + #[inline] fn hash(&self, state: &mut H) { None::.hash(state); } @@ -39,12 +47,20 @@ impl NullLiteral { } } +impl fmt::Display for NullLiteral { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + "null".fmt(f) + } +} + impl<'a> NumericLiteral<'a> { pub fn new(span: Span, value: f64, raw: &'a str, base: NumberBase) -> Self { Self { span, value, raw, base } } /// port from [closure compiler](https://github.com/google/closure-compiler/blob/a4c880032fba961f7a6c06ef99daa3641810bfdd/src/com/google/javascript/jscomp/base/JSCompDoubles.java#L113) + /// /// #[allow(clippy::cast_possible_truncation)] // for `as i32` pub fn ecmascript_to_int32(num: f64) -> i32 { @@ -78,12 +94,24 @@ impl<'a> Hash for NumericLiteral<'a> { } } +impl<'a> fmt::Display for NumericLiteral<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.raw.fmt(f) + } +} + impl<'a> BigIntLiteral<'a> { pub fn is_zero(&self) -> bool { self.raw == "0n" } } +impl<'a> fmt::Display for BigIntLiteral<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.raw.fmt(f) + } +} + impl<'a> fmt::Display for RegExp<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "/{}/{}", self.pattern, self.flags) @@ -163,6 +191,8 @@ impl<'a> StringLiteral<'a> { /// Static Semantics: `IsStringWellFormedUnicode` /// test for \uD800-\uDFFF + /// + /// See: pub fn is_string_well_formed_unicode(&self) -> bool { let mut chars = self.value.chars(); while let Some(c) = chars.next() { @@ -178,3 +208,17 @@ impl<'a> StringLiteral<'a> { true } } + +impl<'a> AsRef for StringLiteral<'a> { + #[inline] + fn as_ref(&self) -> &str { + self.value.as_ref() + } +} + +impl<'a> fmt::Display for StringLiteral<'a> { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.value.fmt(f) + } +}