Skip to content

Commit

Permalink
feat(ast): implement missing Clone, Hash, and Display traits for lite…
Browse files Browse the repository at this point in the history
…rals
  • Loading branch information
DonIsaac committed Jul 30, 2024
1 parent c32cfea commit 7edcb0e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
44 changes: 44 additions & 0 deletions crates/oxc_ast/src/ast_impl/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -145,6 +173,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() {
Expand All @@ -160,3 +190,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)
}
}

0 comments on commit 7edcb0e

Please sign in to comment.