From 130ac6f31b75f268d78d6d335e3d33fe08077b48 Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Mon, 25 Nov 2024 18:02:13 +0100 Subject: [PATCH] Remove need to depend on js-sys when consuming errors (#36) Switch from requiring consumers to depend on js-sys and let bitwarden-error crate re-expose it. Do we need to do the same thing for tsify? --- Cargo.lock | 1 - crates/bitwarden-core/Cargo.toml | 2 -- crates/bitwarden-error-macro/src/basic/attribute.rs | 4 ++-- crates/bitwarden-error-macro/src/flat/attribute.rs | 4 ++-- crates/bitwarden-error-macro/src/full/attribute.rs | 2 +- crates/bitwarden-error/Cargo.toml | 11 ++++++++--- crates/bitwarden-error/src/lib.rs | 13 +++++++++++++ 7 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 39ecb28b..f41be3aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -357,7 +357,6 @@ dependencies = [ "chrono", "getrandom", "hmac", - "js-sys", "log", "rand", "rand_chacha", diff --git a/crates/bitwarden-core/Cargo.toml b/crates/bitwarden-core/Cargo.toml index 88fb7916..fe1c6a1c 100644 --- a/crates/bitwarden-core/Cargo.toml +++ b/crates/bitwarden-core/Cargo.toml @@ -23,7 +23,6 @@ secrets = [] # Secrets manager API wasm = [ "bitwarden-error/wasm", "dep:wasm-bindgen", - "dep:js-sys", "dep:tsify-next", ] # WASM support @@ -54,7 +53,6 @@ wasm-bindgen = { workspace = true, optional = true } zeroize = { version = ">=1.7.0, <2.0", features = ["derive", "aarch64"] } zxcvbn = { version = ">=3.0.1, <4.0", optional = true } tsify-next = { workspace = true, optional = true } -js-sys = { workspace = true, optional = true } bitwarden-error = { workspace = true } [target.'cfg(not(target_arch="wasm32"))'.dependencies] diff --git a/crates/bitwarden-error-macro/src/basic/attribute.rs b/crates/bitwarden-error-macro/src/basic/attribute.rs index b060517f..a900bba2 100644 --- a/crates/bitwarden-error-macro/src/basic/attribute.rs +++ b/crates/bitwarden-error-macro/src/basic/attribute.rs @@ -36,14 +36,14 @@ fn basic_error_wasm( quote! { const _: () = { - use wasm_bindgen::prelude::*; + use bitwarden_error::wasm_bindgen::prelude::*; #[wasm_bindgen(typescript_custom_section)] const TS_APPEND_CONTENT: &'static str = #ts_code; #[wasm_bindgen(js_name = #is_error_function_name, skip_typescript)] pub fn is_error(error: &JsValue) -> bool { - let name_js_value = js_sys::Reflect::get(&error, &JsValue::from_str("name")).unwrap_or(JsValue::NULL); + let name_js_value = bitwarden_error::js_sys::Reflect::get(&error, &JsValue::from_str("name")).unwrap_or(JsValue::NULL); let name = name_js_value.as_string().unwrap_or_default(); name == #export_as_identifier_str } diff --git a/crates/bitwarden-error-macro/src/flat/attribute.rs b/crates/bitwarden-error-macro/src/flat/attribute.rs index b77791fa..9af2be59 100644 --- a/crates/bitwarden-error-macro/src/flat/attribute.rs +++ b/crates/bitwarden-error-macro/src/flat/attribute.rs @@ -89,14 +89,14 @@ fn flat_error_wasm( quote! { const _: () = { - use wasm_bindgen::prelude::*; + use bitwarden_error::wasm_bindgen::prelude::*; #[wasm_bindgen(typescript_custom_section)] const TS_APPEND_CONTENT: &'static str = #ts_code; #[wasm_bindgen(js_name = #is_error_function_name, skip_typescript)] pub fn is_error(error: &JsValue) -> bool { - let name_js_value = js_sys::Reflect::get(&error, &JsValue::from_str("name")).unwrap_or(JsValue::NULL); + let name_js_value = bitwarden_error::js_sys::Reflect::get(&error, &JsValue::from_str("name")).unwrap_or(JsValue::NULL); let name = name_js_value.as_string().unwrap_or_default(); name == #export_as_identifier_str } diff --git a/crates/bitwarden-error-macro/src/full/attribute.rs b/crates/bitwarden-error-macro/src/full/attribute.rs index 90acbcf9..88ad8d86 100644 --- a/crates/bitwarden-error-macro/src/full/attribute.rs +++ b/crates/bitwarden-error-macro/src/full/attribute.rs @@ -14,7 +14,7 @@ pub(crate) fn bitwarden_error_full( let wasm_attributes = cfg!(feature = "wasm").then(|| { quote! { - #[derive(tsify_next::Tsify)] + #[derive(bitwarden_error::tsify_next::Tsify)] #[tsify(into_wasm_abi)] } }); diff --git a/crates/bitwarden-error/Cargo.toml b/crates/bitwarden-error/Cargo.toml index 17fd9fdd..5c6aaa42 100644 --- a/crates/bitwarden-error/Cargo.toml +++ b/crates/bitwarden-error/Cargo.toml @@ -10,18 +10,23 @@ license-file.workspace = true keywords.workspace = true [features] -wasm = ["bitwarden-error-macro/wasm", "dep:wasm-bindgen"] +wasm = [ + "bitwarden-error-macro/wasm", + "dep:js-sys", + "dep:tsify-next", + "dep:wasm-bindgen", +] [dependencies] bitwarden-error-macro = { workspace = true } +js-sys = { workspace = true, optional = true } +tsify-next = { workspace = true, optional = true } wasm-bindgen = { workspace = true, optional = true } [lints] workspace = true [dev-dependencies] -js-sys = "0.3.72" serde.workspace = true trybuild = "1.0.101" -tsify-next.workspace = true wasm-bindgen-test = "0.3.45" diff --git a/crates/bitwarden-error/src/lib.rs b/crates/bitwarden-error/src/lib.rs index 0eb32f24..24e59ab5 100644 --- a/crates/bitwarden-error/src/lib.rs +++ b/crates/bitwarden-error/src/lib.rs @@ -3,6 +3,19 @@ pub mod flat_error; #[cfg(feature = "wasm")] pub mod wasm; +/// Re-export the `js_sys` crate since the proc macro depends on it. +#[cfg(feature = "wasm")] +#[doc(hidden)] +pub use ::js_sys; +/// Re-export the `tsify_next` crate since the proc macro depends on it. +#[cfg(feature = "wasm")] +#[doc(hidden)] +pub use ::tsify_next; +/// Re-export the `wasm_bindgen` crate since the proc macro depends on it. +#[cfg(feature = "wasm")] +#[doc(hidden)] +pub use ::wasm_bindgen; + pub mod prelude { pub use bitwarden_error_macro::*;