mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(ast): implement missing Clone, Hash, and Display traits for literals (#4552)
This commit is contained in:
parent
ba7000107e
commit
b3b70282c7
2 changed files with 45 additions and 1 deletions
|
|
@ -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> {
|
||||
|
|
|
|||
|
|
@ -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<H: Hasher>(&self, state: &mut H) {
|
||||
None::<bool>.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)
|
||||
///
|
||||
/// <https://262.ecma-international.org/5.1/#sec-9.5>
|
||||
#[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: <https://tc39.es/ecma262/multipage/abstract-operations.html#sec-isstringwellformedunicode>
|
||||
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<str> 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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue