Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ast): implement missing Clone, Hash, and Display traits for literals #4552

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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() {
Expand All @@ -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)
}
}