Skip to content

Commit dd4b9e8

Browse files
authored
refactor(bindings): Deprecate jsvalue::*_serde (#6462)
1 parent 44b0790 commit dd4b9e8

File tree

14 files changed

+388
-58
lines changed

14 files changed

+388
-58
lines changed

.github/workflows/CI.yml

+1
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ jobs:
641641
- name: Run cargo test (core)
642642
if: matrix.settings.crate == 'swc_core'
643643
run: |
644+
rustup target add wasm32-unknown-unknown
644645
cargo test -p swc_core --features ecma_quote --features common --features ecma_utils
645646
646647
- name: Run cargo test (binding_core_wasm)

Cargo.lock

+18-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/binding_core_wasm/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ swc_core = { version = "0.43.15", features = [
3131
] }
3232
tracing = { version = "0.1.37", features = ["max_level_off"] }
3333
wasm-bindgen = { version = "0.2.82", features = [
34-
"serde-serialize",
3534
"enable-interning",
3635
] }
3736

bindings/binding_core_wasm/src/lib.rs

+36-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use anyhow::Error;
2+
use serde::Serialize;
3+
use serde_wasm_bindgen::Serializer;
24
use swc_core::{
35
base::HandlerOpts,
46
binding_macros::wasm::{
@@ -21,6 +23,12 @@ use swc_core::{
2123
use wasm_bindgen::{prelude::*, JsCast};
2224
mod types;
2325

26+
// A serializer with options to provide backward compat for the input / output
27+
// from the bindgen generated swc interfaces.
28+
const COMPAT_SERIALIZER: Serializer = Serializer::new()
29+
.serialize_maps_as_objects(true)
30+
.serialize_missing_as_null(true);
31+
2432
/// Custom interface definitions for the @swc/wasm's public interface instead of
2533
/// auto generated one, which is not reflecting most of types in detail.
2634
#[wasm_bindgen(typescript_custom_section)]
@@ -81,12 +89,16 @@ pub fn minify_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
8189
let opts = if opts.is_null() || opts.is_undefined() {
8290
Default::default()
8391
} else {
84-
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
92+
serde_wasm_bindgen::from_value(opts)
93+
.map_err(|e| anyhow::anyhow!("failed to parse options: {}", e))?
8594
};
8695
let fm = c.cm.new_source_file(FileName::Anon, s.into());
8796
let program =
8897
anyhow::Context::context(c.minify(fm, handler, &opts), "failed to minify file")?;
89-
anyhow::Context::context(JsValue::from_serde(&program), "failed to serialize json")
98+
99+
program
100+
.serialize(&COMPAT_SERIALIZER)
101+
.map_err(|e| anyhow::anyhow!("failed to serialize program: {}", e))
90102
})
91103
})
92104
.map_err(|e| convert_err(e, None))
@@ -106,7 +118,8 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
106118
let opts: ParseOptions = if opts.is_null() || opts.is_undefined() {
107119
Default::default()
108120
} else {
109-
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
121+
serde_wasm_bindgen::from_value(opts)
122+
.map_err(|e| anyhow::anyhow!("failed to parse options: {}", e))?
110123
};
111124
let fm = c.cm.new_source_file(FileName::Anon, s.into());
112125
let cmts = c.comments().clone();
@@ -133,7 +146,9 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
133146
opts.syntax.typescript(),
134147
));
135148

