diff --git a/Cargo.lock b/Cargo.lock index 016d2c2f4..d13a36ae4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1220,7 +1220,6 @@ dependencies = [ "oxc_allocator", "oxc_span", "oxc_syntax", - "ryu-js", "serde", "serde_json", "static_assertions", @@ -1603,6 +1602,7 @@ dependencies = [ "oxc_span", "phf", "rustc-hash", + "ryu-js", "serde", "tsify", "unicode-id-start", @@ -1655,7 +1655,6 @@ dependencies = [ "oxc_span", "oxc_syntax", "rustc-hash", - "ryu-js", "serde", ] diff --git a/crates/oxc_ast/Cargo.toml b/crates/oxc_ast/Cargo.toml index 04cf42d7a..5390d9729 100644 --- a/crates/oxc_ast/Cargo.toml +++ b/crates/oxc_ast/Cargo.toml @@ -27,7 +27,6 @@ num-bigint = { workspace = true } serde = { workspace = true, features = ["derive"], optional = true } serde_json = { workspace = true, optional = true } -ryu-js = { workspace = true, optional = true } tsify = { workspace = true, optional = true } wasm-bindgen = { workspace = true, optional = true } @@ -40,9 +39,9 @@ serialize = [ "oxc_allocator/serialize", "dep:serde", "dep:serde_json", - "dep:ryu-js", "dep:tsify", "dep:wasm-bindgen", "oxc_span/serialize", "oxc_syntax/serialize", + "oxc_syntax/to_js_string", ] diff --git a/crates/oxc_ast/src/serialize.rs b/crates/oxc_ast/src/serialize.rs index 7a71de213..fbbf1bccd 100644 --- a/crates/oxc_ast/src/serialize.rs +++ b/crates/oxc_ast/src/serialize.rs @@ -20,9 +20,8 @@ impl serde_json::ser::Formatter for EcmaFormatter { where W: ?Sized + std::io::Write, { - let mut buffer = ryu_js::Buffer::new(); - let s = buffer.format(value); - writer.write_all(s.as_bytes()) + use oxc_syntax::number::ToJsString; + writer.write_all(value.to_js_string().as_bytes()) } } diff --git a/crates/oxc_syntax/Cargo.toml b/crates/oxc_syntax/Cargo.toml index c634ad97b..e668284e1 100644 --- a/crates/oxc_syntax/Cargo.toml +++ b/crates/oxc_syntax/Cargo.toml @@ -28,10 +28,12 @@ rustc-hash = { workspace = true } dashmap = { workspace = true } phf = { workspace = true, features = ["macros"] } +ryu-js = { workspace = true, optional = true } serde = { workspace = true, features = ["derive"], optional = true } tsify = { workspace = true, optional = true } wasm-bindgen = { workspace = true, optional = true } [features] -default = [] -serialize = ["dep:serde", "dep:tsify", "dep:wasm-bindgen", "oxc_index/serialize", "bitflags/serde"] +default = [] +to_js_string = ["dep:ryu-js"] +serialize = ["dep:serde", "dep:tsify", "dep:wasm-bindgen", "oxc_index/serialize", "bitflags/serde"] diff --git a/crates/oxc_syntax/src/number.rs b/crates/oxc_syntax/src/number.rs index 5603b1b59..fa81f2a04 100644 --- a/crates/oxc_syntax/src/number.rs +++ b/crates/oxc_syntax/src/number.rs @@ -26,3 +26,17 @@ impl BigintBase { self == &Self::Decimal } } + +/// +#[cfg(feature = "to_js_string")] +pub trait ToJsString { + fn to_js_string(&self) -> String; +} + +#[cfg(feature = "to_js_string")] +impl ToJsString for f64 { + fn to_js_string(&self) -> String { + let mut buffer = ryu_js::Buffer::new(); + buffer.format(*self).to_string() + } +} diff --git a/crates/oxc_transformer/Cargo.toml b/crates/oxc_transformer/Cargo.toml index b34e6654a..3d78c0733 100644 --- a/crates/oxc_transformer/Cargo.toml +++ b/crates/oxc_transformer/Cargo.toml @@ -24,12 +24,11 @@ oxc_span = { workspace = true } oxc_allocator = { workspace = true } oxc_semantic = { workspace = true } oxc_diagnostics = { workspace = true } -oxc_syntax = { workspace = true } +oxc_syntax = { workspace = true, features = ["to_js_string"] } rustc-hash = { workspace = true } indexmap = { workspace = true } serde = { workspace = true, features = ["derive"] } -ryu-js = { workspace = true } [dev-dependencies] oxc_parser = { workspace = true } diff --git a/crates/oxc_transformer/src/typescript/enum.rs b/crates/oxc_transformer/src/typescript/enum.rs index d2700af21..15a329432 100644 --- a/crates/oxc_transformer/src/typescript/enum.rs +++ b/crates/oxc_transformer/src/typescript/enum.rs @@ -4,11 +4,10 @@ use oxc_allocator::{Box, Vec}; use oxc_ast::{ast::*, visit::walk_mut, VisitMut}; use oxc_span::{Atom, SPAN}; use oxc_syntax::{ - number::NumberBase, + number::{NumberBase, ToJsString}, operator::{AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator}, }; use rustc_hash::FxHashMap; -use ryu_js::Buffer; use crate::context::Ctx; @@ -392,12 +391,12 @@ impl<'a> TypeScriptEnum<'a> { { let left_string = match left { ConstantValue::String(str) => str, - ConstantValue::Number(v) => f64_to_js_string(v), + ConstantValue::Number(v) => v.to_js_string(), }; let right_string = match right { ConstantValue::String(str) => str, - ConstantValue::Number(v) => f64_to_js_string(v), + ConstantValue::Number(v) => v.to_js_string(), }; return Some(ConstantValue::String(format!("{left_string}{right_string}"))); @@ -537,8 +536,3 @@ impl<'a> VisitMut<'a> for IdentifierReferenceRename<'a> { } } } - -fn f64_to_js_string(value: f64) -> String { - let mut buffer = Buffer::new(); - buffer.format(value).to_string() -}