fix(backend): attribute of a struct marked as #[napi(constructor)] contain Rust keywords, causing to throw is not a valid identifier
when generating getters and setters
This commit is contained in:
parent
b1dd613243
commit
4259e85e0e
6 changed files with 48 additions and 4 deletions
|
@ -628,8 +628,14 @@ impl NapiStruct {
|
|||
};
|
||||
let ty = &field.ty;
|
||||
|
||||
let getter_name = Ident::new(&format!("get_{}", field_name), Span::call_site());
|
||||
let setter_name = Ident::new(&format!("set_{}", field_name), Span::call_site());
|
||||
let getter_name = Ident::new(
|
||||
&format!("get_{}", rm_raw_prefix(&field_name)),
|
||||
Span::call_site(),
|
||||
);
|
||||
let setter_name = Ident::new(
|
||||
&format!("set_{}", rm_raw_prefix(&field_name)),
|
||||
Span::call_site(),
|
||||
);
|
||||
|
||||
if field.getter {
|
||||
let default_to_napi_value_convert = quote! {
|
||||
|
@ -748,12 +754,18 @@ impl NapiStruct {
|
|||
};
|
||||
|
||||
if field.getter {
|
||||
let getter_name = Ident::new(&format!("get_{}", field_name), Span::call_site());
|
||||
let getter_name = Ident::new(
|
||||
&format!("get_{}", rm_raw_prefix(&field_name)),
|
||||
Span::call_site(),
|
||||
);
|
||||
(quote! { .with_getter(#getter_name) }).to_tokens(&mut prop);
|
||||
}
|
||||
|
||||
if field.writable && field.setter {
|
||||
let setter_name = Ident::new(&format!("set_{}", field_name), Span::call_site());
|
||||
let setter_name = Ident::new(
|
||||
&format!("set_{}", rm_raw_prefix(&field_name)),
|
||||
Span::call_site(),
|
||||
);
|
||||
(quote! { .with_setter(#setter_name) }).to_tokens(&mut prop);
|
||||
}
|
||||
|
||||
|
@ -904,3 +916,11 @@ impl NapiImpl {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn rm_raw_prefix(s: &str) -> &str {
|
||||
if s.starts_with("r#") {
|
||||
&s[2..]
|
||||
} else {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
|
|
@ -194,6 +194,14 @@ Generated by [AVA](https://avajs.dev).
|
|||
static optionOnly(optional?: string | undefined | null): string␊
|
||||
}␊
|
||||
␊
|
||||
export class Selector {␊
|
||||
orderBy: Array<string>␊
|
||||
select: Array<string>␊
|
||||
struct: string␊
|
||||
where?: string␊
|
||||
constructor(orderBy: Array<string>, select: Array<string>, struct: string, where?: string)␊
|
||||
}␊
|
||||
␊
|
||||
export class Width {␊
|
||||
value: number␊
|
||||
constructor(value: number)␊
|
||||
|
|
Binary file not shown.
8
examples/napi/index.d.ts
vendored
8
examples/napi/index.d.ts
vendored
|
@ -184,6 +184,14 @@ export class Optional {
|
|||
static optionOnly(optional?: string | undefined | null): string
|
||||
}
|
||||
|
||||
export class Selector {
|
||||
orderBy: Array<string>
|
||||
select: Array<string>
|
||||
struct: string
|
||||
where?: string
|
||||
constructor(orderBy: Array<string>, select: Array<string>, struct: string, where?: string)
|
||||
}
|
||||
|
||||
export class Width {
|
||||
value: number
|
||||
constructor(value: number)
|
||||
|
|
7
examples/napi/src/constructor.rs
Normal file
7
examples/napi/src/constructor.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
#[napi(constructor)]
|
||||
pub struct Selector {
|
||||
pub order_by: Vec<String>,
|
||||
pub select: Vec<String>,
|
||||
pub r#struct: String,
|
||||
pub r#where: Option<String>,
|
||||
}
|
|
@ -27,6 +27,7 @@ mod bigint;
|
|||
mod callback;
|
||||
mod class;
|
||||
mod class_factory;
|
||||
mod constructor;
|
||||
mod date;
|
||||
mod either;
|
||||
mod r#enum;
|
||||
|
|
Loading…
Reference in a new issue