fix (macro-rs): use cfg
instead of cfg_attr
This commit is contained in:
parent
8b6e300d4e
commit
5d2238a266
1 changed files with 65 additions and 66 deletions
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue