Merge pull request #1701 from yoogoc/main

throw `is not a valid identifier` when generating getters and setters
This commit is contained in:
LongYinan 2023-08-25 22:58:44 +08:00 committed by GitHub
commit b235cea33f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 4 deletions

View file

@ -628,8 +628,14 @@ impl NapiStruct {
}; };
let ty = &field.ty; let ty = &field.ty;
let getter_name = Ident::new(&format!("get_{}", field_name), Span::call_site()); let getter_name = Ident::new(
let setter_name = Ident::new(&format!("set_{}", field_name), Span::call_site()); &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 { if field.getter {
let default_to_napi_value_convert = quote! { let default_to_napi_value_convert = quote! {
@ -748,12 +754,18 @@ impl NapiStruct {
}; };
if field.getter { 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); (quote! { .with_getter(#getter_name) }).to_tokens(&mut prop);
} }
if field.writable && field.setter { 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); (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
}
}

View file

@ -194,6 +194,14 @@ Generated by [AVA](https://avajs.dev).
static optionOnly(optional?: string | undefined | null): string␊ 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 {␊ export class Width {␊
value: number␊ value: number␊
constructor(value: number)␊ constructor(value: number)␊

View file

@ -184,6 +184,14 @@ export class Optional {
static optionOnly(optional?: string | undefined | null): string 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 { export class Width {
value: number value: number
constructor(value: number) constructor(value: number)

View 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>,
}

View file

@ -27,6 +27,7 @@ mod bigint;
mod callback; mod callback;
mod class; mod class;
mod class_factory; mod class_factory;
mod constructor;
mod date; mod date;
mod either; mod either;
mod r#enum; mod r#enum;