From 584b0bfdab00d92b3ada405486d1b6278ac8ba66 Mon Sep 17 00:00:00 2001 From: Chielo Newctle Date: Fri, 10 Jan 2025 15:14:43 +0800 Subject: [PATCH] fix: exclude `cfg` of `js-sys` from macro expansion --- garde/Cargo.toml | 2 +- garde_derive/Cargo.toml | 1 + garde_derive/src/emit.rs | 11 ++++++++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/garde/Cargo.toml b/garde/Cargo.toml index 1b73480..e4d4883 100644 --- a/garde/Cargo.toml +++ b/garde/Cargo.toml @@ -34,7 +34,7 @@ email = ["regex"] email-idna = ["dep:idna"] regex = ["dep:regex", "dep:once_cell", "garde_derive?/regex"] pattern = ["regex"] # for backward compatibility with <0.14.0 -js-sys = ["dep:js-sys"] +js-sys = ["dep:js-sys", "garde_derive?/js-sys"] rust_decimal = ["dep:rust_decimal"] [dependencies] diff --git a/garde_derive/Cargo.toml b/garde_derive/Cargo.toml index 1107214..e4ed286 100644 --- a/garde_derive/Cargo.toml +++ b/garde_derive/Cargo.toml @@ -15,6 +15,7 @@ proc-macro = true [features] regex = ["dep:regex"] +js-sys = [] [dependencies] syn = { version = "2", features = ["full", "derive"] } diff --git a/garde_derive/src/emit.rs b/garde_derive/src/emit.rs index f08fa0b..782df6f 100644 --- a/garde_derive/src/emit.rs +++ b/garde_derive/src/emit.rs @@ -304,10 +304,9 @@ impl ToTokens for Rules<'_> { } Pattern(pat) => match pat { model::ValidatePattern::Expr(expr) => quote_spanned!(expr.span() => (&#expr,)), - #[cfg(feature = "regex")] + #[cfg(all(feature = "regex", feature = "js-sys"))] model::ValidatePattern::Lit(s) => quote!({ #[cfg(not(all( - feature = "js-sys", target_arch = "wasm32", target_os = "unknown" )))] @@ -315,7 +314,6 @@ impl ToTokens for Rules<'_> { #rules_mod::pattern::regex::init_pattern!(#s); #[cfg(all( - feature = "js-sys", target_arch = "wasm32", target_os = "unknown" ))] @@ -324,6 +322,13 @@ impl ToTokens for Rules<'_> { (&PATTERN,) }), + #[cfg(all(feature = "regex", not(feature = "js-sys")))] + model::ValidatePattern::Lit(s) => quote!({ + static PATTERN: #rules_mod::pattern::regex::StaticPattern = + #rules_mod::pattern::regex::init_pattern!(#s); + + (&PATTERN,) + }), }, };