feat(syntax): add ToJsString trait for f64 (#3131)

This commit is contained in:
Boshen 2024-04-29 21:00:04 +08:00 committed by GitHub
parent a8af5de8f5
commit 870d11f1bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 26 additions and 20 deletions

3
Cargo.lock generated
View file

@ -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",
]

View file

@ -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",
]

View file

@ -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())
}
}

View file

@ -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"]

View file

@ -26,3 +26,17 @@ impl BigintBase {
self == &Self::Decimal
}
}
/// <https://tc39.es/ecma262/#sec-numeric-types-number-tostring>
#[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()
}
}

View file

@ -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 }

View file

@ -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()
}