fix(napi-derive): fix macro expansion naming shadow (#1278)
Co-authored-by: fengyu <fengyu.shelby@bytedance.com>
This commit is contained in:
parent
5ba603459b
commit
bc69e15efb
1 changed files with 11 additions and 8 deletions
|
@ -405,9 +405,11 @@ impl NapiStruct {
|
||||||
|
|
||||||
match &field.name {
|
match &field.name {
|
||||||
syn::Member::Named(ident) => {
|
syn::Member::Named(ident) => {
|
||||||
field_destructions.push(quote! { #ident });
|
// alias here prevents field name shadowing
|
||||||
|
let alias_ident = format_ident!("{}_", ident);
|
||||||
|
field_destructions.push(quote! { #ident: #alias_ident });
|
||||||
field_conversions.push(
|
field_conversions.push(
|
||||||
quote! { <#ty as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, #ident)? },
|
quote! { <#ty as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, #alias_ident)? },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
syn::Member::Unnamed(i) => {
|
syn::Member::Unnamed(i) => {
|
||||||
|
@ -499,21 +501,22 @@ impl NapiStruct {
|
||||||
};
|
};
|
||||||
match &field.name {
|
match &field.name {
|
||||||
syn::Member::Named(ident) => {
|
syn::Member::Named(ident) => {
|
||||||
field_destructions.push(quote! { #ident });
|
let alias_ident = format_ident!("{}_", ident);
|
||||||
|
field_destructions.push(quote! { #ident: #alias_ident });
|
||||||
if is_optional_field {
|
if is_optional_field {
|
||||||
obj_field_setters.push(quote! {
|
obj_field_setters.push(quote! {
|
||||||
if #ident.is_some() {
|
if #alias_ident.is_some() {
|
||||||
obj.set(#field_js_name, #ident)?;
|
obj.set(#field_js_name, #alias_ident)?;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
obj_field_setters.push(quote! { obj.set(#field_js_name, #ident)?; });
|
obj_field_setters.push(quote! { obj.set(#field_js_name, #alias_ident)?; });
|
||||||
}
|
}
|
||||||
if is_optional_field {
|
if is_optional_field {
|
||||||
obj_field_getters.push(quote! { let #ident: #ty = obj.get(#field_js_name)?; });
|
obj_field_getters.push(quote! { let #alias_ident: #ty = obj.get(#field_js_name)?; });
|
||||||
} else {
|
} else {
|
||||||
obj_field_getters.push(quote! {
|
obj_field_getters.push(quote! {
|
||||||
let #ident: #ty = obj.get(#field_js_name)?.ok_or_else(|| napi::bindgen_prelude::Error::new(
|
let #alias_ident: #ty = obj.get(#field_js_name)?.ok_or_else(|| napi::bindgen_prelude::Error::new(
|
||||||
napi::bindgen_prelude::Status::InvalidArg,
|
napi::bindgen_prelude::Status::InvalidArg,
|
||||||
format!("Missing field `{}`", #field_js_name),
|
format!("Missing field `{}`", #field_js_name),
|
||||||
))?;
|
))?;
|
||||||
|
|
Loading…
Reference in a new issue