diff --git a/crates/oxc_ast/src/ast/literal.rs b/crates/oxc_ast/src/ast/literal.rs index eaf89103a1a329..6991aa2e6a55a8 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 108b9db81b5a5a..ec9dbd851eef1d 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) @@ -145,6 +173,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() { @@ -160,3 +190,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) + } +}