Skip to content

Commit

Permalink
feat(ast/estree)!: replace serde with custom ESTree serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel authored and Boshen committed Feb 20, 2025
1 parent 46b9151 commit beea0cc
Show file tree
Hide file tree
Showing 38 changed files with 3,784 additions and 2,680 deletions.
11 changes: 5 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/oxc_allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ workspace = true
doctest = false

[dependencies]
oxc_estree = { workspace = true, optional = true }

allocator-api2 = { workspace = true }
bumpalo = { workspace = true, features = ["allocator-api2", "collections"] }
hashbrown = { workspace = true, default-features = false, features = ["inline-more", "allocator-api2"] }
Expand All @@ -32,4 +34,4 @@ serde = { workspace = true }
serde_json = { workspace = true }

[features]
serialize = ["dep:serde"]
serialize = ["dep:serde", "oxc_estree/serialize"]
30 changes: 26 additions & 4 deletions crates/oxc_allocator/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use std::{
};

#[cfg(any(feature = "serialize", test))]
use serde::{Serialize, Serializer};
use oxc_estree::{ESTree, Serializer as ESTreeSerializer};
#[cfg(any(feature = "serialize", test))]
use serde::{Serialize, Serializer as SerdeSerializer};

use crate::Allocator;

Expand Down Expand Up @@ -183,11 +185,18 @@ impl<T: ?Sized + Debug> Debug for Box<'_, T> {

#[cfg(any(feature = "serialize", test))]
impl<T: Serialize> Serialize for Box<'_, T> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
fn serialize<S: SerdeSerializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.deref().serialize(serializer)
}
}

#[cfg(any(feature = "serialize", test))]
impl<T: ESTree> ESTree for Box<'_, T> {
fn serialize<S: ESTreeSerializer>(&self, serializer: S) {
self.deref().serialize(serializer);
}
}

impl<T: Hash> Hash for Box<'_, T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
Expand Down Expand Up @@ -238,8 +247,21 @@ mod test {
fn box_serialize() {
let allocator = Allocator::default();
let b = Box::new_in("x", &allocator);
let b = serde_json::to_string(&b).unwrap();
assert_eq!(b, "\"x\"");
let s = serde_json::to_string(&b).unwrap();
assert_eq!(s, r#""x""#);
}

#[test]
fn box_serialize_estree() {
use oxc_estree::{CompactSerializer, ESTree};

let allocator = Allocator::default();
let b = Box::new_in("x", &allocator);

let mut serializer = CompactSerializer::new();
b.serialize(&mut serializer);
let s = serializer.into_string();
assert_eq!(s, r#""x""#);
}

#[test]
Expand Down
31 changes: 27 additions & 4 deletions crates/oxc_allocator/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use std::{
use allocator_api2::vec::Vec as InnerVec;
use bumpalo::Bump;
#[cfg(any(feature = "serialize", test))]
use serde::{Serialize, Serializer};
use oxc_estree::{ESTree, Serializer as ESTreeSerializer};
#[cfg(any(feature = "serialize", test))]
use serde::{Serialize, Serializer as SerdeSerializer};

use crate::{Allocator, Box};

Expand Down Expand Up @@ -274,11 +276,18 @@ where

#[cfg(any(feature = "serialize", test))]
impl<T: Serialize> Serialize for Vec<'_, T> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
fn serialize<S: SerdeSerializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_slice().serialize(serializer)
}
}

#[cfg(any(feature = "serialize", test))]
impl<T: ESTree> ESTree for Vec<'_, T> {
fn serialize<S: ESTreeSerializer>(&self, serializer: S) {
self.as_slice().serialize(serializer);
}
}

impl<T: Hash> Hash for Vec<'_, T> {
#[inline(always)]
fn hash<H: Hasher>(&self, state: &mut H) {
Expand Down Expand Up @@ -319,8 +328,22 @@ mod test {
let allocator = Allocator::default();
let mut v = Vec::new_in(&allocator);
v.push("x");
let v = serde_json::to_string(&v).unwrap();
assert_eq!(v, "[\"x\"]");
let s = serde_json::to_string(&v).unwrap();
assert_eq!(s, r#"["x"]"#);
}

#[test]
fn vec_serialize_estree() {
use oxc_estree::{CompactSerializer, ESTree};

let allocator = Allocator::default();
let mut v = Vec::new_in(&allocator);
v.push("x");

let mut serializer = CompactSerializer::new();
v.serialize(&mut serializer);
let s = serializer.into_string();
assert_eq!(s, r#"["x"]"#);
}

#[test]
Expand Down
6 changes: 0 additions & 6 deletions crates/oxc_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,12 @@ bitflags = { workspace = true }
cow-utils = { workspace = true }
nonmax = { workspace = true }

serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }

[features]
default = []
serialize = [
"dep:serde",
"dep:serde_json",
"oxc_allocator/serialize",
"oxc_regular_expression/serialize",
"oxc_span/serialize",
"oxc_syntax/serialize",
"oxc_syntax/to_js_string",
"oxc_estree/serialize",
]
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast_impl/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl NumericLiteral<'_> {
return int32_value;
}

// NaN, Infinity if not included in our NumericLiteral, so we just serde(skip) step 2.
// NaN, Infinity if not included in our NumericLiteral, so we just skip step 2.

// step 3
let pos_int = num.signum() * num.abs().floor();
Expand Down
Loading

0 comments on commit beea0cc

Please sign in to comment.