From 5d2238a266cf905e8e3343c464d8bbd3602c1fa5 Mon Sep 17 00:00:00 2001 From: sup39 Date: Fri, 12 Apr 2024 04:26:36 +0900 Subject: [PATCH] fix (macro-rs): use `cfg` instead of `cfg_attr` --- packages/macro-rs/src/lib.rs | 131 +++++++++++++++++------------------ 1 file changed, 65 insertions(+), 66 deletions(-) diff --git a/packages/macro-rs/src/lib.rs b/packages/macro-rs/src/lib.rs index 14c4658fe3..54dd378ec9 100644 --- a/packages/macro-rs/src/lib.rs +++ b/packages/macro-rs/src/lib.rs @@ -13,40 +13,36 @@ use quote::{quote, ToTokens}; /// ## Example with `i32` argument /// ```rust /// #[macro_rs::napi] -/// fn add_one(x: i32) -> i32 { -/// x + 1 +/// pub fn add_one(x: i32) -> i32 { +/// x + 1 /// } /// ``` /// -/// becomes +/// generates /// /// ```rust -/// fn add_one(x: i32) -> i32 { -/// x + 1 -/// } -/// #[cfg_attr(feature = "napi", napi_derive::napi(js_name = "addOne"))] -/// fn add_one_napi(x: i32) -> i32 { -/// add_one(x) +/// #[cfg(feature = "napi")] +/// #[napi_derive::napi(js_name = "addOne")] +/// pub fn add_one_napi(x: i32) -> i32 { +/// add_one(x) /// } /// ``` /// /// ## Example with `&str` argument /// ```rust /// #[macro_rs::napi] -/// fn concatenate_string(str1: &str, str2: &str) -> String { -/// str1.to_owned() + str2 +/// pub fn concatenate_string(str1: &str, str2: &str) -> String { +/// str1.to_owned() + str2 /// } /// ``` /// -/// becomes +/// generates /// /// ```rust -/// fn concatenate_string(str1: &str, str2: &str) -> String { -/// str1.to_owned() + str2 -/// } -/// #[cfg_attr(feature = "napi", napi_derive::napi(js_name = "concatenateString"))] -/// fn concatenate_string_napi(str1: String, str2: String) -> String { -/// concatenate_string(&str1, &str2) +/// #[cfg(feature = "napi")] +/// #[napi_derive::napi(js_name = "concatenateString")] +/// pub fn concatenate_string_napi(str1: String, str2: String) -> String { +/// concatenate_string(&str1, &str2) /// } /// ``` /// @@ -142,7 +138,8 @@ fn napi_impl(attr: TokenStream, item: TokenStream) -> TokenStream { quote! { #item_fn - #[cfg_attr(feature = "napi", napi_derive::napi(js_name = #js_name))] + #[cfg(feature = "napi")] + #[napi_derive::napi(js_name = #js_name)] #(#item_fn_attrs)* #item_fn_vis #item_fn_sig { #ident(#(#called_args),*) @@ -155,71 +152,73 @@ mod tests { use proc_macro2::TokenStream; use quote::quote; + macro_rules! test_macro { + ($source:expr, $generated:expr) => { + assert_eq!( + super::napi_impl(TokenStream::new(), $source).to_string(), + format!("{} {}", $source, $generated), + ) + }; + } + #[test] fn primitive_argument() { - let generated = super::napi_impl( - TokenStream::new(), + test_macro!( quote! { - fn add_one(x: i32) -> i32 { - x + 1 - } + pub fn add_one(x: i32) -> i32 { + x + 1 + } }, + quote! { + #[cfg(feature = "napi")] + #[napi_derive::napi(js_name = "addOne")] + pub fn add_one_napi(x: i32) -> i32 { + add_one(x) + } + } ); - let expected = quote! { - fn add_one(x: i32) -> i32 { - x + 1 - } - #[cfg_attr(feature = "napi", napi_derive::napi(js_name = "addOne"))] - fn add_one_napi(x: i32) -> i32 { - add_one(x) - } - }; - assert_eq!(generated.to_string(), expected.to_string()); } #[test] fn str_ref_argument() { - let generated = super::napi_impl( - TokenStream::new(), + test_macro!( quote! { - fn concatenate_string(str1: &str, str2: &str) -> String { - str1.to_owned() + str2 - } + pub fn concatenate_string(str1: &str, str2: &str) -> String { + str1.to_owned() + str2 + } }, + quote! { + #[cfg(feature = "napi")] + #[napi_derive::napi(js_name = "concatenateString")] + pub fn concatenate_string_napi(str1: String, str2: String) -> String { + concatenate_string(&str1, &str2) + } + } ); - let expected = quote! { - fn concatenate_string(str1: &str, str2: &str) -> String { - str1.to_owned() + str2 - } - #[cfg_attr(feature = "napi", napi_derive::napi(js_name = "concatenateString"))] - fn concatenate_string_napi(str1: String, str2: String) -> String { - concatenate_string(&str1, &str2) - } - }; - assert_eq!(generated.to_string(), expected.to_string()); } #[test] fn mut_ref_argument() { - let generated = super::napi_impl( - TokenStream::new(), + test_macro!( quote! { - fn append_string_and_clone(base_str: &mut String, appended_str: &str) -> String { - base_str.push_str(appended_str); - base_str.to_owned() - } + pub fn append_string_and_clone( + base_str: &mut String, + appended_str: &str, + ) -> String { + base_str.push_str(appended_str); + base_str.to_owned() + } }, + quote! { + #[cfg(feature = "napi")] + #[napi_derive::napi(js_name = "appendStringAndClone")] + pub fn append_string_and_clone_napi( + mut base_str: String, + appended_str: String, + ) -> String { + append_string_and_clone(&mut base_str, &appended_str) + } + } ); - let expected = quote! { - fn append_string_and_clone(base_str: &mut String, appended_str: &str) -> String { - base_str.push_str(appended_str); - base_str.to_owned() - } - #[cfg_attr(feature = "napi", napi_derive::napi(js_name = "appendStringAndClone"))] - fn append_string_and_clone_napi(mut base_str: String, appended_str: String) -> String { - append_string_and_clone(&mut base_str, &appended_str) - } - }; - assert_eq!(generated.to_string(), expected.to_string()); } }