136-
anyhow::Context::context(JsValue::from_serde(&program), "failed to serialize json")
149+
program
150+
.serialize(&COMPAT_SERIALIZER)
151+
.map_err(|e| anyhow::anyhow!("failed to serialize program: {}", e))
137152
})
138153
})
139154
})
@@ -160,9 +175,9 @@ pub fn transform_sync(
160175
let opts: Options = if opts.is_null() || opts.is_undefined() {
161176
Default::default()
162177
} else {
163-
anyhow::Context::context(opts.into_serde(), "failed to parse options")
164-
.map_err(|e| convert_err(e, None))?
178+
serde_wasm_bindgen::from_value(opts)?
165179
};
180+
166181
let error_format = opts.experimental.error_format.unwrap_or_default();
167182
try_with_handler(c.cm.clone(), Default::default(), |handler| {
168183
c.run(|| {
@@ -193,9 +208,13 @@ pub fn transform_sync(
193208
"failed to process js file",
194209
)?
195210
}
196-
Err(v) => unsafe { c.process_js(handler, v.into_serde().expect(""), &opts)? },
211+
Err(v) => {
212+
c.process_js(handler, serde_wasm_bindgen::from_value(v).expect("Should able to deserialize into program"), &opts)?
213+
}
197214
};
198-
anyhow::Context::context(JsValue::from_serde(&out), "failed to serialize json")
215+
216+
out.serialize(&COMPAT_SERIALIZER)
217+
.map_err(|e| anyhow::anyhow!("failed to serialize transform result: {}", e))
199218
})
200219
})
201220
.map_err(|e| convert_err(e, Some(error_format)))
@@ -218,10 +237,13 @@ pub fn print_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> {
218237
let opts: Options = if opts.is_null() || opts.is_undefined() {
219238
Default::default()
220239
} else {
221-
anyhow::Context::context(opts.into_serde(), "failed to parse options")?
240+
serde_wasm_bindgen::from_value(opts)
241+
.map_err(|e| anyhow::anyhow!("failed to parse options: {}", e))?
222242
};
223-
let program: Program =
224-
anyhow::Context::context(s.into_serde(), "failed to deserialize program")?;
243+
244+
let program: Program = serde_wasm_bindgen::from_value(s)
245+
.map_err(|e| anyhow::anyhow!("failed to deserialize program: {}", e))?;
246+
225247
let s = anyhow::Context::context(
226248
c.print(
227249
&program,
@@ -241,7 +263,9 @@ pub fn print_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> {
241263
),
242264
"failed to print code",
243265
)?;
244-
anyhow::Context::context(JsValue::from_serde(&s), "failed to serialize json")
266+
267+
serde_wasm_bindgen::to_value(&s)
268+
.map_err(|e| anyhow::anyhow!("failed to serialize json: {}", e))
245269
})
246270
})
247271
.map_err(|e| convert_err(e, None))

crates/binding_macros/Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ binding_wasm = [
1818
"swc_common",
1919
"swc_ecma_transforms",
2020
"swc_ecma_ast",
21+
"swc_ecma_visit",
2122

2223
# Optional packages
2324
"once_cell",
2425
"wasm-bindgen",
2526
"wasm-bindgen-futures",
2627
"js-sys",
28+
"serde",
29+
"serde-wasm-bindgen",
2730
"anyhow",
2831
"console_error_panic_hook",
2932
]
@@ -34,14 +37,16 @@ swc = { optional = true, version = "0.232.99", path = "../swc" }
3437
swc_common = { optional = true, version = "0.29.14", path = "../swc_common" }
3538
swc_ecma_ast = { optional = true, version = "0.94.19", path = "../swc_ecma_ast" }
3639
swc_ecma_transforms = { optional = true, version = "0.198.52", path = "../swc_ecma_transforms" }
40+
swc_ecma_visit = { optional = true, version = "0.80.19", path = "../swc_ecma_visit" }
3741

3842
# Optional deps for the wasm binding macro
3943
anyhow = { optional = true, version = "1.0.58" }
4044
console_error_panic_hook = { optional = true, version = "0.1.7" }
4145
js-sys = { optional = true, version = "0.3.59" }
4246
once_cell = { optional = true, version = "1.13.0" }
47+
serde = { optional = true, version = "1", features = ["derive"] }
48+
serde-wasm-bindgen = { optional = true, version = "0.4.5" }
4349
wasm-bindgen = { optional = true, version = "0.2.82", features = [
44-
"serde-serialize",
4550
"enable-interning",
4651
] }
4752
wasm-bindgen-futures = { optional = true, version = "0.4.32" }

0 commit comments

Comments
 (0)