From 4259e85e0efba5b65dab8f5c10c7958779d03afc Mon Sep 17 00:00:00 2001 From: yoogo Date: Thu, 17 Aug 2023 14:31:30 +0800 Subject: [PATCH] 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 --- crates/backend/src/codegen/struct.rs | 28 +++++++++++++++--- .../__snapshots__/typegen.spec.ts.md | 8 +++++ .../__snapshots__/typegen.spec.ts.snap | Bin 4024 -> 4066 bytes examples/napi/index.d.ts | 8 +++++ examples/napi/src/constructor.rs | 7 +++++ examples/napi/src/lib.rs | 1 + 6 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 examples/napi/src/constructor.rs diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index eaf38521..ae0498f5 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -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 + } +} diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index 3b6ab303..921209c8 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -194,6 +194,14 @@ Generated by [AVA](https://avajs.dev). static optionOnly(optional?: string | undefined | null): string␊ }␊ ␊ + export class Selector {␊ + orderBy: Array␊ + select: Array␊ + struct: string␊ + where?: string␊ + constructor(orderBy: Array, select: Array, struct: string, where?: string)␊ + }␊ + ␊ export class Width {␊ value: number␊ constructor(value: number)␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 9b0a029a85bc9927457a0c551e2ec04934e4d522..ec00a21503d939194c1ca3734a89512b7527f7c4 100644 GIT binary patch literal 4066 zcmV<84;}D9RzVq$M%O8sf00000000BETkmq*wh>R;>2yZl`Ua>w9rHYA#gaT8_r|hEk|jru^=EYF zIvK~4@Q%1UT0HVXfRZnYW|}wX=YHuVU-|}pu0BF{7XS&8An&B8NgR{l?qUHfe)|Ww zpU1-}8J&xte&I5d5&!fTF-@{4U=tqm)R&x%k~HNb$>d_rS-{6^97a48u|Ee&DSrAn z2z=>({p>%#`|J;&;Xl9m{bzst?Z2OW{($+JOdc9R0UIvZ@y_Yt!?Qkn@c9~i=OPMY z`7j8DKa4mwuC4J;^CXpQ6!}81{ZEpou^*ibKY|L+2G7~m8e=KYAl4KgjlTJmD&&ED7EZB_8X%Z+&Li#csvCB|SQG_L})6)Y0stK1Q+v^zV z$Z8s^y&^Rl8dB>K3DacBSNh@7T1u+dSr*CgA=nj;eKzBk%ig zo!W6Am(#?bg(U^;(W`N;udkPe4Vu18CN%JlJ^QM+FL=tC4}U~514X8O3KPgv&=M9h ze}%Dup~fRF6qlU(lIi^i*2TrhpToYAX< zmUpl)Q`R-~0gti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U*J zLIe=P*f^#>#wz1HTo@ocQ>C{Y_!)X33+Xgv+D+ z{z}pD#Ag1vLviWWot@JJvmG6)OlAa4?(9f@?&wgeel`u>tDmEu&8kXabK-0rOhb$p zS^X$Vw<8K7$FulObF^?uKmnn~K2fQT5vwuOd~>)Wiq%@0j2RWrYaSeS-!nyY_M)54 zvrQLa!p68i`r!OLaYYj)M2OT@V;k*lsH@K$p)R5h1$>~+E>p_mws zr7;)by!46tsY^Fg4qK+qsE@^cn9!@oJgwZ?a;hR4m$yk6F6CebQFF9|7q4-*>DMNP zS0eX>y~e_M6LWiBq-mDO3o3;Bll=sOF&0%S7H!qqCJ_dntfVR^dvUrZ8*Ds+wOlM) ztxmIKFa~-sLiLx!VlH&lc?2Y7s&KfUV6LT$h_;wmFOC+TSud~W_}w|CFH$32x>MB0 zDI_ks?sv~V-^1MJH4h^+Txd6ss`&(f9Pb+5QxEsNVYs`^>T!a{NsDZ5b9hisG^Oz^ zXa*8g_N>`U33Lk=UJV_fqupjx4XMYl`& zPCNPl%3cGw=J8csth%dkfCbv|=vL(^^uHycc<6fms|50M6_aQg&R#|a&qkI-hs{#? zE75TtA;lz7{o+Ak=aO~l1j0~`a)tKb@rGs~u(0SH>E7ToIS|@l&$TRu>>Ps1k z2~hC;*1I^N>Kb5(p^tO*NQE#O0i2>>fpkNGRgs-pm`n}&Vzts_U^F+La`RDf&&quF zK%(+ncwVf}Byh0HqMBvhR)>eV`|P@uk+W$R7_g;WZq8-ynK&0Gy7YUYuF7|wN4w + select: Array + struct: string + where?: string + constructor(orderBy: Array, select: Array, struct: string, where?: string) +} + export class Width { value: number constructor(value: number) diff --git a/examples/napi/src/constructor.rs b/examples/napi/src/constructor.rs new file mode 100644 index 00000000..5661362b --- /dev/null +++ b/examples/napi/src/constructor.rs @@ -0,0 +1,7 @@ +#[napi(constructor)] +pub struct Selector { + pub order_by: Vec, + pub select: Vec, + pub r#struct: String, + pub r#where: Option, +} diff --git a/examples/napi/src/lib.rs b/examples/napi/src/lib.rs index 9fd31d27..cb2b24da 100644 --- a/examples/napi/src/lib.rs +++ b/examples/napi/src/lib.rs @@ -27,6 +27,7 @@ mod bigint; mod callback; mod class; mod class_factory; +mod constructor; mod date; mod either; mod r#enum;